3Answers
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');
  • 8
Reply Report
      • 1
    • It would be better to use textdomain(NULL) instead of 'messages' so that this function would behave the same way as C macro equivalent.
      • 1
    • Moreover it is worth noting that one needs to give --keyword=pgettext:1c,2 to xgettext to let it know about this function existence during fetching of translation strings.

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

  • 3
Reply Report
      • 2
    • Thank you for sharing your experience, this is definitely an interesting turn of events, I did not expect gettext to cause so much trouble in php

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:

  • 3
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions