Check whether the translator struct is nil before translating.

This commit is contained in:
joyqi 2023-02-22 17:01:48 +08:00
parent 5bd8b1895b
commit 4809bab5c7
1 changed files with 5 additions and 1 deletions

View File

@ -136,6 +136,10 @@ func SetContext(ctx *GinContext, ts ...Translator) {
// Translate translates the key to the current language of the context
func (t *Translator) Translate(ctx *GinContext) string {
if t == nil {
return ""
}
return t.fn(ctx)
}
@ -146,5 +150,5 @@ func (t *Translator) SetContext(ctx *GinContext) {
// String returns the translated string through the context
func (t *Translator) String() string {
return t.fn(t.ctx)
return t.Translate(t.ctx)
}