📎 Webclip
Internationalization using Gettext in the Phoenix framework
The post explains internationalization as adapting a web app for different languages or cultures, and localization as the work of actually implementing those language or culture changes. It contrasts Phoenix’s earlier Linguist approach with Gettext, which lets templates mark translatable text directly with gettext and defaults to returning the original string if no translation is available.
Reading notes#
- Internationalization is the process of adapting an app for different languages or cultures, and localization is the process of implementing a different language or culture in the app.
- Linguist required translation keys in views, but the post says it created too much overhead and became messy in large applications.
- Phoenix 1.1 added Gettext support, which uses
gettextin templates to mark text for translation. gettextcan be added before localization work starts, and if no other language is needed it simply returns the original string.gettext.extractgenerates or updates.pottemplate files from translatable strings in the app..potfiles store the template strings and should not be edited manually..pofiles store translations for a locale, andgettext.mergeupdates them from the.potfiles.- The post shows adding an Esperanto locale with
mix gettext.merge priv/gettext --locale eoand editing the generateddefault.pofile with Esperanto translations. - A custom plug can read a
localequery parameter, set the application locale withGettext.put_locale, and store it in the session. - The plug is added to the browser pipeline so requests can switch between locales through the URL.
- The maintenance workflow is to wrap new template text in
gettextand periodically runmix gettext.extract --mergeto keep template and translation files in sync.
