Implement plural translation (testing)

This commit is contained in:
Alexander Fuhr 2014-09-15 14:11:42 +02:00
parent f48094f01a
commit e2b5e05f21
5 changed files with 50 additions and 12 deletions

View File

@ -14,9 +14,15 @@ if (extension_loaded('gettext')) {
{
return Translator::translate($messageId, $domain);
}
function mtp($domain, $messageId, $messageId2, $n)
function tp($messageId, $messageId2, $number)
{
return Translator::translatePlural($messageId, $messageId2, $n, $domain);
return Translator::translatePlural($messageId, $messageId2, $number, Translator::DEFAULT_DOMAIN);
}
function mtp($domain, $messageId, $messageId2, $number)
{
return Translator::translatePlural($messageId, $messageId2, $number, $domain);
}
} else {
function t($messageId)
@ -28,4 +34,20 @@ if (extension_loaded('gettext')) {
{
return $messageId;
}
function tp($messageId, $messageId2, $number)
{
if ($number === 0 || $number > 1 || $number < 0) {
return $messageId2;
}
return $messageId;
}
function mt($domain, $messageId, $messageId2, $number)
{
if ($number === 0 || $number > 1 || $number < 0) {
return $messageId2;
}
return $messageId;
}
}

View File

@ -48,9 +48,19 @@ class Translator
return $res;
}
public static function translatePlural($msgid1, $msgid2, $n, $domain)
/**
* 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 integer $number The number to get the plural or singular string
* @param string $domain The primary domain to use
*
* @return string The translated string
*/
public static function translatePlural($textSingular, $textPlural, $number, $domain)
{
$res = dngettext($domain, $msgid1, $msgid2, $n);
$res = dngettext($domain, $textSingular, $textPlural, $number);
return $res;
}

View File

@ -228,15 +228,15 @@ class ActionController extends Zend_Controller_Action
/**
* Translate a plural string
*
* @param $msgid1
* @param $msgid2
* @param $n
* @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
*
* @return string
* @return string The translated string
*/
public function translatePlural($msgid1, $msgid2, $n)
public function translatePlural($textSingular, $textPlural, $number)
{
return Translator::translatePlural($msgid1, $msgid2, $n, $this->view->translationDomain);
return Translator::translatePlural($textSingular, $textPlural, $number, $this->view->translationDomain);
}
protected function ignoreXhrBody()

View File

@ -132,9 +132,14 @@ class View extends Zend_View_Abstract
return Translator::translate($text, $this->translationDomain);
}
public function translatePlural($text, $text2, $n)
/**
* Translate a plural string
*
* @see Translator::translatePlural()
*/
public function translatePlural($textSingular, $textPlural, $number)
{
return Translator::translatePlural($text, $text2, $n, $this->translationDomain);
return Translator::translatePlural($textSingular, $textPlural, $number, $this->translationDomain);
}
/**

View File

@ -333,6 +333,7 @@ class GettextTranslationHelper
'"MIME-Version: 1.0\n"',
'"Content-Type: text/plain; charset=' . $headerInfo['charset'] . '\n"',
'"Content-Transfer-Encoding: 8bit\n"',
'"Plural-Forms: nplurals=2; plural=(n != 1);\n"',
''
)
) . PHP_EOL . substr($content, strpos($content, '#: '))