Merging other languages

This commit is contained in:
James Miranda
2016-09-23 18:01:10 -03:00
parent 380a8ee74c
commit de3c5bdaa4
490 changed files with 24539 additions and 24588 deletions

View File

@@ -1,113 +1,118 @@
# 14.5 多语言支持
我们在第十章介绍过国际化和本地化开发了一个go-i18n库这小节我们将把该库集成到beego框架里面来使得我们的框架支持国际化和本地化。
## i18n集成
beego中设置全局变量如下
Translation i18n.IL
Lang string //设置语言包zh、en
LangPath string //设置语言包所在位置
初始化多语言函数:
func InitLang(){
beego.Translation:=i18n.NewLocale()
beego.Translation.LoadPath(beego.LangPath)
beego.Translation.SetLocale(beego.Lang)
}
为了方便在模板中直接调用多语言包,我们设计了三个函数来处理响应的多语言:
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)
}
## 多语言开发使用
1. 设置语言以及语言包所在位置然后初始化i18n对象
beego.Lang = "zh"
beego.LangPath = "views/lang"
beego.InitLang()
2. 设计多语言包
上面讲了如何初始化多语言包现在设计多语言包多语言包是json文件如第十章介绍的一样我们需要把设计的文件放在LangPath下面例如zh.json或者en.json
# zh.json
{
"zh": {
"submit": "提交",
"create": "创建"
}
}
#en.json
{
"en": {
"submit": "Submit",
"create": "Create"
}
}
3. 使用语言包
我们可以在controller中调用翻译获取响应的翻译语言如下所示
func (this *MainController) Get() {
this.Data["create"] = beego.Translation.Translate("create")
this.TplNames = "index.tpl"
}
我们也可以在模板中直接调用响应的翻译函数:
//直接文本翻译
{{.create | Trans}}
//时间翻译
{{.time | TransDate}}
//货币翻译
{{.money | TransMoney}}
## links
* [目录](<preface.md>)
* 上一节: [用户认证](<14.4.md>)
* 下一节: [pprof支持](<14.6.md>)
# 14.5 Multi-language support
We introduced in Chapter internationalization and localization, we developed a go-i18n library, the library this section we will integrate beego frame inside, making our framework supports internationalization and localization.
## I18n integration
beego global variable is set as follows:
Translation i18n.IL
Lang string // set the language pack, zh, en
LangPath string // set the language pack location
Multi-language function to initialize:
func InitLang(){
beego.Translation:=i18n.NewLocale()
beego.Translation.LoadPath(beego.LangPath)
beego.Translation.SetLocale(beego.Lang)
}
In order to facilitate more direct call in the template language pack, we have designed three functions to handle the response of multiple languages:
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)
}
## Use multi-language development
1. Set the location of language and language packs, then initialize the i18n objects:
beego.Lang = "zh"
beego.LangPath = "views/lang"
beego.InitLang()
2. Design Multi-language Pack
The above talked about how to initialize multi language pack, the language pack is now designing multi, multi-language package is json file, as described in Chapter X, we need to design a document on LangPath following example zh.json or en.json
# zh.json
{
"zh": {
"submit": "提交",
"create": "创建"
}
}
#en.json
{
"en": {
"submit": "Submit",
"create": "Create"
}
}
3. Use the Language Pack
We can call the controller to get the response of the translation language translation, as follows:
func (this *MainController) Get() {
this.Data["create"] = beego.Translation.Translate("create")
this.TplNames = "index.tpl"
}
We can also directly call response in the template translation function:
// 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)