Skip to content

Commit 0b5d877

Browse files
author
Yu Tang
committed
Merge remote-tracking branch 'origin/FearlessKiwis-MAGETWO-44290-Translations-json-file-is-not-cached-on-client-side' into FearlessKiwis-develop-no-refactoring
2 parents 924eff3 + 880d095 commit 0b5d877

File tree

8 files changed

+231
-80
lines changed

8 files changed

+231
-80
lines changed

app/code/Magento/Translation/Block/Js.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,26 @@ class Js extends Template
1616
*/
1717
protected $config;
1818

19+
/**
20+
* @var \Magento\Translation\Model\FileManager
21+
*/
22+
private $fileManager;
23+
1924
/**
2025
* @param Template\Context $context
2126
* @param Config $config
27+
* @param \Magento\Translation\Model\FileManager $fileManager
2228
* @param array $data
2329
*/
2430
public function __construct(
2531
Template\Context $context,
2632
Config $config,
33+
\Magento\Translation\Model\FileManager $fileManager,
2734
array $data = []
2835
) {
2936
parent::__construct($context, $data);
3037
$this->config = $config;
38+
$this->fileManager = $fileManager;
3139
}
3240

3341
/**
@@ -39,4 +47,23 @@ public function dictionaryEnabled()
3947
{
4048
return $this->config->dictionaryEnabled();
4149
}
50+
51+
/**
52+
* gets current js-translation.json timestamp
53+
*
54+
* @return string
55+
*/
56+
public function getTranslationFileTimestamp()
57+
{
58+
return $this->fileManager->getTranslationFileTimestamp();
59+
}
60+
61+
/**
62+
* @return string
63+
*/
64+
public function getTranslationFilePath()
65+
{
66+
return $this->fileManager->getTranslationFilePath();
67+
68+
}
4269
}

app/code/Magento/Translation/Model/FileManager.php

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Translation\Model;
77

8+
use Magento\Framework\App\Filesystem\DirectoryList;
9+
810
/**
911
* A service for handling Translation config files
1012
*/
@@ -15,18 +17,28 @@ class FileManager
1517
*/
1618
const TRANSLATION_CONFIG_FILE_NAME = 'Magento_Translation/js/i18n-config.js';
1719

18-
/**
19-
* @var \Magento\Framework\View\Asset\Repository
20-
*/
20+
/** @var \Magento\Framework\View\Asset\Repository */
2121
private $assetRepo;
2222

23+
/** @var \Magento\Framework\App\Filesystem\DirectoryList */
24+
private $directoryList;
25+
26+
/** @var \Magento\Framework\Filesystem\Driver\File */
27+
private $driverFile;
28+
2329
/**
2430
* @param \Magento\Framework\View\Asset\Repository $assetRepo
31+
* @param \Magento\Framework\App\Filesystem\DirectoryList $directoryList,
32+
* @param \Magento\Framework\Filesystem\Driver\File $driverFile,
2533
*/
2634
public function __construct(
27-
\Magento\Framework\View\Asset\Repository $assetRepo
35+
\Magento\Framework\View\Asset\Repository $assetRepo,
36+
\Magento\Framework\App\Filesystem\DirectoryList $directoryList,
37+
\Magento\Framework\Filesystem\Driver\File $driverFile
2838
) {
2939
$this->assetRepo = $assetRepo;
40+
$this->directoryList = $directoryList;
41+
$this->driverFile = $driverFile;
3042
}
3143

3244
/**
@@ -41,4 +53,40 @@ public function createTranslateConfigAsset()
4153
''
4254
);
4355
}
56+
57+
/**
58+
* gets current js-translation.json timestamp
59+
*
60+
* @return string|void
61+
*/
62+
public function getTranslationFileTimestamp()
63+
{
64+
$translationFilePath = $this->getTranslationFileFullPath();
65+
if ($this->driverFile->isExists($translationFilePath)) {
66+
$statArray = $this->driverFile->stat($translationFilePath);
67+
if (array_key_exists('mtime', $statArray)) {
68+
return $statArray['mtime'];
69+
}
70+
}
71+
}
72+
73+
/**
74+
* @return string
75+
*/
76+
protected function getTranslationFileFullPath()
77+
{
78+
return $this->directoryList->getPath(DirectoryList::STATIC_VIEW) .
79+
\DIRECTORY_SEPARATOR .
80+
$this->assetRepo->getStaticViewFileContext()->getPath() .
81+
\DIRECTORY_SEPARATOR .
82+
Js\Config::DICTIONARY_FILE_NAME;
83+
}
84+
85+
/**
86+
* @return string
87+
*/
88+
public function getTranslationFilePath()
89+
{
90+
return $this->assetRepo->getStaticViewFileContext()->getPath();
91+
}
4492
}

