From 8e3da74e8edf50a1d928b7bd6c86b66abb179565 Mon Sep 17 00:00:00 2001 From: neilime Date: Thu, 20 Dec 2012 16:00:12 +0100 Subject: [PATCH 1/5] Allows several translation files for same domain / locale --- src/Translator/Translator.php | 53 +++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/src/Translator/Translator.php b/src/Translator/Translator.php index 8e35c5ec..6fd745af 100644 --- a/src/Translator/Translator.php +++ b/src/Translator/Translator.php @@ -428,11 +428,10 @@ public function addTranslationFile( $this->files[$textDomain] = array(); } - $this->files[$textDomain][$locale] = array( - 'type' => $type, - 'filename' => $filename, - ); - + $this->files[$textDomain][$locale][] = array( + 'type' => $type, + 'filename' => $filename, + ); return $this; } @@ -504,6 +503,8 @@ protected function loadMessages($textDomain, $locale) return; } } + + $hasToCache = false; // Try to load from remote sources if (isset($this->remote[$textDomain])) { @@ -514,8 +515,12 @@ protected function loadMessages($textDomain, $locale) throw new Exception\RuntimeException('Specified loader is not a remote loader'); } - $this->messages[$textDomain][$locale] = $loader->load($locale, $textDomain); - goto cache; + if(isset($this->messages[$textDomain][$locale]))$this->messages[$textDomain][$locale]->exchangeArray(array_merge( + (array)$this->messages[$textDomain][$locale], + (array)$loader->load($locale, $textDomain) + )); + else $this->messages[$textDomain][$locale] = $loader->load($locale, $textDomain); + $hasToCache = true; } } @@ -531,8 +536,12 @@ protected function loadMessages($textDomain, $locale) throw new Exception\RuntimeException('Specified loader is not a file loader'); } - $this->messages[$textDomain][$locale] = $loader->load($locale, $filename); - goto cache; + if(isset($this->messages[$textDomain][$locale]))$this->messages[$textDomain][$locale]->exchangeArray(array_merge( + (array)$this->messages[$textDomain][$locale], + (array)$loader->load($locale, $filename) + )); + else $this->messages[$textDomain][$locale] = $loader->load($locale, $filename); + $hasToCache = true; } } } @@ -542,23 +551,25 @@ protected function loadMessages($textDomain, $locale) if (!isset($this->files[$textDomain][$currentLocale])) { continue; } - - $file = $this->files[$textDomain][$currentLocale]; - $loader = $this->getPluginManager()->get($file['type']); - - if (!$loader instanceof FileLoaderInterface) { - throw new Exception\RuntimeException('Specified loader is not a file loader'); + foreach($this->files[$textDomain][$currentLocale] as $file){ + $loader = $this->getPluginManager()->get($file['type']); + + if (!$loader instanceof FileLoaderInterface) { + throw new Exception\RuntimeException('Specified loader is not a file loader'); + } + + if(isset($this->messages[$textDomain][$locale]))$this->messages[$textDomain][$locale]->exchangeArray(array_merge( + (array)$this->messages[$textDomain][$locale], + (array)$loader->load($locale, $file['filename']) + )); + else $this->messages[$textDomain][$locale] = $loader->load($locale, $file['filename']); + $hasToCache = true; } - - $this->messages[$textDomain][$locale] = $loader->load($locale, $file['filename']); - unset($this->files[$textDomain][$currentLocale]); - goto cache; } // Cache the loaded text domain - cache: - if ($cache !== null) { + if ($hasToCache && $cache !== null) { $cache->setItem($cacheId, $this->messages[$textDomain][$locale]); } } From bc8d94a8c301b6010bde67890d81438d18bbe6a0 Mon Sep 17 00:00:00 2001 From: neilime Date: Fri, 21 Dec 2012 11:05:49 +0100 Subject: [PATCH 2/5] Fixed an error of indentation --- src/Translator/Translator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Translator/Translator.php b/src/Translator/Translator.php index 6fd745af..088647af 100644 --- a/src/Translator/Translator.php +++ b/src/Translator/Translator.php @@ -559,8 +559,8 @@ protected function loadMessages($textDomain, $locale) } if(isset($this->messages[$textDomain][$locale]))$this->messages[$textDomain][$locale]->exchangeArray(array_merge( - (array)$this->messages[$textDomain][$locale], - (array)$loader->load($locale, $file['filename']) + (array)$this->messages[$textDomain][$locale], + (array)$loader->load($locale, $file['filename']) )); else $this->messages[$textDomain][$locale] = $loader->load($locale, $file['filename']); $hasToCache = true; From a9ee35c2329bebda23e726e2b9c54d43b943dbb7 Mon Sep 17 00:00:00 2001 From: neilime Date: Fri, 21 Dec 2012 17:43:03 +0100 Subject: [PATCH 3/5] Adapt to coding Standards --- src/Translator/Translator.php | 40 ++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/Translator/Translator.php b/src/Translator/Translator.php index 088647af..18c54491 100644 --- a/src/Translator/Translator.php +++ b/src/Translator/Translator.php @@ -515,11 +515,14 @@ protected function loadMessages($textDomain, $locale) throw new Exception\RuntimeException('Specified loader is not a remote loader'); } - if(isset($this->messages[$textDomain][$locale]))$this->messages[$textDomain][$locale]->exchangeArray(array_merge( - (array)$this->messages[$textDomain][$locale], - (array)$loader->load($locale, $textDomain) - )); - else $this->messages[$textDomain][$locale] = $loader->load($locale, $textDomain); + if (isset($this->messages[$textDomain][$locale])) { + $this->messages[$textDomain][$locale]->exchangeArray(array_merge( + (array)$this->messages[$textDomain][$locale], + (array)$loader->load($locale, $textDomain) + )); + } else { + $this->messages[$textDomain][$locale] = $loader->load($locale, $textDomain); + } $hasToCache = true; } } @@ -536,11 +539,14 @@ protected function loadMessages($textDomain, $locale) throw new Exception\RuntimeException('Specified loader is not a file loader'); } - if(isset($this->messages[$textDomain][$locale]))$this->messages[$textDomain][$locale]->exchangeArray(array_merge( - (array)$this->messages[$textDomain][$locale], - (array)$loader->load($locale, $filename) - )); - else $this->messages[$textDomain][$locale] = $loader->load($locale, $filename); + if (isset($this->messages[$textDomain][$locale])) { + $this->messages[$textDomain][$locale]->exchangeArray(array_merge( + (array)$this->messages[$textDomain][$locale], + (array)$loader->load($locale, $filename) + )); + } else { + $this->messages[$textDomain][$locale] = $loader->load($locale, $filename); + } $hasToCache = true; } } @@ -558,11 +564,15 @@ protected function loadMessages($textDomain, $locale) throw new Exception\RuntimeException('Specified loader is not a file loader'); } - if(isset($this->messages[$textDomain][$locale]))$this->messages[$textDomain][$locale]->exchangeArray(array_merge( - (array)$this->messages[$textDomain][$locale], - (array)$loader->load($locale, $file['filename']) - )); - else $this->messages[$textDomain][$locale] = $loader->load($locale, $file['filename']); + if (isset($this->messages[$textDomain][$locale])) { + $this->messages[$textDomain][$locale]->exchangeArray(array_merge( + (array)$this->messages[$textDomain][$locale], + (array)$loader->load($locale, $file['filename']) + )); + } + else { + $this->messages[$textDomain][$locale] = $loader->load($locale, $file['filename']); + } $hasToCache = true; } unset($this->files[$textDomain][$currentLocale]); From 9b9ba59f3c0bc23b0bbd94b760b4e581bdadaf51 Mon Sep 17 00:00:00 2001 From: neilime Date: Wed, 2 Jan 2013 11:16:15 +0100 Subject: [PATCH 4/5] Fix PHP coding standard errors --- src/Translator/Translator.php | 113 ++++++++++++++++++---------------- 1 file changed, 59 insertions(+), 54 deletions(-) diff --git a/src/Translator/Translator.php b/src/Translator/Translator.php index 18c54491..95808502 100644 --- a/src/Translator/Translator.php +++ b/src/Translator/Translator.php @@ -87,7 +87,7 @@ class Translator /** * Instantiate a translator * - * @param array|Traversable $options + * @param array|Traversable $options * @return Translator * @throws Exception\InvalidArgumentException */ @@ -208,12 +208,13 @@ public static function factory($options) /** * Set the default locale. * - * @param string $locale + * @param string $locale * @return Translator */ public function setLocale($locale) { $this->locale = $locale; + return $this; } @@ -234,12 +235,13 @@ public function getLocale() /** * Set the fallback locale. * - * @param string $locale + * @param string $locale * @return Translator */ public function setFallbackLocale($locale) { $this->fallbackLocale = $locale; + return $this; } @@ -262,6 +264,7 @@ public function getFallbackLocale() public function setCache(CacheStorage $cache = null) { $this->cache = $cache; + return $this; } @@ -284,6 +287,7 @@ public function getCache() public function setPluginManager(LoaderPluginManager $pluginManager) { $this->pluginManager = $pluginManager; + return $this; } @@ -332,11 +336,11 @@ public function translate($message, $textDomain = 'default', $locale = null) /** * Translate a plural message. * - * @param string $singular - * @param string $plural - * @param int $number - * @param string $textDomain - * @param string|null $locale + * @param string $singular + * @param string $plural + * @param int $number + * @param string $textDomain + * @param string|null $locale * @return string * @throws Exception\OutOfBoundsException */ @@ -382,9 +386,9 @@ public function translatePlural( /** * Get a translated message. * - * @param string $message - * @param string $locale - * @param string $textDomain + * @param string $message + * @param string $locale + * @param string $textDomain * @return string|null */ protected function getTranslatedMessage( @@ -410,10 +414,10 @@ protected function getTranslatedMessage( /** * Add a translation file. * - * @param string $type - * @param string $filename - * @param string $textDomain - * @param string $locale + * @param string $type + * @param string $filename + * @param string $textDomain + * @param string $locale * @return Translator */ public function addTranslationFile( @@ -429,19 +433,20 @@ public function addTranslationFile( } $this->files[$textDomain][$locale][] = array( - 'type' => $type, - 'filename' => $filename, - ); + 'type' => $type, + 'filename' => $filename, + ); + return $this; } /** * Add multiple translations with a file pattern. * - * @param string $type - * @param string $baseDir - * @param string $pattern - * @param string $textDomain + * @param string $type + * @param string $baseDir + * @param string $pattern + * @param string $textDomain * @return Translator */ public function addTranslationFilePattern( @@ -466,8 +471,8 @@ public function addTranslationFilePattern( /** * Add remote translations. * - * @param string $type - * @param string $textDomain + * @param string $type + * @param string $textDomain * @return Translator */ public function addRemoteTranslations($type, $textDomain = 'default') @@ -484,8 +489,8 @@ public function addRemoteTranslations($type, $textDomain = 'default') /** * Load messages for a given language and domain. * - * @param string $textDomain - * @param string $locale + * @param string $textDomain + * @param string $locale * @throws Exception\RuntimeException * @return void */ @@ -500,10 +505,11 @@ protected function loadMessages($textDomain, $locale) if (null !== ($result = $cache->getItem($cacheId))) { $this->messages[$textDomain][$locale] = $result; + return; } } - + $hasToCache = false; // Try to load from remote sources @@ -516,12 +522,12 @@ protected function loadMessages($textDomain, $locale) } if (isset($this->messages[$textDomain][$locale])) { - $this->messages[$textDomain][$locale]->exchangeArray(array_merge( - (array)$this->messages[$textDomain][$locale], - (array)$loader->load($locale, $textDomain) - )); + $this->messages[$textDomain][$locale]->exchangeArray(array_merge( + (array) $this->messages[$textDomain][$locale], + (array) $loader->load($locale, $textDomain) + )); } else { - $this->messages[$textDomain][$locale] = $loader->load($locale, $textDomain); + $this->messages[$textDomain][$locale] = $loader->load($locale, $textDomain); } $hasToCache = true; } @@ -540,12 +546,12 @@ protected function loadMessages($textDomain, $locale) } if (isset($this->messages[$textDomain][$locale])) { - $this->messages[$textDomain][$locale]->exchangeArray(array_merge( - (array)$this->messages[$textDomain][$locale], - (array)$loader->load($locale, $filename) - )); + $this->messages[$textDomain][$locale]->exchangeArray(array_merge( + (array) $this->messages[$textDomain][$locale], + (array) $loader->load($locale, $filename) + )); } else { - $this->messages[$textDomain][$locale] = $loader->load($locale, $filename); + $this->messages[$textDomain][$locale] = $loader->load($locale, $filename); } $hasToCache = true; } @@ -557,23 +563,22 @@ protected function loadMessages($textDomain, $locale) if (!isset($this->files[$textDomain][$currentLocale])) { continue; } - foreach($this->files[$textDomain][$currentLocale] as $file){ - $loader = $this->getPluginManager()->get($file['type']); - - if (!$loader instanceof FileLoaderInterface) { - throw new Exception\RuntimeException('Specified loader is not a file loader'); - } - - if (isset($this->messages[$textDomain][$locale])) { - $this->messages[$textDomain][$locale]->exchangeArray(array_merge( - (array)$this->messages[$textDomain][$locale], - (array)$loader->load($locale, $file['filename']) - )); - } - else { - $this->messages[$textDomain][$locale] = $loader->load($locale, $file['filename']); - } - $hasToCache = true; + foreach ($this->files[$textDomain][$currentLocale] as $file) { + $loader = $this->getPluginManager()->get($file['type']); + + if (!$loader instanceof FileLoaderInterface) { + throw new Exception\RuntimeException('Specified loader is not a file loader'); + } + + if (isset($this->messages[$textDomain][$locale])) { + $this->messages[$textDomain][$locale]->exchangeArray(array_merge( + (array) $this->messages[$textDomain][$locale], + (array) $loader->load($locale, $file['filename']) + )); + } else { + $this->messages[$textDomain][$locale] = $loader->load($locale, $file['filename']); + } + $hasToCache = true; } unset($this->files[$textDomain][$currentLocale]); } From b96d89174e9cc35514e60c4110ae6db59bf2e6fc Mon Sep 17 00:00:00 2001 From: neilime Date: Thu, 3 Jan 2013 11:46:31 +0100 Subject: [PATCH 5/5] Add test for Translator --- test/Translator/TranslatorTest.php | 26 +++++++++++++++++++ .../testarray/translation-more-de_DE.php | 14 ++++++++++ .../testarray/translation-more-en_US.php | 16 ++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 test/Translator/_files/testarray/translation-more-de_DE.php create mode 100644 test/Translator/_files/testarray/translation-more-en_US.php diff --git a/test/Translator/TranslatorTest.php b/test/Translator/TranslatorTest.php index d565eb6f..a6a01f44 100644 --- a/test/Translator/TranslatorTest.php +++ b/test/Translator/TranslatorTest.php @@ -71,6 +71,32 @@ public function testFactoryCreatesTranslator() $this->assertEquals('de_DE', $translator->getLocale()); } + public function testTranslationFromSeveralTranslationFiles() + { + $translator = Translator::factory(array( + 'locale' => 'de_DE', + 'translation_file_patterns' => array( + array( + 'type' => 'phparray', + 'base_dir' => $this->testFilesDir . '/testarray', + 'pattern' => 'translation-%s.php' + ), + array( + 'type' => 'phparray', + 'base_dir' => $this->testFilesDir . '/testarray', + 'pattern' => 'translation-more-%s.php' + ) + ) + )); + + //Test translator instance + $this->assertInstanceOf('Zend\I18n\Translator\Translator', $translator); + + //Test translations + $this->assertEquals('Nachricht 1', $translator->translate('Message 1')); //translation-de_DE.php + $this->assertEquals('Nachricht 9', $translator->translate('Message 9')); //translation-more-de_DE.php + } + public function testFactoryCreatesTranslatorWithCache() { $translator = Translator::factory(array( diff --git a/test/Translator/_files/testarray/translation-more-de_DE.php b/test/Translator/_files/testarray/translation-more-de_DE.php new file mode 100644 index 00000000..b959c7ab --- /dev/null +++ b/test/Translator/_files/testarray/translation-more-de_DE.php @@ -0,0 +1,14 @@ + 'Nachricht 2', + 'Message 9' => 'Nachricht 9' +); diff --git a/test/Translator/_files/testarray/translation-more-en_US.php b/test/Translator/_files/testarray/translation-more-en_US.php new file mode 100644 index 00000000..fd0071c8 --- /dev/null +++ b/test/Translator/_files/testarray/translation-more-en_US.php @@ -0,0 +1,16 @@ + 'Message 5 (en)', + 'Message 6' => 'Message 6 (en)', + 'Message 7' => 'Message 7 (en)', + 'Message 8' => 'Message 8 (en)', +);