Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

Check whether the translator struct is nil before translating.

joyqi4809bab5c7c0
diff --git a/plugin/plugin.go b/plugin/plugin.go
index 217bd9a5..47899f78 100644
--- a/plugin/plugin.go
+++ b/plugin/plugin.go
@@ -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)
}