Skip to content

Commit 175ab02

Browse files
author
Oleksii Korshenko
authored
ENGCOM-1075: Removal of inline translate script block and load a require file. #14339
2 parents b18ce4a + 8433b82 commit 175ab02

File tree

3 files changed

+61
-42
lines changed

3 files changed

+61
-42
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Framework\View\Element\Template;
1010
use Magento\Translation\Model\Js\Config;
11+
use Magento\Framework\Escaper;
1112

1213
/**
1314
* @api

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

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,19 @@
99
/** @var \Magento\Translation\Block\Js $block */
1010
?>
1111
<?php if ($block->dictionaryEnabled()): ?>
12-
<script>
13-
require.config({
14-
deps: [
15-
'jquery',
16-
'mage/translate',
17-
'jquery/jquery-storageapi'
18-
],
19-
callback: function ($) {
20-
'use strict';
21-
22-
var dependencies = [],
23-
versionObj;
24-
25-
$.initNamespaceStorage('mage-translation-storage');
26-
$.initNamespaceStorage('mage-translation-file-version');
27-
versionObj = $.localStorage.get('mage-translation-file-version');
28-
29-
<?php $version = $block->getTranslationFileVersion(); ?>
30-
31-
if (versionObj.version !== '<?= /* @escapeNotVerified */ $block->escapeJsQuote($version) ?>') {
32-
dependencies.push(
33-
'text!<?= /* @noEscape */ Magento\Translation\Model\Js\Config::DICTIONARY_FILE_NAME ?>'
34-
);
3512

13+
<?php
14+
$version = $block->getTranslationFileVersion();
15+
$fileName = Magento\Translation\Model\Js\Config::DICTIONARY_FILE_NAME;
16+
?>
17+
<script type="text/x-magento-init">
18+
{
19+
"*": {
20+
"mage/translate-init": {
21+
"dictionaryFile": "text!<?= $block->escapeJs($fileName); ?>",
22+
"version": "<?= $block->escapeJs($version) ?>"
3623
}
37-
38-
require.config({
39-
deps: dependencies,
40-
callback: function (string) {
41-
if (typeof string === 'string') {
42-
$.mage.translate.add(JSON.parse(string));
43-
$.localStorage.set('mage-translation-storage', string);
44-
$.localStorage.set(
45-
'mage-translation-file-version',
46-
{
47-
version: '<?= /* @escapeNotVerified */ $block->escapeJsQuote($version) ?>'
48-
}
49-
);
50-
} else {
51-
$.mage.translate.add($.localStorage.get('mage-translation-storage'));
52-
}
53-
}
54-
});
5524
}
56-
});
25+
}
5726
</script>
5827
<?php endif; ?>

lib/web/mage/translate-init.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
define([
7+
'jquery',
8+
'mage/translate',
9+
'jquery/jquery-storageapi'
10+
], function ($) {
11+
'use strict';
12+
13+
return function (pageOptions) {
14+
var dependencies = [],
15+
versionObj;
16+
17+
$.initNamespaceStorage('mage-translation-storage');
18+
$.initNamespaceStorage('mage-translation-file-version');
19+
versionObj = $.localStorage.get('mage-translation-file-version');
20+
21+
if (versionObj.version !== pageOptions.version) {
22+
dependencies.push(
23+
pageOptions.dictionaryFile
24+
);
25+
}
26+
27+
require.config({
28+
deps: dependencies,
29+
30+
/**
31+
* @param {String} string
32+
*/
33+
callback: function (string) {
34+
if (typeof string === 'string') {
35+
$.mage.translate.add(JSON.parse(string));
36+
$.localStorage.set('mage-translation-storage', string);
37+
$.localStorage.set(
38+
'mage-translation-file-version',
39+
{
40+
version: pageOptions.version
41+
}
42+
);
43+
} else {
44+
$.mage.translate.add($.localStorage.get('mage-translation-storage'));
45+
}
46+
}
47+
});
48+
};
49+
});

0 commit comments

Comments
 (0)