Using gettext
Single value
echo gettext( \"Hello, world!\\n\" );
Plurals
printf(ngett
While trying to use the GNU xgettext utility to extract the strings from the source code I ran into some trouble with the pgettext() idea above.
At first it looks like it's going to work. Using the --keyword argument I can run the xgettext utility to extract these context and message strings from the test script:
echo pgettext('Letter','mail');
echo pgettext('Letter','character');
and get a .pot file with the expected output:
...
msgctxt "mail"
msgid "Letter"
msgstr ""
msgctxt "character"
msgid "Letter"
msgstr ""
...
But the PHP *gettext() functions don't allow me to pass the context strings - so I can't get the translated text.
Being able to use the GNU utilities makes things easier for me, so the solution for me was to use something like this:
function _c( $txt ) { return gettext( $txt ); }
echo "", _c( "mail:Letter" ), "\n";
echo "
", _c( "character:Letter" ), "\n";
Now I run the xgettext utility
xgettext ... --keyword="_c:1" ...
against my test script. This generates a .pot file with simple msgid's that can be accessed via the PHP gettext() function:
...
msgid "mail:Letter"
...
msgid "character:Letter"
...
Next I copy the .pot template to the various LC_MESSAGE folders as a .po file and edit the translated texts:
...
msgid "mail:Letter"
msgstr "Russian outputs for Mail: \"письмо\""
msgid "character:Letter"
msgstr "Russian outputs for Letter of the Alphabet: \"буква\""
...
And my test script works:
...
Russian outputs for Mail: "письмо"
Russian outputs for Letter of the Alphabet: "буква"
...
The documentation for xgettext is here: http://www.gnu.org/software/gettext/manual/html_node/xgettext-Invocation.html
(I'm still having a problem with poedit and "plural" text but that's another subject.)