Timo Huovinen Timo Huovinen - 3 months ago 23
PHP Question

gettext, how to handle homonyms?

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?

Answer

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');