PHP 8.5.0 Beta 1 available for testing

Voting

: min(seven, eight)?
(Example: nine)

The Note You're Voting On

sasq1 at go2 dot pl
16 years ago
And what about pgettext and npgettext? They are there in the gettext documentation, but there aren't in PHP. They're very useful if you have the same messages for translation, but in different contexts, so they should be translated separately and probably differently.

Fortunately, there is a simple work-around, which may help:
From the gettext.h header one can find out that pgettext() is only a macro calling dcgettext() internally with properly mangled parameters - msgctxt and msgid are glued together with use of ASCII char 4 [EOT, End Of Text]. The same way they're written in .mo files, so it's possible to refer them this way.

Here's my "emulated" pgettext() function:

<?php
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;
}

}
?>

By default, xgettext doesn't support pgettext function for PHP source files. But there is a parameter which can work-around it. Here's how I call xgettext:

xgettext --force-po --keyword="pgettext:1c,2" -c -o messages.po sourceFile.php

In sourceFile.php I use the following test code:

pgettext('menu', 'Open'); //Substitute "Otwórz"
pgettext('forum', 'Open'); //Substitute "Otwarty", different context

Generated .po file fragment:

msgctxt "menu"
msgid "Open"
msgstr "Otwórz"

msgctxt "forum"
msgctxt "Open"
msgstr "Otwarty"

I've tested it out and everything works fine :-) If anyone have some further suggestions or fixes, please write ;-)

<< Back to user notes page

To Top