]> source.dussan.org Git - nextcloud-server.git/commitdiff
Correctly fallback to english, if the plural case is not translated
authorJoas Schilling <nickvergessen@gmx.de>
Thu, 11 Dec 2014 10:33:40 +0000 (11:33 +0100)
committerJoas Schilling <nickvergessen@gmx.de>
Thu, 11 Dec 2014 11:42:21 +0000 (12:42 +0100)
lib/private/l10n.php
lib/private/l10n/string.php
tests/data/l10n/ru.json
tests/lib/l10n.php

index 3c759a9e3a29387f83a2683f34be5725a5cc827b..6c66bee3e7975f5f2a167e225558a2fef39366b1 100644 (file)
@@ -238,7 +238,7 @@ class OC_L10N implements \OCP\IL10N {
                $this->init();
                $identifier = "_${text_singular}_::_${text_plural}_";
                if( array_key_exists($identifier, $this->translations)) {
-                       return new OC_L10N_String( $this, $identifier, $parameters, $count );
+                       return new OC_L10N_String($this, $identifier, $parameters, $count, array($text_singular, $text_plural));
                }else{
                        if($count === 1) {
                                return new OC_L10N_String($this, $text_singular, $parameters, $count);
index 04eaacab57be4f99b17e3c8481522d8092a67750..5f5452ad16d4d33b9bf7a56c76d8c721a2c8841a 100644 (file)
@@ -22,6 +22,11 @@ class OC_L10N_String{
         */
        protected $parameters;
 
+       /**
+        * @var array
+        */
+       protected $plurals;
+
        /**
         * @var integer
         */
@@ -30,11 +35,12 @@ class OC_L10N_String{
        /**
         * @param OC_L10N $l10n
         */
-       public function __construct($l10n, $text, $parameters, $count = 1) {
+       public function __construct($l10n, $text, $parameters, $count = 1, $plurals = array()) {
                $this->l10n = $l10n;
                $this->text = $text;
                $this->parameters = $parameters;
                $this->count = $count;
+               $this->plurals = $plurals;
        }
 
        public function __toString() {
@@ -45,7 +51,19 @@ class OC_L10N_String{
                        if(is_array($translations[$this->text])) {
                                $fn = $this->l10n->getPluralFormFunction();
                                $id = $fn($this->count);
-                               $text = $translations[$this->text][$id];
+
+                               if ($translations[$this->text][$id] !== '') {
+                                       // The translation of this plural case is not empty, so use it
+                                       $text = $translations[$this->text][$id];
+                               } else {
+                                       // We didn't find the plural in the language,
+                                       // so we fall back to english.
+                                       $id = ($id != 0) ? 1 : 0;
+                                       if (isset($this->plurals[$id])) {
+                                               // Fallback to the english plural
+                                               $text = $this->plurals[$id];
+                                       }
+                               }
                        }
                        else{
                                $text = $translations[$this->text];
index 177b14a6b20f2038b3c17b56af54add502802e9c..7041205e761d470624c2c8e01ca32c3da66eeb59 100644 (file)
@@ -1,6 +1,7 @@
 {
        "translations" : {
-               "_%n file_::_%n files_" : ["%n файл", "%n файла", "%n файлов"]
+               "_%n file_::_%n files_" : ["%n файл", "%n файла", "%n файлов"],
+               "_%n missing plural_::_%n missing plurals_" : ["", "", ""]
        },
        "pluralForm" :  "nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);"
 }
index 68f43b76f518348f95db2e38cec9de63065e0bab..2235581add87b5f2eae619250917fb6e652a88be 100644 (file)
@@ -42,6 +42,24 @@ class Test_L10n extends \Test\TestCase {
                */
        }
 
+       public function russianMissingPluralTranslationsData() {
+               return array(
+                       array(1, '1 missing plural'),
+                       array(2, '2 missing plurals'),
+                       array(6, '6 missing plurals'),
+               );
+       }
+
+       /**
+        * @dataProvider russianMissingPluralTranslationsData
+        */
+       public function testRussianMissingPluralTranslations($count, $expected) {
+               $l = new OC_L10N('test');
+               $l->load(OC::$SERVERROOT.'/tests/data/l10n/ru.json');
+
+               $this->assertEquals($expected, (string)$l->n('%n missing plural', '%n missing plurals', $count));
+       }
+
        public function testCzechPluralTranslations() {
                $l = new OC_L10N('test');
                $transFile = OC::$SERVERROOT.'/tests/data/l10n/cs.json';