119 lines
3.0 KiB
Markdown
119 lines
3.0 KiB
Markdown
# 14.5 Multi-language support
|
|
|
|
In the chapter where we introduced internationalization and localization, we developed the `go-i18n` library. In this section, we will see how this library is integrated into the Beego framework, and how it enables our Beego applications to support both internationalization and localization.
|
|
|
|
## I18n integration
|
|
|
|
Beego first sets some global variables:
|
|
|
|
Translation i18n.IL
|
|
Lang string // set the language pack, zh, en
|
|
LangPath string // set the language pack location
|
|
|
|
A multi-language initialization function is defined:
|
|
|
|
func InitLang(){
|
|
beego.Translation:=i18n.NewLocale()
|
|
beego.Translation.LoadPath(beego.LangPath)
|
|
beego.Translation.SetLocale(beego.Lang)
|
|
}
|
|
|
|
In order to facilitate multi-language calls in the template package directly, we designed three functions for handling multi-language responses:
|
|
|
|
beegoTplFuncMap["Trans"] = i18n.I18nT
|
|
beegoTplFuncMap["TransDate"] = i18n.I18nTimeDate
|
|
beegoTplFuncMap["TransMoney"] = i18n.I18nMoney
|
|
|
|
func I18nT(args ...interface{}) string {
|
|
ok := false
|
|
var s string
|
|
if len(args) == 1 {
|
|
s, ok = args[0].(string)
|
|
}
|
|
if !ok {
|
|
s = fmt.Sprint(args...)
|
|
}
|
|
return beego.Translation.Translate(s)
|
|
}
|
|
|
|
func I18nTimeDate(args ...interface{}) string {
|
|
ok := false
|
|
var s string
|
|
if len(args) == 1 {
|
|
s, ok = args[0].(string)
|
|
}
|
|
if !ok {
|
|
s = fmt.Sprint(args...)
|
|
}
|
|
return beego.Translation.Time(s)
|
|
}
|
|
|
|
func I18nMoney(args ...interface{}) string {
|
|
ok := false
|
|
var s string
|
|
if len(args) == 1 {
|
|
s, ok = args[0].(string)
|
|
}
|
|
if !ok {
|
|
s = fmt.Sprint(args...)
|
|
}
|
|
return beego.Translation.Money(s)
|
|
}
|
|
|
|
## Multi-language development
|
|
|
|
1. Setting the language and location of the language pack, then initialize i18n objects:
|
|
|
|
beego.Lang = "zh"
|
|
beego.LangPath = "views/lang"
|
|
beego.InitLang()
|
|
|
|
2. Designing a multi-language package
|
|
|
|
Above, we talked about how to initialize a multi-language package. Now, let's look at how to design one. Multi-language packages are typically JSON files, as you've already seen in Chapter 10. We must provide translation files for languages we wish to support on our `LangPath`, such as the following:
|
|
|
|
# zh.json
|
|
|
|
{
|
|
"zh": {
|
|
"submit": "提交",
|
|
"create": "创建"
|
|
}
|
|
}
|
|
|
|
# en.json
|
|
|
|
{
|
|
"en": {
|
|
"submit": "Submit",
|
|
"create": "Create"
|
|
}
|
|
}
|
|
|
|
3. Using language packages
|
|
|
|
We can call the controller to get the translated response in the desired language, like so:
|
|
|
|
func (this *MainController) Get() {
|
|
this.Data["create"] = beego.Translation.Translate("create")
|
|
this.TplNames = "index.tpl"
|
|
}
|
|
|
|
We can also directly interpolate translated responses in our templates:
|
|
|
|
// Direct Text translation
|
|
{{.create | Trans}}
|
|
|
|
// Time to translate
|
|
{{.time | TransDate}}
|
|
|
|
// Currency translation
|
|
{{.money | TransMoney}}
|
|
|
|
## Links
|
|
|
|
- [Directory](preface.md)
|
|
- Previous section: [User validation](14.4.md)
|
|
- Next section: [pprof](14.6.md)
|
|
|