Skip to content

Commit 5618292

Browse files
committed
MC-19247: Broken translations with advanced bundling
- Remove inline translation dictionary from html for production mode;
1 parent 3092740 commit 5618292

File tree

5 files changed

+49
-41
lines changed

5 files changed

+49
-41
lines changed

app/code/Magento/Translation/ViewModel/Dictionary.php

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,15 @@
66

77
namespace Magento\Translation\ViewModel;
88

9-
use Magento\Framework\App\Filesystem\DirectoryList;
109
use Magento\Framework\Exception\FileSystemException;
1110
use Magento\Framework\Exception\LocalizedException;
12-
use Magento\Framework\Filesystem;
1311
use Magento\Framework\View\Asset\Repository as AssetRepository;
1412
use Magento\Framework\View\Element\Block\ArgumentInterface;
1513
use Magento\Translation\Model\Js\Config as JsConfig;
1614
use Magento\Framework\App\State as AppState;
17-
use Magento\Framework\Filesystem\DriverInterface;
1815

1916
/**
20-
* View model responsible for getting translate dictionary file content for the layout.
17+
* View model responsible for handling translation dictionary in layout.
2118
*/
2219
class Dictionary implements ArgumentInterface
2320
{
@@ -31,32 +28,16 @@ class Dictionary implements ArgumentInterface
3128
*/
3229
private $appState;
3330

34-
/**
35-
* @var Filesystem
36-
*/
37-
private $filesystem;
38-
39-
/**
40-
* @var DriverInterface
41-
*/
42-
private $filesystemDriver;
43-
4431
/**
4532
* @param AssetRepository $assetRepo
4633
* @param AppState $appState
47-
* @param Filesystem $filesystem
48-
* @param DriverInterface $filesystemDriver
4934
*/
5035
public function __construct(
5136
AssetRepository $assetRepo,
52-
AppState $appState,
53-
Filesystem $filesystem,
54-
DriverInterface $filesystemDriver
37+
AppState $appState
5538
) {
5639
$this->assetRepo = $assetRepo;
5740
$this->appState = $appState;
58-
$this->filesystem = $filesystem;
59-
$this->filesystemDriver = $filesystemDriver;
6041
}
6142

6243
/**
@@ -69,15 +50,30 @@ public function __construct(
6950
public function getTranslationDictionary(): string
7051
{
7152
$asset = $this->assetRepo->createAsset(JsConfig::DICTIONARY_FILE_NAME);
72-
if ($this->appState->getMode() === AppState::MODE_PRODUCTION) {
73-
$staticViewFilePath = $this->filesystem->getDirectoryRead(
74-
DirectoryList::STATIC_VIEW
75-
)->getAbsolutePath();
76-
$content = $this->filesystemDriver->fileGetContents($staticViewFilePath . $asset->getPath());
77-
} else {
78-
$content = $asset->getContent();
79-
}
8053

81-
return $content;
54+
return $asset->getContent();
55+
}
56+
57+
/**
58+
* Get translation dictionary url.
59+
*
60+
* @return string
61+
* @throws LocalizedException
62+
*/
63+
public function getTranslationDictionaryUrl(): string
64+
{
65+
$asset = $this->assetRepo->createAsset(JsConfig::DICTIONARY_FILE_NAME);
66+
67+
return $asset->getUrl();
68+
}
69+
70+
/**
71+
* Check if application is in production mode.
72+
*
73+
* @return bool
74+
*/
75+
public function isAppStateProduction(): bool
76+
{
77+
return $this->appState->getMode() === AppState::MODE_PRODUCTION;
8278
}
8379
}

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,24 @@
66

77
/** @var \Magento\Translation\ViewModel\Dictionary $viewModel */
88
$viewModel = $block->getData('dictionary_view_model');
9-
$dictionary = json_encode($viewModel->getTranslationDictionary(), JSON_HEX_APOS | JSON_UNESCAPED_SLASHES);
9+
if (!$viewModel->isAppStateProduction()) {
10+
$dictionary = json_encode($viewModel->getTranslationDictionary(), JSON_HEX_APOS | JSON_UNESCAPED_SLASHES);
11+
// Use of JSON.parse below is intentional as JSON is significantly faster to parse than JavaScript
12+
?>
13+
<script>
14+
define('mageTranslationDictionary', function () {
15+
var dict = <?= /* @noEscape */ $dictionary ?>;
16+
return JSON.parse(dict);
17+
});
18+
</script>
19+
<?php
20+
} else {
21+
?>
22+
<script>
23+
define('mageTranslationDictionary', ['text!<?= /* @noEscape */ $viewModel->getTranslationDictionaryUrl() ?>'], function (dict) {
24+
return JSON.parse(dict);
25+
});
26+
</script>
27+
<?php
28+
}
1029
?>
11-
<script>
12-
define('dictionary', function () {
13-
var dict = <?= /* @noEscape */ $dictionary ?>;
14-
// Use of JSON.parse is intentional as JSON is significantly faster to parse than JavaScript
15-
return JSON.parse(dict);
16-
});
17-
</script>

dev/tests/js/jasmine/require.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require.config({
2121
},
2222
map: {
2323
'*': {
24-
dictionary: '../../../../../../dev/tests/js/jasmine/tests/lib/mage/fake-dictionary'
24+
mageTranslationDictionary: '../../../../../../dev/tests/js/jasmine/tests/lib/mage/fake-dictionary'
2525
}
2626
},
2727
shim: {

dev/tests/js/jasmine/tests/lib/mage/fake-dictionary.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5-
// Fake dictionary for js unit tests as real dictionary is defined in template and cannot be initialized by requirejs
5+
// Fake translation dictionary for js unit tests as real dictionary is defined in template and cannot be initialized by requirejs
66
define([], function () {
77
'use strict';
88

lib/web/mage/translate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
define([
77
'jquery',
88
'mage/mage',
9-
'dictionary'
9+
'mageTranslationDictionary'
1010
], function ($, mage, dictionary) {
1111
'use strict';
1212

0 commit comments

Comments
 (0)