app/code/Magento/Translation/Test/Unit/Block/JsTest.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,27 @@ class JsTest extends \PHPUnit_Framework_TestCase
1919
*/
2020
protected $configMock;
2121

22+
/**
23+
* @var \PHPUnit_Framework_MockObject_MockObject
24+
*/
25+
protected $fileManagerMock;
26+
2227
protected function setUp()
2328
{
2429
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
2530
$this->configMock = $this->getMockBuilder('Magento\Translation\Model\Js\Config')
2631
->disableOriginalConstructor()
2732
->getMock();
28-
$this->model = $objectManager->getObject('Magento\Translation\Block\Js', ['config' => $this->configMock]);
33+
$this->fileManagerMock = $this->getMockBuilder('\Magento\Translation\Model\FileManager')
34+
->disableOriginalConstructor()
35+
->getMock();
36+
$this->model = $objectManager->getObject(
37+
'Magento\Translation\Block\Js',
38+
[
39+
'config' => $this->configMock,
40+
'fileManager' => $this->fileManagerMock
41+
]
42+
);
2943
}
3044

3145
public function testIsDictionaryStrategy()
@@ -35,4 +49,20 @@ public function testIsDictionaryStrategy()
3549
->willReturn(true);
3650
$this->assertTrue($this->model->dictionaryEnabled());
3751
}
52+
53+
public function testGetTranslationFileTimestamp()
54+
{
55+
$this->fileManagerMock->expects($this->once())
56+
->method('getTranslationFileTimestamp')
57+
->willReturn(1445736974);
58+
$this->assertEquals(1445736974, $this->model->getTranslationFileTimestamp());
59+
}
60+
61+
public function testGetTranslationFilePath()
62+
{
63+
$this->fileManagerMock->expects($this->once())
64+
->method('getTranslationFilePath')
65+
->willReturn('frontend/Magento/luma/en_EN');
66+
$this->assertEquals('frontend/Magento/luma/en_EN', $this->model->getTranslationFilePath());
67+
}
3868
}

app/code/Magento/Translation/Test/Unit/Model/FileManagerTest.php

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,31 @@ class FileManagerTest extends \PHPUnit_Framework_TestCase
2020
*/
2121
private $assetRepoMock;
2222

