Add sample codes for plugin i18n

This commit is contained in:
joyqi 2023-02-20 11:46:44 +08:00
parent 12109fdb24
commit 3c2c13f7c4
2 changed files with 19 additions and 2 deletions

View File

@ -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

View File

@ -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)
}
}