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.

Add sample codes for plugin i18n

joyqi3c2c13f7c439
diff --git a/plugin/connector.go b/plugin/connector.go
index 7fdca12b..7afe4ff4 100644
--- a/plugin/connector.go
+++ b/plugin/connector.go
@@ -8,7 +8,7 @@ type Connector interface {
// ConnectorName presents the name of the connector
// e.g. Facebook, Twitter, Instagram
- ConnectorName() string
+ ConnectorName() Translator
// ConnectorSlugName presents the slug name of the connector
// Please use lowercase and hyphen as the separator
diff --git a/plugin/plugin.go b/plugin/plugin.go
index cc676aca..ff190e71 100644
--- a/plugin/plugin.go
+++ b/plugin/plugin.go
@@ -2,7 +2,8 @@ package plugin
import (
"encoding/json"
-
+ "github.com/answerdev/answer/internal/base/handler"
+ "github.com/answerdev/answer/internal/base/translator"
"github.com/gin-gonic/gin"
)
@@ -101,3 +102,19 @@ func (m *statusManager) MarshalJSON() ([]byte, error) {
func (m *statusManager) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &m.status)
}
+
+// Translator presents a generator of translated string.
+// We use it to delegate the translation work outside the plugin.
+type Translator func(ctx *GinContext) string
+
+// Translate translates the key to the current language of the context
+func Translate(ctx *GinContext, key string) string {
+ return translator.Tr(handler.GetLang(ctx), key)
+}
+
+// MakeTranslator generates a translator from the key
+func MakeTranslator(key string) Translator {
+ return func(ctx *GinContext) string {
+ return translator.Tr(handler.GetLang(ctx), key)
+ }
+}