Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gettext, how to handle homonyms?

Tags:

php

gettext

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?

like image 788
Timo Huovinen Avatar asked Apr 28 '13 07:04

Timo Huovinen


3 Answers

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');
like image 162
MatthiasLaug Avatar answered Nov 20 '22 09:11

MatthiasLaug


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.)

like image 4
Sam Azer Avatar answered Nov 20 '22 07:11

Sam Azer


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/
like image 3
chifliiiii Avatar answered Nov 20 '22 07:11

chifliiiii