23+
/**
24+
* @var \Magento\Framework\App\Filesystem\DirectoryList|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
private $directoryListMock;
27+
28+
/**
29+
* @var \Magento\Framework\Filesystem\Driver\File|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
private $driverFileMock;
32+
2333
protected function setUp()
2434
{
35+
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
2536
$this->assetRepoMock = $this->getMock('\Magento\Framework\View\Asset\Repository', [], [], '', false);
26-
$this->model = new FileManager($this->assetRepoMock);
37+
$this->directoryListMock = $this->getMock('\Magento\Framework\App\Filesystem\DirectoryList', [], [], '', false);
38+
$this->driverFileMock = $this->getMock('\Magento\Framework\Filesystem\Driver\File', [], [], '', false);
39+
40+
$this->model = $objectManager->getObject(
41+
'\Magento\Translation\Model\FileManager',
42+
[
43+
'assetRepo' => $this->assetRepoMock,
44+
'directoryList' => $this->directoryListMock,
45+
'driverFile' => $this->driverFileMock,
46+
]
47+
);
2748
}
2849

2950
public function testCreateTranslateConfigAsset()
@@ -50,4 +71,52 @@ public function testCreateTranslateConfigAsset()
5071

5172
$this->assertSame($fileMock, $this->model->createTranslateConfigAsset());
5273
}
74+
75+
public function testGetTranslationFileTimestamp()
76+
{
77+
78+
$path = 'path';
79+
$contextMock = $this->getMockForAbstractClass(
80+
'\Magento\Framework\View\Asset\ContextInterface',
81+
[],
82+
'',
83+
true,
84+
true,
85+
true,
86+
['getPath']
87+
);
88+
$this->assetRepoMock->expects($this->atLeastOnce())
89+
->method('getStaticViewFileContext')
90+
->willReturn($contextMock);
91+
$contextMock->expects($this->atLeastOnce())->method('getPath')->willReturn($path);
92+
$this->directoryListMock->expects($this->atLeastOnce())->method('getPath')->willReturn($path);
93+
$this->driverFileMock->expects($this->once())
94+
->method('isExists')
95+
->with('path/path/js-translation.json')
96+
->willReturn(true);
97+
$this->driverFileMock->expects($this->once())->method('stat')->willReturn(['mtime' => 1445736974]);
98+
$this->assertEquals(1445736974, $this->model->getTranslationFileTimestamp());
99+
100+
101+
}
102+
103+
public function testGetTranslationFilePath()
104+
{
105+
$path = 'path';
106+
$contextMock = $this->getMockForAbstractClass(
107+
'\Magento\Framework\View\Asset\ContextInterface',
108+
[],
109+
'',
110+
true,
111+
true,
112+
true,
113+
['getPath']
114+
);
115+
$this->assetRepoMock->expects($this->atLeastOnce())
116+
->method('getStaticViewFileContext')
117+
->willReturn($contextMock);
118+
$contextMock->expects($this->atLeastOnce())->method('getPath')->willReturn($path);
119+
$this->assertEquals($path, $this->model->getTranslationFilePath());
120+
121+
}
53122
}

app/code/Magento/Translation/view/base/templates/translate.phtml

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,50 @@
77
?>
88
<?php /** @var $block \Magento\Translation\Block\Js */ ?>
99
<?php if ($block->dictionaryEnabled()): ?>
10-
<script type="text/x-magento-init">
11-
{
12-
"*": {
13-
"Magento_Translation/js/add-to-translate": {
14-
"config": "text!<?php /* @escapeNotVerified */ echo Magento\Translation\Model\Js\Config::DICTIONARY_FILE_NAME?>"
10+
<script>
11+
require.config({
12+
deps: [
13+
'jquery',
14+
'mage/translate',
15+
'jquery/jquery-storageapi'
16+
],
17+
callback: function ($) {
18+
'use strict';
19+
20+
var dependencies = [],
21+
versionObj;
22+
23+
$.initNamespaceStorage('mage-translation-storage');
24+
$.initNamespaceStorage('mage-translation-file-version');
25+
versionObj = $.localStorage.get('mage-translation-file-version');
26+
27+
if (versionObj.version !== '<?php /* @escapeNotVerified */
28+
echo sha1($block->getTranslationFileTimestamp() . $block->getTranslationFilePath()) ?>') {
29+
dependencies.push(
30+
'text!<?php /* @escapeNotVerified */ echo Magento\Translation\Model\Js\Config::DICTIONARY_FILE_NAME?>'
31+
);
32+
1533
}
34+
35+
require.config({
36+
deps: dependencies,
37+
callback: function (string) {
38+
if (typeof string === 'string') {
39+
$.mage.translate.add(JSON.parse(string));
40+
$.localStorage.set('mage-translation-storage', string);
41+
$.localStorage.set(
42+
'mage-translation-file-version',
43+
{
44+
version: '<?php /* @escapeNotVerified */
45+
echo sha1($block->getTranslationFileTimestamp() . $block->getTranslationFilePath()) ?>'
46+
}
47+
);
48+
} else {
49+
$.mage.translate.add($.localStorage.get('mage-translation-storage'));
50+
}
51+
}
52+
});
1653
}
17-
}
54+
});
1855
</script>
19-
<?php endif; ?>
56+
<?php endif; ?>

app/code/Magento/Translation/view/base/web/js/add-to-translate.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

app/code/Magento/Ui/view/base/web/js/lib/ko/bind/i18n.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,11 @@ define([
8282
*/
8383
setTranslateProp = function (el, original) {
8484
var location = $(el).prop('tagName').toLowerCase(),
85-
translationArray = $.mage.translate.parsedTranslate(original),
86-
translationData = translationArray.length > 3 ?
87-
{
88-
shown: translationArray[1], translated: translationArray[2], original: translationArray[3]
89-
} :
90-
{
91-
shown: original, translated: original, original: original
85+
translated = $.mage.__(original),
86+
translationData = {
87+
shown: translated,
88+
translated: translated,
89+
original: original
9290
},
9391
translateAttr = composeTranslateAttr(translationData, location);
9492

0 commit comments

Comments
 (0)