Using gettext
Single value
echo gettext( "Hello, world!\n" );
Plurals
printf(ngettext("%d comment", "%d comments", $n), $n);
English homonym?
echo gettext("Letter");// as in mail, for Russian outputs "письмо"
echo gettext("Letter");// as in character, for Russian outputs "буква"
Same with the english word "character", it can be the character of a person or a letter! How is gettext supposed to identify the right translation for homonyms?
For the ones using Poedit like me you need to following. First create the function. I'm using one named _x like the one WordPress use:
Then on poedit you need to enter the following on Sources Keywords tab:
So when you need to use context translation you use _x function. Eg:
I took all the info from these links:
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:
and get a .pot file with the expected output:
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:
Now I run the xgettext utility
against my test script. This generates a .pot file with simple msgid's that can be accessed via the PHP gettext() function:
Next I copy the .pot template to the various LC_MESSAGE folders as a .po file and edit the translated texts:
And my test script works:
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.)
What you are looking for is contexts for
gettext
which solves ambiguities like your example. You can find information about in the documentation. Still the needed methodpgettext
is not implemented in PHP so you might use the helper method stated in a user comment in the php documentation.In your case it would be