14.5.md 2.9 KB
Newer Older
A
astaxie 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
# 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)