note 89975 added to book.gettext
| From: | sasq1 at go2 dot pl | Date: | Tue, 31 Mar 2009 18:18:20 +0000 |
| Subject: | note 89975 added to book.gettext | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-152662@lists.php.net to get a copy of this message | ||
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 ;-)
----
Server IP: 82.177.164.100
Probable Submitter: 83.9.182.121
----
Manual Page -- http://www.php.net/manual/en/book.gettext.php
Edit -- https://master.php.net/note/edit/89975
Del: integrated -- https://master.php.net/note/delete/89975/integrated
Del: useless -- https://master.php.net/note/delete/89975/useless
Del: bad code -- https://master.php.net/note/delete/89975/bad+code
Del: spam -- https://master.php.net/note/delete/89975/spam
Del: non-english -- https://master.php.net/note/delete/89975/non-english
Del: in docs -- https://master.php.net/note/delete/89975/in+docs
Del: other reasons-- https://master.php.net/note/delete/89975
Reject -- https://master.php.net/note/reject/89975
Search -- https://master.php.net/manage/user-notes.php