-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Nil pointer dereference in Arabic translations #1385
Comments
I can confirm that the Arabic translation caused panic. I was able to resolve it by adding ![]() @BlackSud0 can you verify it? |
Hi @nodivbyzero My Solution to Arabic Translation PanicThe panic was caused by using the cardinal number system ( The FixReplace the cardinal number system with simple singular/plural translations. Here's an example for the "len" validation tag: // Before (problematic code)
customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("len-string", "يجب أن يكون طول {0} مساويا ل {1}", false); err != nil {
return
}
if err = ut.AddCardinal("len-string-character", "{0} حرف", locales.PluralRuleOne, false); err != nil {
return
}
if err = ut.AddCardinal("len-string-character", "{0} أحرف", locales.PluralRuleOther, false); err != nil {
return
}
// ... more AddCardinal calls ...
}
// After (fixed code)
customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("len-string", "يجب أن يكون طول {0} مساويا ل {1}", false); err != nil {
return
}
if err = ut.Add("len-string-character-one", "{0} حرف", false); err != nil {
return
}
if err = ut.Add("len-string-character-other", "{0} أحرف", false); err != nil {
return
}
// ... more simple Add calls ...
} And in the translation function:// Before (problematic code)
c, err = ut.C("len-string-character", f64, digits, ut.FmtNumber(f64, digits))
// After (fixed code)
if f64 == 1 {
c, err = ut.T("len-string-character-one", ut.FmtNumber(f64, digits))
} else {
c, err = ut.T("len-string-character-other", ut.FmtNumber(f64, digits))
} Why This Works
Implementation Notes
The complete solution can be found in this gist ar.go. |
Hey @BlackSud0 Could you please create a new PR with the changes you’re suggesting? |
What happened?
When using Arabic translations with the validator, a nil pointer dereference occurs in the translator. Line 177 (ar.go:177) C method during a struct validation especially (min or max) tags.
Error Stack Trace
Environment & Version
Steps to Reproduce
Example Code
The text was updated successfully, but these errors were encountered: