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

Jump to cache-storing instead of returning #2322

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions library/Zend/I18n/Translator/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ protected function loadMessages($textDomain, $locale)
}

$this->messages[$textDomain][$locale] = $loader->load($locale, $textDomain);
return;
goto cache;
}
}

Expand All @@ -532,12 +532,12 @@ protected function loadMessages($textDomain, $locale)
}

$this->messages[$textDomain][$locale] = $loader->load($locale, $filename);
return;
goto cache;
}
}
}

// Load concrete files, may override those loaded from patterns
// Try to load from concrete files
foreach (array($locale, '*') as $currentLocale) {
if (!isset($this->files[$textDomain][$currentLocale])) {
continue;
Expand All @@ -553,10 +553,11 @@ protected function loadMessages($textDomain, $locale)
$this->messages[$textDomain][$locale] = $loader->load($locale, $file['filename']);

unset($this->files[$textDomain][$currentLocale]);
return;
goto cache;
}

// Cache the loaded text domain
cache:
if ($cache !== null) {
$cache->setItem($cacheId, $this->messages[$textDomain][$locale]);
}
Expand Down
18 changes: 17 additions & 1 deletion tests/ZendTest/I18n/Translator/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,20 @@ public function testTranslate()
$this->assertEquals('bar', $this->translator->translate('foo'));
}

public function testTranslationsLoadedFromCache()
{
$cache = \Zend\Cache\StorageFactory::factory(array('adapter' => 'memory'));
$this->translator->setCache($cache);

$cache->addItem(
'Zend_I18n_Translator_Messages_' . md5('default' . 'en_EN'),
new TextDomain(array('foo' => 'bar'))
);

public function testTranslateWithCache()
$this->assertEquals('bar', $this->translator->translate('foo'));
}

public function testTranslationsAreStoredInCache()
{
$cache = \Zend\Cache\StorageFactory::factory(array('adapter' => 'memory'));
$this->translator->setCache($cache);
Expand All @@ -124,6 +136,10 @@ public function testTranslateWithCache()
$this->translator->addTranslationFile('test', null);

$this->assertEquals('bar', $this->translator->translate('foo'));

$item = $cache->getItem('Zend_I18n_Translator_Messages_' . md5('default' . 'en_EN'));
$this->assertInstanceOf('Zend\I18n\Translator\TextDomain', $item);
$this->assertEquals('bar', $item['foo']);
}

public function testTranslatePlurals()
Expand Down