Back -> I18N documentation
This app comes with out of the box support for Gettext, if it's installed on the server (it often is, but if not, sudo apt-get install gettext
should do it). By default, the folder which contains the language files is Locale
in the root of the project. This folder can be changed, but if you choose to do so, it MUST be changed in TWO locations: .env
in the root folder of the project, and app/bin/i18n/config_local.sh
. More info below.
All internationalization configuration values are defined in
.env
.
There are several helper scripts that automate the process. They must be run from the root folder of the project. Unfortunately, they're rather Ubuntu-specific right now. Anyway...
All scripts are in app/bin/i18n
.
-
config.sh
:- change default locale folder (if you do this, you MUST change it in the
.env
file in the root of the project, too) - change username for non-sudo user - some commands require root, and those that don't will be executed as this user
- change default locale folder (if you do this, you MUST change it in the
-
config_local.sh
- in gitignore, so won't exist, but can be created
- used for overriding the values of
config.sh
locally, like the non-sudo user (as this can differ from machine to machine)
-
addlang.sh
- to be executed as root:
sudo app/bin/i18n/addlang.sh
- if locale parameter is given (i.e.,
sudo app/bin/i18n/addlang.sh ja_JP
) the locale is installed onto the OS and a language folder is created in the app - if no parameter is given, script checks the subfolders in the locale folder and installs all languages it finds. Useful during deploymeny, or when setting up an app that was started by someone else - automates language installation for everything committed thus far.
- to be executed as root:
-
update-potpo.sh
- no params
- when executed, does the following in order: recompiles the twig views according to
viewsFolders
value(s) inapp/config/shared/root.php
, extracts gettext strings from these compiled files, creates a new language template file (.pot
) and updates all language.po
files with new translatable strings, if any.
-
update-mo.sh
- no params
- when done with translating, run this to get machine objects readable by gettext and finalize translated rendering
The only locale included by default is en_US
. See instructions below if you'd like to add more.
OOTB it contains just some bogus values which aren't even active. To see it in action, go to Standard/Views/home.twig
and uncomment the trans
block at the bottom. Note that for this to work, both gettext and twig/extensions need to be installed (extensions are only suggested in composer.json
, and won't be pulled in by default).
When starting a new multi-language app:
- Decide which languages it will need
- Set up
REGULAR_USER
value inapp/bin/i18n/config_local.sh
. Recommendation: use same user which executes PHP code. - Run
sudo app/bin/i18n/addlang.sh XXXX
for language you'll need, where XXXX is each of the locales. Use real locale codes (e.g.ja_JP
, etc.) - Use the
{% trans %}
block in Twig templates to mark translatable strings. More info here. - Whenever you write new Twig templates or translatable strings, re-run
app/bin/i18n/update-potpo.sh
, translate, and thenapp/bin/i18n/update-mo.sh
- Always commit all language files
When starting from an app made by someone else, as above:
- Clone and set up as usual (make a DB etc.)
- Set up
REGULAR_USER
value inapp/bin/i18n/config_local.sh
. Recommendation: use same user which executes PHP code. - Run
sudo app/bin/i18n/addlang.sh
. This will auto-generate all language folders - Get to work. Use the
{% trans %}
block in Twig templates to mark translatable strings. More info here. - Always commit all language files
The implementation of a language switcher is up to you. Take a look in app/config/config_web.php
for how the domain (the language) gets switched out, then utilize it. Use a param in the router to switch it in the session, then read from session and put into domain settings. Or use a locale as a subdomain in the URL and switch based on that. Bottom line, implementing a switcher is outside the scope of a starter app.
- for more information, please read this SitePoint post.
- for an easier way to edit
.po
language files, check out PoEdit or its alternatives.
- GUI for using helper scripts?
- GUI for editing .po files?
- More Linux-agnostic helper scripts, for more distros (not interested in Windows or OS X)