From a1dab12c4d05e99390f055c7a220c12ccf49d991 Mon Sep 17 00:00:00 2001 From: Pieter Hoste Date: Sun, 30 Jul 2017 10:31:15 +0200 Subject: [PATCH 1/6] Improved calculating version hash for the js-translation.json file. --- app/code/Magento/Translation/Block/Js.php | 8 ++++++++ app/code/Magento/Translation/Model/FileManager.php | 2 +- .../Translation/view/base/templates/translate.phtml | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Translation/Block/Js.php b/app/code/Magento/Translation/Block/Js.php index 316c136666cc0..cc0f2530f19f9 100644 --- a/app/code/Magento/Translation/Block/Js.php +++ b/app/code/Magento/Translation/Block/Js.php @@ -68,4 +68,12 @@ public function getTranslationFilePath() { return $this->fileManager->getTranslationFilePath(); } + + /** + * @return string + */ + public function getTranslationFileFullPath() + { + return $this->fileManager->getTranslationFileFullPath(); + } } diff --git a/app/code/Magento/Translation/Model/FileManager.php b/app/code/Magento/Translation/Model/FileManager.php index f1dca0189f3b8..11ce8bd2fb573 100644 --- a/app/code/Magento/Translation/Model/FileManager.php +++ b/app/code/Magento/Translation/Model/FileManager.php @@ -73,7 +73,7 @@ public function getTranslationFileTimestamp() /** * @return string */ - protected function getTranslationFileFullPath() + public function getTranslationFileFullPath() { return $this->directoryList->getPath(DirectoryList::STATIC_VIEW) . \DIRECTORY_SEPARATOR . diff --git a/app/code/Magento/Translation/view/base/templates/translate.phtml b/app/code/Magento/Translation/view/base/templates/translate.phtml index 9e1021022d876..c6cd2a33f0592 100644 --- a/app/code/Magento/Translation/view/base/templates/translate.phtml +++ b/app/code/Magento/Translation/view/base/templates/translate.phtml @@ -26,7 +26,7 @@ $.initNamespaceStorage('mage-translation-file-version'); versionObj = $.localStorage.get('mage-translation-file-version'); - getTranslationFileTimestamp() . $block->getTranslationFilePath()); ?> + getTranslationFileFullPath()) . $block->getTranslationFilePath()); ?> if (versionObj.version !== '') { dependencies.push( From b3a1c7ee7a17eff7b3964bcb3beecd58743ec9ac Mon Sep 17 00:00:00 2001 From: Pieter Hoste Date: Mon, 31 Jul 2017 21:10:42 +0200 Subject: [PATCH 2/6] Refactored getting translation file version into a more desirable state, still using the modified timestamp for now. --- app/code/Magento/Translation/Block/Js.php | 4 ++-- app/code/Magento/Translation/Model/FileManager.php | 10 +++++++++- .../Translation/view/base/templates/translate.phtml | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Translation/Block/Js.php b/app/code/Magento/Translation/Block/Js.php index cc0f2530f19f9..2198ed5cf6b86 100644 --- a/app/code/Magento/Translation/Block/Js.php +++ b/app/code/Magento/Translation/Block/Js.php @@ -72,8 +72,8 @@ public function getTranslationFilePath() /** * @return string */ - public function getTranslationFileFullPath() + public function getTranslationFileVersion() { - return $this->fileManager->getTranslationFileFullPath(); + return $this->fileManager->getTranslationFileVersion(); } } diff --git a/app/code/Magento/Translation/Model/FileManager.php b/app/code/Magento/Translation/Model/FileManager.php index 11ce8bd2fb573..84d8bdacead24 100644 --- a/app/code/Magento/Translation/Model/FileManager.php +++ b/app/code/Magento/Translation/Model/FileManager.php @@ -73,7 +73,7 @@ public function getTranslationFileTimestamp() /** * @return string */ - public function getTranslationFileFullPath() + protected function getTranslationFileFullPath() { return $this->directoryList->getPath(DirectoryList::STATIC_VIEW) . \DIRECTORY_SEPARATOR . @@ -104,4 +104,12 @@ public function updateTranslationFileContent($content) } $this->driverFile->filePutContents($this->getTranslationFileFullPath(), $content); } + + /** + * @return string + */ + public function getTranslationFileVersion() + { + return sha1($this->getTranslationFileTimestamp() . $this->getTranslationFilePath()); + } } diff --git a/app/code/Magento/Translation/view/base/templates/translate.phtml b/app/code/Magento/Translation/view/base/templates/translate.phtml index c6cd2a33f0592..7007cd7fd55a9 100644 --- a/app/code/Magento/Translation/view/base/templates/translate.phtml +++ b/app/code/Magento/Translation/view/base/templates/translate.phtml @@ -26,7 +26,7 @@ $.initNamespaceStorage('mage-translation-file-version'); versionObj = $.localStorage.get('mage-translation-file-version'); - getTranslationFileFullPath()) . $block->getTranslationFilePath()); ?> + getTranslationFileVersion(); ?> if (versionObj.version !== '') { dependencies.push( From 507cd4208b18f63ff147c9c605b6c70addbb2cba Mon Sep 17 00:00:00 2001 From: Pieter Hoste Date: Mon, 31 Jul 2017 22:57:39 +0200 Subject: [PATCH 3/6] Start using the file contents for calculating the hash again instead of the modified timestamp. --- app/code/Magento/Translation/Model/FileManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Translation/Model/FileManager.php b/app/code/Magento/Translation/Model/FileManager.php index 84d8bdacead24..e25838053f0ac 100644 --- a/app/code/Magento/Translation/Model/FileManager.php +++ b/app/code/Magento/Translation/Model/FileManager.php @@ -110,6 +110,6 @@ public function updateTranslationFileContent($content) */ public function getTranslationFileVersion() { - return sha1($this->getTranslationFileTimestamp() . $this->getTranslationFilePath()); + return sha1(sha1_file($this->getTranslationFileFullPath()) . $this->getTranslationFilePath()); } } From def3cff68d8a39efb41acf409d74cef6eb15208f Mon Sep 17 00:00:00 2001 From: Pieter Hoste Date: Mon, 31 Jul 2017 23:17:20 +0200 Subject: [PATCH 4/6] Fixes integration tests. --- app/code/Magento/Translation/Model/FileManager.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Translation/Model/FileManager.php b/app/code/Magento/Translation/Model/FileManager.php index e25838053f0ac..3ca454546ccea 100644 --- a/app/code/Magento/Translation/Model/FileManager.php +++ b/app/code/Magento/Translation/Model/FileManager.php @@ -110,6 +110,13 @@ public function updateTranslationFileContent($content) */ public function getTranslationFileVersion() { - return sha1(sha1_file($this->getTranslationFileFullPath()) . $this->getTranslationFilePath()); + $translationFile = $this->getTranslationFileFullPath(); + $translationFileHash = ''; + + if ($this->driverFile->isExists($translationFile)) { + $translationFileHash = sha1_file($translationFile); + } + + return sha1($translationFileHash . $this->getTranslationFilePath()); } } From 93ca363bca02fd96dd096c135b52ec03dd4ee02e Mon Sep 17 00:00:00 2001 From: Ievgen Shakhsuvarov Date: Tue, 8 Aug 2017 16:24:21 +0300 Subject: [PATCH 5/6] magento/magento2#10378: Improved calculating version hash for the js-translation.json file --- .../Magento/Translation/Model/FileManager.php | 35 ++++++++---- .../Magento/Translation/Model/Inline/File.php | 56 +++++++++++++++++++ 2 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 app/code/Magento/Translation/Model/Inline/File.php diff --git a/app/code/Magento/Translation/Model/FileManager.php b/app/code/Magento/Translation/Model/FileManager.php index 3ca454546ccea..820284c2d8b1e 100644 --- a/app/code/Magento/Translation/Model/FileManager.php +++ b/app/code/Magento/Translation/Model/FileManager.php @@ -6,6 +6,8 @@ namespace Magento\Translation\Model; use Magento\Framework\App\Filesystem\DirectoryList; +use Magento\Framework\App\ObjectManager; +use Magento\Translation\Model\Inline\File as TranslationFile; /** * A service for handling Translation config files @@ -17,28 +19,42 @@ class FileManager */ const TRANSLATION_CONFIG_FILE_NAME = 'Magento_Translation/js/i18n-config.js'; - /** @var \Magento\Framework\View\Asset\Repository */ + /** + * @var \Magento\Framework\View\Asset\Repository + */ private $assetRepo; - /** @var \Magento\Framework\App\Filesystem\DirectoryList */ + /** + * @var \Magento\Framework\App\Filesystem\DirectoryList + */ private $directoryList; - /** @var \Magento\Framework\Filesystem\Driver\File */ + /** + * @var \Magento\Framework\Filesystem\Driver\File + */ private $driverFile; + /** + * @var TranslationFile + */ + private $translationFile; + /** * @param \Magento\Framework\View\Asset\Repository $assetRepo - * @param \Magento\Framework\App\Filesystem\DirectoryList $directoryList, - * @param \Magento\Framework\Filesystem\Driver\File $driverFile, + * @param \Magento\Framework\App\Filesystem\DirectoryList $directoryList + * @param \Magento\Framework\Filesystem\Driver\File $driverFile + * @param TranslationFile $translationFile */ public function __construct( \Magento\Framework\View\Asset\Repository $assetRepo, \Magento\Framework\App\Filesystem\DirectoryList $directoryList, - \Magento\Framework\Filesystem\Driver\File $driverFile + \Magento\Framework\Filesystem\Driver\File $driverFile, + \Magento\Translation\Model\Inline\File $translationFile = null ) { $this->assetRepo = $assetRepo; $this->directoryList = $directoryList; $this->driverFile = $driverFile; + $this->translationFile = $translationFile ?: ObjectManager::getInstance()->get(TranslationFile::class); } /** @@ -111,12 +127,11 @@ public function updateTranslationFileContent($content) public function getTranslationFileVersion() { $translationFile = $this->getTranslationFileFullPath(); - $translationFileHash = ''; - - if ($this->driverFile->isExists($translationFile)) { - $translationFileHash = sha1_file($translationFile); + if (!$this->driverFile->isExists($translationFile)) { + $this->updateTranslationFileContent($this->translationFile->getTranslationFileContent()); } + $translationFileHash = sha1_file($translationFile); return sha1($translationFileHash . $this->getTranslationFilePath()); } } diff --git a/app/code/Magento/Translation/Model/Inline/File.php b/app/code/Magento/Translation/Model/Inline/File.php new file mode 100644 index 0000000000000..c859bfa34658d --- /dev/null +++ b/app/code/Magento/Translation/Model/Inline/File.php @@ -0,0 +1,56 @@ +translateResource = $translateResource; + $this->localeResolver = $localeResolver; + $this->jsonSerializer = $jsonSerializer; + } + + /** + * Generate translation file content for the current locale. + * + * @return string + */ + public function getTranslationFileContent() + { + $translations = $this->translateResource->getTranslationArray(null, $this->localeResolver->getLocale()); + $translations = $this->jsonSerializer->serialize($translations); + return $translations; + } +} From cbca08573439fefcc4764de2d0b2cfa306830ce3 Mon Sep 17 00:00:00 2001 From: Ievgen Shakhsuvarov Date: Thu, 17 Aug 2017 17:01:38 +0300 Subject: [PATCH 6/6] magento/magento2#10378: Improved calculating version hash for the js-translation.json file --- app/code/Magento/Translation/Block/Js.php | 2 + .../Magento/Translation/Model/FileManager.php | 2 + .../Magento/Translation/Model/Inline/File.php | 22 ++++--- .../Translation/Test/Unit/Block/JsTest.php | 10 ++++ .../Test/Unit/Model/Inline/FileTest.php | 60 +++++++++++++++++++ 5 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 app/code/Magento/Translation/Test/Unit/Model/Inline/FileTest.php diff --git a/app/code/Magento/Translation/Block/Js.php b/app/code/Magento/Translation/Block/Js.php index 2198ed5cf6b86..d4d627d19393a 100644 --- a/app/code/Magento/Translation/Block/Js.php +++ b/app/code/Magento/Translation/Block/Js.php @@ -70,6 +70,8 @@ public function getTranslationFilePath() } /** + * Gets current version of the translation file. + * * @return string */ public function getTranslationFileVersion() diff --git a/app/code/Magento/Translation/Model/FileManager.php b/app/code/Magento/Translation/Model/FileManager.php index 820284c2d8b1e..b4a541f2579c0 100644 --- a/app/code/Magento/Translation/Model/FileManager.php +++ b/app/code/Magento/Translation/Model/FileManager.php @@ -122,6 +122,8 @@ public function updateTranslationFileContent($content) } /** + * Calculate translation file version hash. + * * @return string */ public function getTranslationFileVersion() diff --git a/app/code/Magento/Translation/Model/Inline/File.php b/app/code/Magento/Translation/Model/Inline/File.php index c859bfa34658d..678ceeb1aeec2 100644 --- a/app/code/Magento/Translation/Model/Inline/File.php +++ b/app/code/Magento/Translation/Model/Inline/File.php @@ -5,37 +5,41 @@ */ namespace Magento\Translation\Model\Inline; +use Magento\Framework\Translate\ResourceInterface; +use Magento\Framework\Locale\ResolverInterface; +use Magento\Framework\Serialize\Serializer\Json; + /** * Prepares content of inline translations file. */ class File { /** - * @var \Magento\Framework\Translate\ResourceInterface + * @var ResourceInterface */ private $translateResource; /** - * @var \Magento\Framework\Locale\ResolverInterface + * @var ResolverInterface */ private $localeResolver; /** - * @var \Magento\Framework\Serialize\Serializer\Json + * @var Json */ private $jsonSerializer; /** * Initialize dependencies * - * @param \Magento\Framework\Translate\ResourceInterface $translateResource - * @param \Magento\Framework\Locale\ResolverInterface $localeResolver - * @param \Magento\Framework\Serialize\Serializer\Json $jsonSerializer + * @param ResourceInterface $translateResource + * @param ResolverInterface $localeResolver + * @param Json $jsonSerializer */ public function __construct( - \Magento\Framework\Translate\ResourceInterface $translateResource, - \Magento\Framework\Locale\ResolverInterface $localeResolver, - \Magento\Framework\Serialize\Serializer\Json $jsonSerializer + ResourceInterface $translateResource, + ResolverInterface $localeResolver, + Json $jsonSerializer ) { $this->translateResource = $translateResource; $this->localeResolver = $localeResolver; diff --git a/app/code/Magento/Translation/Test/Unit/Block/JsTest.php b/app/code/Magento/Translation/Test/Unit/Block/JsTest.php index aa6587c75b6a4..5dc0e6fe5dcaa 100644 --- a/app/code/Magento/Translation/Test/Unit/Block/JsTest.php +++ b/app/code/Magento/Translation/Test/Unit/Block/JsTest.php @@ -65,4 +65,14 @@ public function testGetTranslationFilePath() ->willReturn('frontend/Magento/luma/en_EN'); $this->assertEquals('frontend/Magento/luma/en_EN', $this->model->getTranslationFilePath()); } + + public function testGetTranslationFileVersion() + { + $version = sha1('translationFile'); + + $this->fileManagerMock->expects($this->once()) + ->method('getTranslationFileVersion') + ->willReturn($version); + $this->assertEquals($version, $this->model->getTranslationFileVersion()); + } } diff --git a/app/code/Magento/Translation/Test/Unit/Model/Inline/FileTest.php b/app/code/Magento/Translation/Test/Unit/Model/Inline/FileTest.php new file mode 100644 index 0000000000000..5af8fbdb01f39 --- /dev/null +++ b/app/code/Magento/Translation/Test/Unit/Model/Inline/FileTest.php @@ -0,0 +1,60 @@ +translateResourceMock = $this->getMockBuilder(\Magento\Framework\Translate\ResourceInterface::class) + ->disableOriginalConstructor() + ->getMock(); + $this->localeResolverMock = $this->getMockBuilder(\Magento\Framework\Locale\ResolverInterface::class) + ->disableOriginalConstructor() + ->getMock(); + $this->jsonSerializer = new \Magento\Framework\Serialize\Serializer\Json(); + + $this->model = new \Magento\Translation\Model\Inline\File( + $this->translateResourceMock, + $this->localeResolverMock, + $this->jsonSerializer + ); + } + + public function testGetTranslationFileContent() + { + $translations = ['string' => 'translatedString']; + + $this->localeResolverMock->expects($this->atLeastOnce())->method('getLocale')->willReturn('en_US'); + $this->translateResourceMock->expects($this->atLeastOnce())->method('getTranslationArray') + ->willReturn($translations); + + $this->assertEquals( + $this->jsonSerializer->serialize($translations), + $this->model->getTranslationFileContent() + ); + } +}