From 2e3ddff654cff8eca627ade5c1553f7159a0d526 Mon Sep 17 00:00:00 2001 From: Michele Locati Date: Mon, 30 Jan 2017 10:26:58 +0100 Subject: [PATCH 1/8] Add getCacheId method to Translator --- src/Translator/Translator.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Translator/Translator.php b/src/Translator/Translator.php index f14b9ea0..9342e903 100644 --- a/src/Translator/Translator.php +++ b/src/Translator/Translator.php @@ -561,6 +561,18 @@ public function addRemoteTranslations($type, $textDomain = 'default') return $this; } + /** + * Get the cache identifier for the specific textDomain and locale. + * + * @param string $textDomain + * @param string $locale + * @return string + */ + protected function getCacheId($textDomain, $locale) + { + return 'Zend_I18n_Translator_Messages_' . md5($textDomain . $locale); + } + /** * Load messages for a given language and domain. * @@ -577,7 +589,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; From 7158a1f6af5b3c09c7fe1294fef3bff9d1331580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20K=C3=BChne?= Date: Thu, 26 Apr 2018 10:14:31 +0200 Subject: [PATCH 2/8] Make getCacheId() a public function and add clearCache() --- src/Translator/Translator.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Translator/Translator.php b/src/Translator/Translator.php index 9342e903..ec0cc37b 100644 --- a/src/Translator/Translator.php +++ b/src/Translator/Translator.php @@ -562,17 +562,33 @@ public function addRemoteTranslations($type, $textDomain = 'default') } /** - * Get the cache identifier for the specific textDomain and locale. + * Get the cache identifier for a specific textDomain and locale. * * @param string $textDomain * @param string $locale * @return string */ - protected function getCacheId($textDomain, $locale) + public function getCacheId($textDomain, $locale) { return 'Zend_I18n_Translator_Messages_' . md5($textDomain . $locale); } + /** + * Clears the cache for a specific textDomain and locale. + * + * @param $textDomain + * @param $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. * From 3f8b3609a292bfb918085495ca4e65dc979a3c66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20K=C3=BChne?= Date: Mon, 30 Apr 2018 07:38:55 +0200 Subject: [PATCH 3/8] Style Fixes + Unit-Tests --- src/Translator/Translator.php | 11 +++++------ test/Translator/TranslatorTest.php | 29 +++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/Translator/Translator.php b/src/Translator/Translator.php index ec0cc37b..578387af 100644 --- a/src/Translator/Translator.php +++ b/src/Translator/Translator.php @@ -564,9 +564,9 @@ public function addRemoteTranslations($type, $textDomain = 'default') /** * Get the cache identifier for a specific textDomain and locale. * - * @param string $textDomain - * @param string $locale - * @return string + * @param string $textDomain + * @param string $locale + * @return string */ public function getCacheId($textDomain, $locale) { @@ -576,9 +576,8 @@ public function getCacheId($textDomain, $locale) /** * Clears the cache for a specific textDomain and locale. * - * @param $textDomain - * @param $locale - * + * @param string $textDomain + * @param string $locale * @return bool */ public function clearCache($textDomain, $locale) diff --git a/test/Translator/TranslatorTest.php b/test/Translator/TranslatorTest.php index dd7b8fc3..555cefa8 100644 --- a/test/Translator/TranslatorTest.php +++ b/test/Translator/TranslatorTest.php @@ -185,7 +185,7 @@ public function testTranslationsLoadedFromCache() $this->translator->setCache($cache); $cache->addItem( - 'Zend_I18n_Translator_Messages_' . md5('default' . 'en_EN'), + $this->translator->getCacheId('default', 'en_EN'), new TextDomain(['foo' => 'bar']) ); @@ -207,11 +207,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 = \Zend\Cache\StorageFactory::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'); From 90247a4ab9d7c1eb742a39dc56d3144267a5a741 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 16 May 2018 11:34:28 -0500 Subject: [PATCH 4/8] Import all classes used, and use `::class` notation where possible --- test/Translator/TranslatorTest.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/Translator/TranslatorTest.php b/test/Translator/TranslatorTest.php index 555cefa8..f72f5adb 100644 --- a/test/Translator/TranslatorTest.php +++ b/test/Translator/TranslatorTest.php @@ -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; @@ -181,7 +182,7 @@ public function testTranslate() public function testTranslationsLoadedFromCache() { - $cache = \Zend\Cache\StorageFactory::factory(['adapter' => 'memory']); + $cache = CacheFactory::factory(['adapter' => 'memory']); $this->translator->setCache($cache); $cache->addItem( @@ -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(); @@ -217,7 +218,7 @@ public function testTranslationsAreClearedFromCache() $textDomain = 'default'; $locale = 'en_EN'; - $cache = \Zend\Cache\StorageFactory::factory(['adapter' => 'memory']); + $cache = CacheFactory::factory(['adapter' => 'memory']); $this->translator->setCache($cache); $cache->addItem( @@ -440,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']); } From a1fbfcac565a42e7a15b42bf755c204d9c954fc2 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 16 May 2018 11:35:50 -0500 Subject: [PATCH 5/8] Adds CHANGELOG entry for #93 --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb1af664..2eb683e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,8 @@ All notable changes to this project will be documented in this file, in reverse ### 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 From 0fca9d82478b1b3f8b096d6f2f71554dcc3824ab Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 16 May 2018 11:37:58 -0500 Subject: [PATCH 6/8] Merges 2.8.1 and 2.9.0 entries in CHANGELOG --- CHANGELOG.md | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2eb683e3..732d334c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,28 +23,6 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed -- Nothing. - -## 2.8.1 - TBD - -### Added - -- Nothing. - -### Changed - -- Nothing. - -### Deprecated - -- Nothing. - -### Removed - -- Nothing. - -### Fixed - - [#96](https://github.com/zendframework/zend-i18n/pull/96) ensures that the `TranslatorServiceFactory` injects the configured `TranslatorPluginManager`, if any, before returning it. This ensures that configured remote loaders can be utilized. From e77f6fd12c2fe500db086fb701207ff2bf63f65a Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 16 May 2018 11:38:15 -0500 Subject: [PATCH 7/8] Sets date for 2.9.0 release in CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 732d334c..f84b63d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. -## 2.9.0 - TBD +## 2.9.0 - 2018-05-16 ### Added From a6c6d3214613f0b3747386b0913387ff30c66823 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 16 May 2018 11:38:38 -0500 Subject: [PATCH 8/8] Updates branch aliases - dev-master => 2.9.x-dev - dev-develop => 2.10.x-dev --- composer.json | 4 ++-- composer.lock | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index b5ff0265..3235805c 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/composer.lock b/composer.lock index 521332eb..9bf1dead 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e78da3a3805dedf7f4a356f58c754d97", + "content-hash": "ec60cdd072a1f99defcd2077c4eda261", "packages": [ { "name": "zendframework/zend-stdlib",