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

Commit

Permalink
Merging develop to master in preparation for 2.9.0 release.
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed May 16, 2018
2 parents b58a5c0 + a6c6d32 commit 6d69af5
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 11 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 2.8.1 - TBD
## 2.9.0 - 2018-05-16

### Added

- Nothing.
- [#93](https://github.com/zendframework/zend-i18n/pull/93) adds two methods to `Translator`, `getCacheId(string $textDomain, string $locale)`,
and `clearCache(string $textDomain, string $locale)`.

### Changed

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
},
"extra": {
"branch-alias": {
"dev-master": "2.8.x-dev",
"dev-develop": "2.9.x-dev"
"dev-master": "2.9.x-dev",
"dev-develop": "2.10.x-dev"
},
"zf": {
"component": "Zend\\I18n",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 28 additions & 1 deletion src/Translator/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,33 @@ public function addRemoteTranslations($type, $textDomain = 'default')
return $this;
}

/**
* Get the cache identifier for a specific textDomain and locale.
*
* @param string $textDomain
* @param string $locale
* @return string
*/
public function getCacheId($textDomain, $locale)
{
return 'Zend_I18n_Translator_Messages_' . md5($textDomain . $locale);
}

/**
* Clears the cache for a specific textDomain and locale.
*
* @param string $textDomain
* @param string $locale
* @return bool
*/
public function clearCache($textDomain, $locale)
{
if (null === ($cache = $this->getCache())) {
return false;
}
return $cache->removeItem($this->getCacheId($textDomain, $locale));
}

/**
* Load messages for a given language and domain.
*
Expand All @@ -577,7 +604,7 @@ protected function loadMessages($textDomain, $locale)
}

if (null !== ($cache = $this->getCache())) {
$cacheId = 'Zend_I18n_Translator_Messages_' . md5($textDomain . $locale);
$cacheId = $this->getCacheId($textDomain, $locale);

if (null !== ($result = $cache->getItem($cacheId))) {
$this->messages[$textDomain][$locale] = $result;
Expand Down
36 changes: 31 additions & 5 deletions test/Translator/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use PHPUnit\Framework\TestCase;
use Locale;
use Zend\Cache\StorageFactory as CacheFactory;
use Zend\EventManager\EventInterface;
use Zend\I18n\Translator\Translator;
use Zend\I18n\Translator\TextDomain;
Expand Down Expand Up @@ -181,11 +182,11 @@ public function testTranslate()

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

$cache->addItem(
'Zend_I18n_Translator_Messages_' . md5('default' . 'en_EN'),
$this->translator->getCacheId('default', 'en_EN'),
new TextDomain(['foo' => 'bar'])
);

Expand All @@ -194,7 +195,7 @@ public function testTranslationsLoadedFromCache()

public function testTranslationsAreStoredInCache()
{
$cache = \Zend\Cache\StorageFactory::factory(['adapter' => 'memory']);
$cache = CacheFactory::factory(['adapter' => 'memory']);
$this->translator->setCache($cache);

$loader = new TestLoader();
Expand All @@ -207,11 +208,36 @@ public function testTranslationsAreStoredInCache()

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

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

public function testTranslationsAreClearedFromCache()
{
$textDomain = 'default';
$locale = 'en_EN';

$cache = CacheFactory::factory(['adapter' => 'memory']);
$this->translator->setCache($cache);

$cache->addItem(
$this->translator->getCacheId($textDomain, $locale),
new TextDomain(['foo' => 'bar'])
);

$this->assertTrue($this->translator->clearCache($textDomain, $locale));

$item = $cache->getItem($this->translator->getCacheId($textDomain, $locale), $success);
$this->assertNull($item);
$this->assertFalse($success);
}

public function testClearCacheReturnsFalseIfNoCacheIsPresent()
{
$this->assertFalse($this->translator->clearCache('default', 'en_EN'));
}

public function testTranslatePlurals()
{
$this->translator->setLocale('en_EN');
Expand Down Expand Up @@ -415,7 +441,7 @@ public function testGetAllMessagesLoadedInTranslator()
);

$allMessages = $this->translator->getAllMessages();
$this->assertInstanceOf('\Zend\I18n\Translator\TextDomain', $allMessages);
$this->assertInstanceOf(TextDomain::class, $allMessages);
$this->assertEquals(7, count($allMessages));
$this->assertEquals('Message 1 (en)', $allMessages['Message 1']);
}
Expand Down

0 comments on commit 6d69af5

Please sign in to comment.