Gettext for PHP
Gettext is an extension for PHP like for many other languages that supports internationalization.
The documentation of gettext
in PHP is scattered and it takes some experimenting to get it to work.
Preparations #
The system depends on installed locale
on the host system.
The command locale -a
give an account which locale have been installed.
Background might be that setting locale in a program also requires more support for formatting from the system then what is obvious.
On my system, the following locale are available:
|
|
If one needs additional languages, one needs to add that language to /etc/locale.gen
and run locale-gen
.
Application domain #
Lets say, the application is name myapp, this needs to be implemented several places in php
.
|
|
This requires the presence of a compiled message file myapp.mo
in the following location
locale/nb_NO.utf8/LC_MESSAGE
.
Compiling a message file #
To get there, one needs to process the php files first. This is done by:
|
|
This produces a file message.po
, that has the following content:
|
|
Line 8 must contain the correct locale
-id, in this case it is nb_NO.utf8 as it was found by locale -a
.
Line 11 must be changed to character set UTF-8.
This is then compiled by
|
|
Since the translation was made to Norwegian, the resulting compiled file must be stored in that location.
Caching by the PHP engine #
NOTE: the compiled message files are only loaded once.
One needs to restart php
, php-fpm
or mod_php
for it to reload the compiled message files with changed content.
|
|
will then return “hei verden”.
Further reading #
Of course, the internet has more resources: GNU .