Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 39 deletions.
104 changes: 65 additions & 39 deletions src/Translator/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Translator
/**
* Instantiate a translator
*
* @param array|Traversable $options
* @param array|Traversable $options
* @return Translator
* @throws Exception\InvalidArgumentException
*/
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -262,6 +264,7 @@ public function getFallbackLocale()
public function setCache(CacheStorage $cache = null)
{
$this->cache = $cache;

return $this;
}

Expand All @@ -284,6 +287,7 @@ public function getCache()
public function setPluginManager(LoaderPluginManager $pluginManager)
{
$this->pluginManager = $pluginManager;

return $this;
}

Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -428,8 +432,8 @@ public function addTranslationFile(
$this->files[$textDomain] = array();
}

$this->files[$textDomain][$locale] = array(
'type' => $type,
$this->files[$textDomain][$locale][] = array(
'type' => $type,
'filename' => $filename,
);

Expand All @@ -439,10 +443,10 @@ public function addTranslationFile(
/**
* 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(
Expand All @@ -467,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')
Expand All @@ -485,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
*/
Expand All @@ -501,10 +505,13 @@ 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
if (isset($this->remote[$textDomain])) {
foreach ($this->remote[$textDomain] as $loaderType) {
Expand All @@ -514,8 +521,15 @@ 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;
}
}

Expand All @@ -531,8 +545,15 @@ 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;
}
}
}
Expand All @@ -542,23 +563,28 @@ 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']);

$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');
}

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]);
}
}
Expand Down
26 changes: 26 additions & 0 deletions test/Translator/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 14 additions & 0 deletions test/Translator/_files/testarray/translation-more-de_DE.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_I18n
*/

return array(
'Message 2' => 'Nachricht 2',
'Message 9' => 'Nachricht 9'
);
16 changes: 16 additions & 0 deletions test/Translator/_files/testarray/translation-more-en_US.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_I18n
*/

return array(
'Message 5' => 'Message 5 (en)',
'Message 6' => 'Message 6 (en)',
'Message 7' => 'Message 7 (en)',
'Message 8' => 'Message 8 (en)',
);

0 comments on commit 9ee28ff

Please # to comment.