View Source Internationalization
Note: This page is being updated for OSS release. Please be patient.
Sometimes antikythera gears want to show translated messages to users. For this purpose antikythera comes with Elixir's gettext package. Documentation for gettext package is available here.
Gettext itself is one of GNU projects, and used in many other languages.
Preparing translations
- To use gettext you must define
YourGear.Gettext
.- Antikythera's gear generator script automatically sets up
YourGear.Gettext
.
- Antikythera's gear generator script automatically sets up
- Translation files are located in
priv/gettext/${locale}/LC_MESSAGES/${domain}.po
.${locale}
is a locale such asen
,jp
, etc. Default locale isen
.${domain}
is the domain that can be used withdgettext
and defaults todefault
.
Workflow using Gettext mix tasks and macros
- You can also use
mix gettext.extract
andmix gettext.merge
to manage translation files. - As written in Gettext's README, the basic workflow will be as follows:
- Put
gettext/1,2
,dgettext/2,3
or other translation macro calls in your sources code. - Run
$ mix gettext.extract
. All messages will be extracted into template files (priv/gettext/*.pot
).- This is basically an Elixir-equivalent of GNU's xgettext.
- Run
$ mix gettext.merge priv/gettext/ --locale <locale>
. Template files will be merged into translation files. - Add translations in translation files.
- Put
- Notice that you need to put macro calls in your code to use these features.
- In other words, messages appearing in translation function calls (e.g.
Gettext.gettext(YourGear.Gettext, "Some message")
), cannot be extracted by thegettext.extract
task.
- In other words, messages appearing in translation function calls (e.g.
- And importantly, messages passed to macro calls must be compile-time strings.
- You must use function calls to translate messages that dynamically change on runtime.
- Consider these trade-offs before deciding whether you use this extract-and-edit workflow, or just manually manage translation files.
Setting locale for each web request
First you have to determine where to receive locale information from your users. Popular options are:
- URL path:
/some/site/ja/hello.html
- query parameter:
/some/site/hello.html?locale=ja
- URL path:
Auto-generated
YourGear.Gettext
module comes withput_locale/1
, which is a convenient wrapper aroundGettext.put_locale/2
. Invokeput_locale/1
within your controller action as follows (here in the case of query parameter):YourGear.Gettext.put_locale(conn.request.query_params["locale"] || "ja")
The specified locale is stored within the process's process dictionary and will be used in later
*gettext
calls.