应用本地化

中间件 i18n 为 Macaron 实例 提供了国际化和本地化应用的功能。

下载安装

go get github.com/go-macaron/i18n

使用示例

// main.go
import (
    "github.com/go-macaron/i18n"
    "gopkg.in/macaron.v1"
)

func main() {
      m := macaron.Classic()
      m.Use(i18n.I18n(i18n.Options{
        Langs:    []string{"en-US", "zh-CN"},
        Names:    []string{"English", "简体中文"},
    }))

    m.Get("/", func(locale i18n.Locale) string {
        return "current language is" + locale.Lang
    })

    // 在处理器中使用
    m.Get("/trans", func(ctx *macaron.Context) string {
        return ctx.Tr("hello %s", "world")
    })

    m.Run()
}

Pongo2 模板引擎

pongo2 模板引擎中使用 i18n 中间件:

自定义选项

该服务允许接受一个参数来进行自定义选项(i18n.Options):

加载本地化文件

默认情况下,本地化文件应当存放在相对当前目录的 conf/locale 文件夹下:

其它说明

Last updated

Was this helpful?