From 1827256d0e2b49a77df46f90249c2ab893e0ac4f Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Wed, 18 Nov 2009 14:57:18 -0800 Subject: Added support for pgettext() and npgettext() to separate contexts for translatable messages that are going to be ambiguous in English original. --- lib/common.php | 4 ---- lib/language.php | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ scripts/update_pot.sh | 13 +++++++++++- 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/lib/common.php b/lib/common.php index 203b37c87..732c22bfd 100644 --- a/lib/common.php +++ b/lib/common.php @@ -59,10 +59,6 @@ require_once('PEAR.php'); require_once('DB/DataObject.php'); require_once('DB/DataObject/Cast.php'); # for dates -if (!function_exists('gettext')) { - require_once("php-gettext/gettext.inc"); -} - require_once(INSTALLDIR.'/lib/language.php'); // This gets included before the config file, so that admin code and plugins diff --git a/lib/language.php b/lib/language.php index 2570907b7..a99bf89e3 100644 --- a/lib/language.php +++ b/lib/language.php @@ -32,6 +32,63 @@ if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); } +if (!function_exists('gettext')) { + require_once("php-gettext/gettext.inc"); +} + +if (!function_exists('pgettext')) { + /** + * Context-aware gettext wrapper; use when messages in different contexts + * won't be distinguished from the English source but need different translations. + * The context string will appear as msgctxt in the .po files. + * + * Not currently exposed in PHP's gettext module; implemented to be compat + * with gettext.h's macros. + * + * @param string $context context identifier, should be some key like "menu|file" + * @param string $msgid English source text + * @return string original or translated message + */ + function pgettext($context, $msg) + { + $msgid = $context . "\004" . $msg; + $out = dcgettext(textdomain(NULL), $msgid, LC_MESSAGES); + if ($out == $msgid) { + return $msg; + } else { + return $out; + } + } +} + +if (!function_exists('npgettext')) { + /** + * Context-aware ngettext wrapper; use when messages in different contexts + * won't be distinguished from the English source but need different translations. + * The context string will appear as msgctxt in the .po files. + * + * Not currently exposed in PHP's gettext module; implemented to be compat + * with gettext.h's macros. + * + * @param string $context context identifier, should be some key like "menu|file" + * @param string $msg singular English source text + * @param string $plural plural English source text + * @param int $n number of items to control plural selection + * @return string original or translated message + */ + function npgettext($context, $msg, $plural, $n) + { + $msgid = $context . "\004" . $msg; + $out = dcngettext(textdomain(NULL), $msgid, $plural, $n, LC_MESSAGES); + if ($out == $msgid) { + return $msg; + } else { + return $out; + } + } +} + + /** * Content negotiation for language codes * diff --git a/scripts/update_pot.sh b/scripts/update_pot.sh index 9419e4337..8b44d43b4 100755 --- a/scripts/update_pot.sh +++ b/scripts/update_pot.sh @@ -1,3 +1,14 @@ cd `dirname $0` cd .. -xgettext --from-code=UTF-8 --default-domain=statusnet --output=locale/statusnet.po --language=PHP --join-existing actions/*.php classes/*.php lib/*.php scripts/*.php +xgettext \ + --from-code=UTF-8 \ + --default-domain=statusnet \ + --output=locale/statusnet.po \ + --language=PHP \ + --keyword="pgettext:1c,2" \ + --keyword="npgettext:1c,2,3" \ + --join-existing \ + actions/*.php \ + classes/*.php \ + lib/*.php \ + scripts/*.php -- cgit v1.2.3-54-g00ecf