Plugin Translation
Since answer supports multiple languages, the plugin also needs to support multiple languages. The following is an example of how to make your plugin support multiple languages.
Translator structure
In some plugins interface, you can see the Translator
structure, which is used to support multiple languages.
For example, the ConfigField
structure has a Title
field of type Translator
.
type ConfigField struct {
Name string `json:"name"`
Type ConfigType `json:"type"`
Title Translator `json:"title"`
Description Translator `json:"description"`
Required bool `json:"required"`
Value string `json:"value"`
UIOptions ConfigFieldUIOptions `json:"ui_options"`
Options []ConfigFieldOption `json:"options,omitempty"`
}
It's easy to build a Translator
structure, just like this:
import (
"github.com/apache/incubator-answer/plugin"
)
plugin.MakeTranslator("plugin.github_connector.backend.name")
The plugin.github_connector.backend.name
is the key of the translation file, which will be introduced later.
So, the first step is build a Translator
structure for each field that needs to be translated.
Translation file
Make directory i18n
in the root directory of your plugin, and then create a file named en_US.yaml
in it.
The en_US.yaml
file is used to store the English translation of the plugin.
The content of the en_US.yaml
file is as follows:
plugin:
github_connector:
backend:
name:
other: GitHub
info:
name:
other: GitHub Connector
description:
other: Connect to GitHub for third-party login
config:
client_id:
title:
other: ClientID
description:
other: Client ID of your GitHub application
client_secret:
title:
other: ClientSecret
description:
other: Client secret of your GitHub application
ui:
login:
title: Login with GitHub
description: Login with GitHub
- The
plugin
is the root key of the translation file. - The
github_connector
is the name of the plugin. - The
backend
is translation for the backend. The end of key likeother
is just for go-i18n to recognize the translation file. - The
ui
is translation for the frontend.
You can use the key like plugin.github_connector.backend.name
or plugin.github_connector.ui.login.title
for translation.
Create a i18n.go
file in the root directory of your plugin, and then add the following code:
package i18n
const (
ConnectorName = "plugin.github_connector.backend.name"
InfoName = "plugin.github_connector.backend.info.name"
InfoDescription = "plugin.github_connector.backend.info.description"
ConfigClientIDTitle = "plugin.github_connector.backend.config.client_id.title"
ConfigClientIDDescription = "plugin.github_connector.backend.config.client_id.description"
ConfigClientSecretTitle = "plugin.github_connector.backend.config.client_secret.title"
ConfigClientSecretDescription = "plugin.github_connector.backend.config.client_secret.description"
)
The i18n.go
file is used to store the key of the translation file.
Finally, the directory structure of the plugin is as follows:
.
├── README.md
├── github.go
├── go.mod
├── go.sum
└── i18n
├── en_US.yaml
├── translation.go
└── zh_CN.yaml
Backend translation
You just need to return Translator
structure with the key of the translation file.
func (g *GitHubConnector) ConnectorName() plugin.Translator {
return plugin.MakeTranslator(i18n.ConnectorName)
}
The Answer
will automatically translate the key of the translation file into the corresponding language.