Localization
Middleware i18n provides app Internationalization and Localization for Macaron Instances.

Installation

1
go get github.com/go-macaron/i18n
Copied!

Usage

1
// main.go
2
import (
3
"github.com/go-macaron/i18n"
4
"gopkg.in/macaron.v1"
5
)
6
7
func main() {
8
m := macaron.Classic()
9
m.Use(i18n.I18n(i18n.Options{
10
Langs: []string{"en-US", "zh-CN"},
11
Names: []string{"English", "简体中文"},
12
}))
13
14
m.Get("/", func(locale i18n.Locale) string {
15
return "current language is" + locale.Lang
16
})
17
18
// Use in handler.
19
m.Get("/trans", func(ctx *macaron.Context) string {
20
return ctx.Tr("hello %s", "world")
21
})
22
23
m.Run()
24
}
Copied!
1
<!-- templates/hello.tmpl -->
2
<h2>{{.i18n.Tr "hello %s" "world"}}!</h2>
Copied!

Pongo2

To use i18n feature in pongo2 with middleware pongo2:
1
<!-- templates/hello.tmpl -->
2
<h2>{{Tr(Lang,"hello %s","world")}}!</h2>
Copied!

Options

i18n.I18n comes with a variety of configuration options(i18n.Options):
1
// ...
2
m.Use(i18n.I18n(i18n.Options{
3
// Directory to load locale files. Default is "conf/locale".
4
Directory: "conf/locale",
5
// Languages that will be supported, order is meaningful.
6
Langs: []string{"en-US", "zh-CN"},
7
// Human friendly names corresponding to Langs list.
8
Names: []string{"English", "简体中文"},
9
// Locale file naming style. Default is "locale_%s.ini".
10
Format: "locale_%s.ini",
11
// Name of language parameter name in URL. Default is "lang".
12
Parameter: "lang",
13
// Redirect when user uses get parameter to specify language. Default is false.
14
Redirect: false,
15
// Name that maps into template variable. Default is "i18n".
16
TmplName: "i18n",
17
}))
18
// ...
Copied!

Loading Locale Files

By default, locale files should be put in conf/locale:
1
conf/
2
|
3
|__ locale/
4
|
5
|__ locale_en-US.ini
6
|
7
|__ locale_zh-CN.ini
Copied!

Others

Last modified 2yr ago