gettext, how to handle homonyms?

I have made a package to provide an easy solution to the lack of pgettext in PHP.

See here.

You need to install the package with composer require datalinx/gettext-context and then you can use

echo pgettext('Mail', 'Letter'); // Echoes: письмо
echo pgettext('Character', 'Letter'); // Echoes: буква

Plural and domain override functions (npgettext, dpgettext, dnpgettext) are also implemented.


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:

if (!function_exists('_x')) {

function _x($string, $context)
{
  $contextString = "{$context}\004{$string}";
  $translation = _($contextString);
  if ($translation == $contextString)  
     return $string;
  return $translation;
}
}

Then on poedit you need to enter the following on Sources Keywords tab:

_x:1,2c
_

So when you need to use context translation you use _x function. Eg:

<?php
echo    _x('Letter', 'alphabet');
echo    _x('Letter', 'email');
echo    _('Regular translation');

I took all the info from these links:

  • Gettext to work with context (pgettext)
  • http://www.cssigniter.com/ignite/wordpress-poedit-translation-secrets/

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 method pgettext is not implemented in PHP so you might use the helper method stated in a user comment in the php documentation.

if (!function_exists('pgettext')) {

  function pgettext($context, $msgid)
  {
     $contextString = "{$context}\004{$msgid}";
     $translation = dcgettext('messages', contextString,LC_MESSAGES);
     if ($translation == $contextString)  return $msgid;
     else  return $translation;
  }

}

In your case it would be

echo pgettext('mail', 'Letter');
echo pgettext('character', 'Letter');

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 "<P>", _c( "mail:Letter" ), "\n";
echo "<P>", _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.)

Tags:

Php

Gettext