i18n 簡介
i18n 通常有兩種方式篷角,一種是前端翻譯,一種是后端渲染前翻譯系任,前端翻譯的代表是Google Translate和LocalizeJS恳蹲,后端一般是由各個框架提供,在模板層面替換指定字符串至指定語言俩滥。
Gettext 簡介
Gettext是Phoenix默認(rèn)的i18n功能套件嘉蕾,功能十分強大。
我們可以在Phoenix的config.exs中定義默認(rèn)locale
config :my_app, MyApp.Gettext, default_locale: "zh"
Gettext最主要的命令有兩個:
mix gettext.extract
mix gettext.merge priv/gettext
其中 extract 命令會提取所有文件中的 gettext("msgid") 生成模板
merge 命令會根據(jù)模板生成對應(yīng)語言的翻譯列表霜旧。然后我們修改生成的 .po 文件中的 msgstr 即可错忱。
mix gettext.merge priv/gettext --locale zh
Created directory priv/gettext/zh/LC_MESSAGES
Wrote priv/gettext/zh/LC_MESSAGES/errors.po
Wrote priv/gettext/zh/LC_MESSAGES/coherence.po
en and zh
default.po
[1] http://sevenseacat.net/2015/12/20/i18n-in-phoenix-apps.html
[2] http://blog.plataformatec.com.br/2016/03/using-gettext-to-internationalize-a-phoenix-application/
[3] https://hexdocs.pm/gettext/Gettext.html
[4] http://code.parent.co/practical-i18n-with-phoenix-and-elixir/