From 583fd46f15d45dd5e0ef944bc05456cc04cc7770 Mon Sep 17 00:00:00 2001 From: Alexander Fuhr Date: Thu, 11 Sep 2014 18:04:10 +0200 Subject: [PATCH 1/5] Experimental Plural Translation Implementation Personal BACKUP --- library/Icinga/Application/Modules/Module.php | 5 +++++ library/Icinga/Application/functions.php | 4 ++++ library/Icinga/Util/Translator.php | 6 ++++++ library/Icinga/Web/Controller/ActionController.php | 14 ++++++++++++++ library/Icinga/Web/View.php | 5 +++++ .../Translation/Util/GettextTranslationHelper.php | 1 + 6 files changed, 35 insertions(+) diff --git a/library/Icinga/Application/Modules/Module.php b/library/Icinga/Application/Modules/Module.php index 6d66412e6..6cbb49a9b 100644 --- a/library/Icinga/Application/Modules/Module.php +++ b/library/Icinga/Application/Modules/Module.php @@ -924,4 +924,9 @@ class Module { return mt($this->name, $string); } + + protected function translatePlural($string, $string2, $n) + { + return mtp($this->name, $string, $string2, $n); + } } diff --git a/library/Icinga/Application/functions.php b/library/Icinga/Application/functions.php index 422dfeab7..42f35747f 100644 --- a/library/Icinga/Application/functions.php +++ b/library/Icinga/Application/functions.php @@ -14,6 +14,10 @@ if (extension_loaded('gettext')) { { return Translator::translate($messageId, $domain); } + function mtp($domain, $messageId, $messageId2, $n) + { + return Translator::translatePlural($messageId, $messageId2, $n, $domain); + } } else { function t($messageId) { diff --git a/library/Icinga/Util/Translator.php b/library/Icinga/Util/Translator.php index b6cf533d9..df246f580 100644 --- a/library/Icinga/Util/Translator.php +++ b/library/Icinga/Util/Translator.php @@ -48,6 +48,12 @@ class Translator return $res; } + public static function translatePlural($msgid1, $msgid2, $n, $domain) + { + $res = dngettext($domain, $msgid1, $msgid2, $n); + return $res; + } + /** * Register a new gettext domain * diff --git a/library/Icinga/Web/Controller/ActionController.php b/library/Icinga/Web/Controller/ActionController.php index aead4043a..7b0a267ee 100644 --- a/library/Icinga/Web/Controller/ActionController.php +++ b/library/Icinga/Web/Controller/ActionController.php @@ -225,6 +225,20 @@ class ActionController extends Zend_Controller_Action return Translator::translate($text, $this->view->translationDomain); } + /** + * Translate a plural string + * + * @param $msgid1 + * @param $msgid2 + * @param $n + * + * @return string + */ + public function translatePlural($msgid1, $msgid2, $n) + { + return Translator::translatePlural($msgid1, $msgid2, $n, $this->view->translationDomain); + } + protected function ignoreXhrBody() { if ($this->isXhr()) { diff --git a/library/Icinga/Web/View.php b/library/Icinga/Web/View.php index 22e8e6fbd..467ab9671 100644 --- a/library/Icinga/Web/View.php +++ b/library/Icinga/Web/View.php @@ -132,6 +132,11 @@ class View extends Zend_View_Abstract return Translator::translate($text, $this->translationDomain); } + public function translatePlural($text, $text2, $n) + { + return Translator::translatePlural($text, $text2, $n, $this->translationDomain); + } + /** * Load helpers */ diff --git a/modules/translation/library/Translation/Util/GettextTranslationHelper.php b/modules/translation/library/Translation/Util/GettextTranslationHelper.php index 9d72b9c77..82560aeb4 100644 --- a/modules/translation/library/Translation/Util/GettextTranslationHelper.php +++ b/modules/translation/library/Translation/Util/GettextTranslationHelper.php @@ -237,6 +237,7 @@ class GettextTranslationHelper '/usr/bin/xgettext', '--language=PHP', '--keyword=translate', + '--keyword=translatePlural:1,2', '--keyword=mt:2', '--keyword=t', '--sort-output', From e2b5e05f212eafc18a86d898615c2f7cdd376c03 Mon Sep 17 00:00:00 2001 From: Alexander Fuhr Date: Mon, 15 Sep 2014 14:11:42 +0200 Subject: [PATCH 2/5] Implement plural translation (testing) --- library/Icinga/Application/functions.php | 26 +++++++++++++++++-- library/Icinga/Util/Translator.php | 14 ++++++++-- .../Web/Controller/ActionController.php | 12 ++++----- library/Icinga/Web/View.php | 9 +++++-- .../Util/GettextTranslationHelper.php | 1 + 5 files changed, 50 insertions(+), 12 deletions(-) diff --git a/library/Icinga/Application/functions.php b/library/Icinga/Application/functions.php index 42f35747f..94d033c01 100644 --- a/library/Icinga/Application/functions.php +++ b/library/Icinga/Application/functions.php @@ -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; + } } diff --git a/library/Icinga/Util/Translator.php b/library/Icinga/Util/Translator.php index df246f580..8e18f0720 100644 --- a/library/Icinga/Util/Translator.php +++ b/library/Icinga/Util/Translator.php @@ -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; } diff --git a/library/Icinga/Web/Controller/ActionController.php b/library/Icinga/Web/Controller/ActionController.php index 7b0a267ee..7379d0f2a 100644 --- a/library/Icinga/Web/Controller/ActionController.php +++ b/library/Icinga/Web/Controller/ActionController.php @@ -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() diff --git a/library/Icinga/Web/View.php b/library/Icinga/Web/View.php index 467ab9671..f294391a6 100644 --- a/library/Icinga/Web/View.php +++ b/library/Icinga/Web/View.php @@ -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); } /** diff --git a/modules/translation/library/Translation/Util/GettextTranslationHelper.php b/modules/translation/library/Translation/Util/GettextTranslationHelper.php index 82560aeb4..c66f1d40a 100644 --- a/modules/translation/library/Translation/Util/GettextTranslationHelper.php +++ b/modules/translation/library/Translation/Util/GettextTranslationHelper.php @@ -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, '#: ')) From b38ef9c0bd3ef735af0352f30fd390f6c065dc01 Mon Sep 17 00:00:00 2001 From: Alexander Fuhr Date: Tue, 16 Sep 2014 15:19:23 +0200 Subject: [PATCH 3/5] Implement plural and context based translation functionality refs #6982 --- library/Icinga/Application/Modules/Module.php | 14 ++-- library/Icinga/Application/functions.php | 64 +++++++++++++---- library/Icinga/Util/Translator.php | 70 +++++++++++++++++-- .../Web/Controller/ActionController.php | 22 +++--- library/Icinga/Web/View.php | 8 +-- .../Util/GettextTranslationHelper.php | 7 ++ 6 files changed, 148 insertions(+), 37 deletions(-) diff --git a/library/Icinga/Application/Modules/Module.php b/library/Icinga/Application/Modules/Module.php index 6cbb49a9b..b1b051a6c 100644 --- a/library/Icinga/Application/Modules/Module.php +++ b/library/Icinga/Application/Modules/Module.php @@ -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); } } diff --git a/library/Icinga/Application/functions.php b/library/Icinga/Application/functions.php index 94d033c01..304c75093 100644 --- a/library/Icinga/Application/functions.php +++ b/library/Icinga/Application/functions.php @@ -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; } + } diff --git a/library/Icinga/Util/Translator.php b/library/Icinga/Util/Translator.php index 8e18f0720..ccbdcbfcd 100644 --- a/library/Icinga/Util/Translator.php +++ b/library/Icinga/Util/Translator.php @@ -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 * diff --git a/library/Icinga/Web/Controller/ActionController.php b/library/Icinga/Web/Controller/ActionController.php index 7379d0f2a..e04824ee3 100644 --- a/library/Icinga/Web/Controller/ActionController.php +++ b/library/Icinga/Web/Controller/ActionController.php @@ -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() diff --git a/library/Icinga/Web/View.php b/library/Icinga/Web/View.php index f294391a6..7a954934a 100644 --- a/library/Icinga/Web/View.php +++ b/library/Icinga/Web/View.php @@ -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); } /** diff --git a/modules/translation/library/Translation/Util/GettextTranslationHelper.php b/modules/translation/library/Translation/Util/GettextTranslationHelper.php index c66f1d40a..5702def85 100644 --- a/modules/translation/library/Translation/Util/GettextTranslationHelper.php +++ b/modules/translation/library/Translation/Util/GettextTranslationHelper.php @@ -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', From 0be8b97e88e0a79f3013576867dc997a52e1f6be Mon Sep 17 00:00:00 2001 From: Alexander Fuhr Date: Tue, 16 Sep 2014 16:14:07 +0200 Subject: [PATCH 4/5] Extend TranslatorTest with new tests refs #6982 --- .../library/Icinga/Util/TranslatorTest.php | 72 ++++++++++++++++++ .../locale/de_DE/LC_MESSAGES/icingatest.mo | Bin 111 -> 997 bytes .../locale/de_DE/LC_MESSAGES/icingatest.po | 42 +++++++++- 3 files changed, 113 insertions(+), 1 deletion(-) diff --git a/test/php/library/Icinga/Util/TranslatorTest.php b/test/php/library/Icinga/Util/TranslatorTest.php index 9e341d545..4d74cc8dc 100644 --- a/test/php/library/Icinga/Util/TranslatorTest.php +++ b/test/php/library/Icinga/Util/TranslatorTest.php @@ -213,4 +213,76 @@ class TranslatorTest extends BaseTestCase 'Translator::getPreferredLocaleCode does not return the default locale if no match could be found' ); } + + /** + * @depends testWhetherSetupLocaleSetsUpTheGivenLocale + */ + public function testWhetherTranslatePluralReturnsTheSingularForm() + { + Translator::setupLocale('de_DE'); + + $result = Translator::translatePlural('test service', 'test services', 1, 'icingatest'); + + $expected = 'test dienst'; + + $this->assertEquals( + $expected, + $result, + 'Translator::translatePlural() could not return the translated singular form' + ); + } + + /** + * @depends testWhetherSetupLocaleSetsUpTheGivenLocale + */ + public function testWhetherTranslatePluralReturnsThePluralForm() + { + Translator::setupLocale('de_DE'); + + $result = Translator::translatePlural('test service', 'test services', 2, 'icingatest'); + + $expected = 'test dienste'; + + $this->assertEquals( + $expected, + $result, + 'Translator::translatePlural() could not return the translated plural form' + ); + } + + /** + * @depends testWhetherSetupLocaleSetsUpTheGivenLocale + */ + public function testWhetherTranslateReturnsTheContextForm() + { + Translator::setupLocale('de_DE'); + + $result = Translator::translate('context service', 'icingatest', 'test2'); + + $expected = 'context dienst test2'; + + $this->assertEquals( + $expected, + $result, + 'Translator::translate() could not return the translated context form' + ); + } + + /** + * @depends testWhetherSetupLocaleSetsUpTheGivenLocale + */ + public function testWhetherTranslatePluralReturnsTheContextForm() + { + Translator::setupLocale('de_DE'); + + $result = Translator::translatePlural('context service', 'context services', 3, 'icingatest', 'test-context'); + + $expected = 'context plural dienste'; + + $this->assertEquals( + $expected, + $result, + 'Translator::translatePlural() could not return the translated context form' + ); + } } diff --git a/test/php/res/locale/de_DE/LC_MESSAGES/icingatest.mo b/test/php/res/locale/de_DE/LC_MESSAGES/icingatest.mo index 487e490237f1702eefb05f227d70d93ed0d484ca..621eab2bf8652f46a1c76a642e0c32d22fbc6144 100644 GIT binary patch literal 997 zcma)4&2AGh5MCgFwA}cS5OA2@S_Ln=OIxLzrb=j&C{hAZwFM*&$laZ$MjJb_y=nUn zTzLSVfeX9^2hQAi3ug1%(pF-mPoHP}&3J6j@B5`M4%uD8GeS(bNBB%A>^tEJ;RoR% z;n&RnL(B-J8;;`i#6A6o+VG zw`p~gQ1o_(P$Dd>t27Qlj7&a+RAowo5f=Cmtz*&HQlg~AQws+5SR^<6Ula${MnxilHKN=MMh@RZ9GHMH_$4cmO~dq?EgCE62yX1o-4C|3@0 zKA;{_{J7U#_$)>~jG)_i-9Bivo3P#O)-zG`zvK2gz2@>Y0?&7Mi$9dq#My`{72o+{ zlyM=ekQ{Su(AEy(7OO0EMYBh!SyLt|rPqw0awx3Z&vMN()>3+CB9NnrYieN?rpwx@ zgq0cuYgP9>YokOxy>MS)N<$TR8{WF>Tt9Iu&U{Kzfs#J1n84}8O#|oGxDZbdhnZwv T;zINqaej3TT*l8e5;}hYw_YQW literal 111 zcmca7#4?ou2pEA_28dOFm>Gz5fLIEMEr3`Wh=YL`1VHLQz$d>bHCG|Cptv+wAtgU2 Tzeu4tvqT{=H?;(hlp+HFK^+!k diff --git a/test/php/res/locale/de_DE/LC_MESSAGES/icingatest.po b/test/php/res/locale/de_DE/LC_MESSAGES/icingatest.po index 2bf77ccae..3215ac4f7 100644 --- a/test/php/res/locale/de_DE/LC_MESSAGES/icingatest.po +++ b/test/php/res/locale/de_DE/LC_MESSAGES/icingatest.po @@ -1,2 +1,42 @@ +msgid "" +msgstr "" +"Project-Id-Version: Icinga Web 2 Test (0.0.1)\n" +"Report-Msgid-Bugs-To: dev@icinga.org\n" +"POT-Creation-Date: 2014-09-16 13:29+0200\n" +"PO-Revision-Date: 2014-09-16 16:08+0100\n" +"Last-Translator: Alexander Fuhr \n" +"Language: de_DE\n" +"Language-Team: LANGUAGE \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"X-Generator: Poedit 1.5.4\n" + msgid "Lorem ipsum dolor sit amet" -msgstr "Lorem ipsum dolor sit amet!" \ No newline at end of file +msgstr "Lorem ipsum dolor sit amet!" + +msgid "test service" +msgid_plural "test services" +msgstr[0] "test dienst" +msgstr[1] "test dienste" + +msgctxt "test" +msgid "context service" +msgstr "context dienst test" + +msgctxt "test2" +msgid "context service" +msgstr "context dienst test2" + +msgctxt "test-contextu" +msgid "context service" +msgid_plural "context services" +msgstr[0] "context plural dienstu" +msgstr[1] "context plural diensteu" + +msgctxt "test-context" +msgid "context service" +msgid_plural "context services" +msgstr[0] "context plural dienst" +msgstr[1] "context plural dienste" From b8d242984561bd5843e83cd402bc694a477f5827 Mon Sep 17 00:00:00 2001 From: Alexander Fuhr Date: Tue, 16 Sep 2014 16:26:09 +0200 Subject: [PATCH 5/5] Update module/translation documentation refs #6982 --- modules/translation/doc/translation.md | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/modules/translation/doc/translation.md b/modules/translation/doc/translation.md index 74cd54004..55dc415ad 100644 --- a/modules/translation/doc/translation.md +++ b/modules/translation/doc/translation.md @@ -42,6 +42,40 @@ The same works also for views: If you need to provide placeholders in your messages, you should wrap the `$this->translate()` with `sprintf()` for e.g. sprintf($this->translate('Hello User: (%s)'), $user->getName()) +## Translating plural forms + +To provide a plural translation, just use the `translatePlural()` function. + +```php +view->message = $this->translatePlural('Service', 'Services', 3); + } +} +``` + +## Context based translation + +If you want to provide context based translations, you can easily do it with an extra parameter in both methods +`translate()` and `translatePlural()`. + +```php +view->title = $this->translate('My Titile', 'mycontext'); + $this->view->message = $this->translatePlural('Service', 'Services', 3, 'mycontext'); + } +} +``` + # Translation for Translators Icinga Web 2 internally uses the UNIX standard gettext tool to perform internationalization, this means translation