Implement plural and context based translation functionality
refs #6982
This commit is contained in:
parent
e2b5e05f21
commit
b38ef9c0bd
|
@ -918,15 +918,21 @@ class Module
|
|||
* Translate a string with the global mt()
|
||||
*
|
||||
* @param $string
|
||||
* @param null $context
|
||||
*
|
||||
* @return mixed|string
|
||||
*/
|
||||
protected function translate($string)
|
||||
protected function translate($string, $context = null)
|
||||
{
|
||||
return mt($this->name, $string);
|
||||
return mt($this->name, $string, $context);
|
||||
}
|
||||
|
||||
protected function translatePlural($string, $string2, $n)
|
||||
/**
|
||||
* (non-PHPDoc)
|
||||
* @see Translator::translatePlural() For the function documentation.
|
||||
*/
|
||||
protected function translatePlural($textSingular, $textPlural, $number, $context = null)
|
||||
{
|
||||
return mtp($this->name, $string, $string2, $n);
|
||||
return mtp($this->name, $textSingular, $textPlural, $number, $context);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,49 +5,85 @@
|
|||
use Icinga\Util\Translator;
|
||||
|
||||
if (extension_loaded('gettext')) {
|
||||
function t($messageId)
|
||||
|
||||
/**
|
||||
* (non-PHPDoc)
|
||||
* @see Translator::translate() For the function documentation.
|
||||
*/
|
||||
function t($messageId, $context = null)
|
||||
{
|
||||
return Translator::translate($messageId, Translator::DEFAULT_DOMAIN);
|
||||
return Translator::translate($messageId, Translator::DEFAULT_DOMAIN, $context);
|
||||
}
|
||||
|
||||
function mt($domain, $messageId)
|
||||
/**
|
||||
* (non-PHPDoc)
|
||||
* @see Translator::translate() For the function documentation.
|
||||
*/
|
||||
function mt($domain, $messageId, $context = null)
|
||||
{
|
||||
return Translator::translate($messageId, $domain);
|
||||
return Translator::translate($messageId, $domain, $context);
|
||||
}
|
||||
|
||||
function tp($messageId, $messageId2, $number)
|
||||
/**
|
||||
* (non-PHPDoc)
|
||||
* @see Translator::translatePlural() For the function documentation.
|
||||
*/
|
||||
function tp($messageId, $messageId2, $number, $context = null)
|
||||
{
|
||||
return Translator::translatePlural($messageId, $messageId2, $number, Translator::DEFAULT_DOMAIN);
|
||||
return Translator::translatePlural($messageId, $messageId2, $number, Translator::DEFAULT_DOMAIN, $context);
|
||||
}
|
||||
|
||||
function mtp($domain, $messageId, $messageId2, $number)
|
||||
/**
|
||||
* (non-PHPDoc)
|
||||
* @see Translator::translatePlural() For the function documentation.
|
||||
*/
|
||||
function mtp($domain, $messageId, $messageId2, $number, $context = null)
|
||||
{
|
||||
return Translator::translatePlural($messageId, $messageId2, $number, $domain);
|
||||
return Translator::translatePlural($messageId, $messageId2, $number, $domain, $context);
|
||||
}
|
||||
|
||||
} else {
|
||||
function t($messageId)
|
||||
|
||||
/**
|
||||
* (non-PHPDoc)
|
||||
* @see Translator::translate() For the function documentation.
|
||||
*/
|
||||
function t($messageId, $context = null)
|
||||
{
|
||||
return $messageId;
|
||||
}
|
||||
|
||||
function mt($domain, $messageId)
|
||||
/**
|
||||
* (non-PHPDoc)
|
||||
* @see Translator::translate() For the function documentation.
|
||||
*/
|
||||
function mt($domain, $messageId, $context = null)
|
||||
{
|
||||
return $messageId;
|
||||
}
|
||||
|
||||
function tp($messageId, $messageId2, $number)
|
||||
/**
|
||||
* (non-PHPDoc)
|
||||
* @see Translator::translatePlural() For the function documentation.
|
||||
*/
|
||||
function tp($messageId, $messageId2, $number, $context = null)
|
||||
{
|
||||
if ($number === 0 || $number > 1 || $number < 0) {
|
||||
if ((int) $number !== 1) {
|
||||
return $messageId2;
|
||||
}
|
||||
return $messageId;
|
||||
}
|
||||
|
||||
function mt($domain, $messageId, $messageId2, $number)
|
||||
/**
|
||||
* (non-PHPDoc)
|
||||
* @see Translator::translatePlural() For the function documentation.
|
||||
*/
|
||||
function mtp($domain, $messageId, $messageId2, $number, $context = null)
|
||||
{
|
||||
if ($number === 0 || $number > 1 || $number < 0) {
|
||||
if ((int) $number !== 1) {
|
||||
return $messageId2;
|
||||
}
|
||||
return $messageId;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -34,13 +34,18 @@ class Translator
|
|||
*
|
||||
* Falls back to the default domain in case the string cannot be translated using the given domain
|
||||
*
|
||||
* @param string $text The string to translate
|
||||
* @param string $domain The primary domain to use
|
||||
* @param string $text The string to translate
|
||||
* @param string $domain The primary domain to use
|
||||
* @param string|null $context Optional parameter for context based translation
|
||||
*
|
||||
* @return string The translated string
|
||||
* @return string The translated string
|
||||
*/
|
||||
public static function translate($text, $domain)
|
||||
public static function translate($text, $domain, $context = null)
|
||||
{
|
||||
if ($context !== null) {
|
||||
return self::pgettext($text, $domain, $context);
|
||||
}
|
||||
|
||||
$res = dgettext($domain, $text);
|
||||
if ($res === $text && $domain !== self::DEFAULT_DOMAIN) {
|
||||
return dgettext(self::DEFAULT_DOMAIN, $text);
|
||||
|
@ -55,15 +60,70 @@ class Translator
|
|||
* @param string $textPlural The string in plural form to translate
|
||||
* @param integer $number The number to get the plural or singular string
|
||||
* @param string $domain The primary domain to use
|
||||
* @param string|null $context Optional parameter for context based translation
|
||||
*
|
||||
* @return string The translated string
|
||||
*/
|
||||
public static function translatePlural($textSingular, $textPlural, $number, $domain)
|
||||
public static function translatePlural($textSingular, $textPlural, $number, $domain, $context = null)
|
||||
{
|
||||
if ($context !== null) {
|
||||
return self::pngettext($textSingular, $textPlural, $number, $domain, $context);
|
||||
}
|
||||
|
||||
$res = dngettext($domain, $textSingular, $textPlural, $number);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Emulated pgettext()
|
||||
*
|
||||
* @link http://php.net/manual/de/book.gettext.php#89975
|
||||
*
|
||||
* @param $text
|
||||
* @param $domain
|
||||
* @param $context
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function pgettext($text, $domain, $context)
|
||||
{
|
||||
$contextString = "{$context}\004{$text}";
|
||||
|
||||
$translation = dcgettext($domain, $contextString, LC_MESSAGES);
|
||||
|
||||
if ($translation == $contextString) {
|
||||
return $text;
|
||||
} else {
|
||||
return $translation;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Emulated pngettext()
|
||||
*
|
||||
* @link http://php.net/manual/de/book.gettext.php#89975
|
||||
*
|
||||
* @param $textSingular
|
||||
* @param $textPlural
|
||||
* @param $number
|
||||
* @param $domain
|
||||
* @param $context
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function pngettext($textSingular, $textPlural, $number, $domain, $context)
|
||||
{
|
||||
$contextString = "{$context}\004{$textSingular}";
|
||||
|
||||
$translation = dcngettext($domain, $contextString, $textPlural, $number, LC_MESSAGES);
|
||||
|
||||
if ($translation == $contextString || $translation == $textPlural) {
|
||||
return ($number == 1 ? $textSingular : $textPlural);
|
||||
} else {
|
||||
return $translation;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new gettext domain
|
||||
*
|
||||
|
|
|
@ -216,27 +216,29 @@ class ActionController extends Zend_Controller_Action
|
|||
*
|
||||
* Autoselects the module domain, if any, and falls back to the global one if no translation could be found.
|
||||
*
|
||||
* @param string $text The string to translate
|
||||
* @param string $text The string to translate
|
||||
* @param string|null $context Optional parameter for context based translation
|
||||
*
|
||||
* @return string The translated string
|
||||
* @return string The translated string
|
||||
*/
|
||||
public function translate($text)
|
||||
public function translate($text, $context = null)
|
||||
{
|
||||
return Translator::translate($text, $this->view->translationDomain);
|
||||
return Translator::translate($text, $this->view->translationDomain, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate a plural string
|
||||
*
|
||||
* @param string $textSingular The string in singular form to translate
|
||||
* @param string $textPlural The string in plural form to translate
|
||||
* @param string $number The number to get the plural or singular string
|
||||
* @param string $textSingular The string in singular form to translate
|
||||
* @param string $textPlural The string in plural form to translate
|
||||
* @param string $number The number to get the plural or singular string
|
||||
* @param string|null $context Optional parameter for context based translation
|
||||
*
|
||||
* @return string The translated string
|
||||
* @return string The translated string
|
||||
*/
|
||||
public function translatePlural($textSingular, $textPlural, $number)
|
||||
public function translatePlural($textSingular, $textPlural, $number, $context = null)
|
||||
{
|
||||
return Translator::translatePlural($textSingular, $textPlural, $number, $this->view->translationDomain);
|
||||
return Translator::translatePlural($textSingular, $textPlural, $number, $this->view->translationDomain, $context);
|
||||
}
|
||||
|
||||
protected function ignoreXhrBody()
|
||||
|
|
|
@ -127,9 +127,9 @@ class View extends Zend_View_Abstract
|
|||
);
|
||||
}
|
||||
|
||||
public function translate($text)
|
||||
public function translate($text, $context = null)
|
||||
{
|
||||
return Translator::translate($text, $this->translationDomain);
|
||||
return Translator::translate($text, $this->translationDomain, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -137,9 +137,9 @@ class View extends Zend_View_Abstract
|
|||
*
|
||||
* @see Translator::translatePlural()
|
||||
*/
|
||||
public function translatePlural($textSingular, $textPlural, $number)
|
||||
public function translatePlural($textSingular, $textPlural, $number, $context = null)
|
||||
{
|
||||
return Translator::translatePlural($textSingular, $textPlural, $number, $this->translationDomain);
|
||||
return Translator::translatePlural($textSingular, $textPlural, $number, $this->translationDomain, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -237,9 +237,16 @@ class GettextTranslationHelper
|
|||
'/usr/bin/xgettext',
|
||||
'--language=PHP',
|
||||
'--keyword=translate',
|
||||
'--keyword=translate:1,2c',
|
||||
'--keyword=translatePlural:1,2',
|
||||
'--keyword=translatePlural:1,2,4c',
|
||||
'--keyword=mt:2',
|
||||
'--keyword=mtp:2,3',
|
||||
'--keyword=mtp:2,3,5c',
|
||||
'--keyword=t',
|
||||
'--keyword=t:1,2c',
|
||||
'--keyword=tp:1,2',
|
||||
'--keyword=tp:1,2,4c',
|
||||
'--sort-output',
|
||||
'--force-po',
|
||||
'--omit-header',
|
||||
|
|
Loading…
Reference in New Issue