• 3

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 187


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 187
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

gettext, how to handle homonyms?

Using gettext

Single value

echo gettext( "Hello, world!\n" );


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?

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:


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

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

I took all the info from these links:

  • 3
Reply Report