Skip to content

Commit c6cc171

Browse files
committed
MAGETWO-73047: [GITHUB] Product Edit Page Can't Load #5967
1 parent ba3e643 commit c6cc171

File tree

4 files changed

+198
-89
lines changed

4 files changed

+198
-89
lines changed

app/code/Magento/Translation/Model/Js/PreProcessor.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Translation\Model\Js;
79

810
use Magento\Framework\App\AreaList;
@@ -85,13 +87,13 @@ public function translate($content)
8587
}
8688

8789
/**
88-
* Replace callback for preg_replace_callback function
90+
* Replace callback for preg_replace_callback function.
8991
*
9092
* @param array $matches
9193
* @return string
9294
*/
9395
protected function replaceCallback($matches)
9496
{
95-
return '"' . __($matches[1]) . '"';
97+
return '\'' . __($matches['translate']) . '\'';
9698
}
9799
}

app/code/Magento/Translation/Test/Unit/Model/Js/PreProcessorTest.php

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

app/code/Magento/Translation/etc/di.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@
6565
<argument name="patterns" xsi:type="array">
6666
<item name="i18n_translation" xsi:type="string"><![CDATA[~i18n\:\s*(["'])(.*?)(?<!\\)\1~]]></item>
6767
<item name="translate_wrapping" xsi:type="string"><![CDATA[~translate\=("')([^\'].*?)\'\"~]]></item>
68-
<item name="mage_translation_widget" xsi:type="string"><![CDATA[~(?:\$|jQuery)\.mage\.__\((?s)[^'"]*?(['"])(.+?)(?<!\\)\1(?s).*?\)~]]></item>
69-
<item name="mage_translation_static" xsi:type="string"><![CDATA[~\$t\((?s)[^'"]*?(["'])(.+?)\1(?s).*?\)~]]></item>
68+
<item name="mage_translation_widget" xsi:type="string"><![CDATA[~(?s)(?:\$|jQuery)\.mage\.__\(\s*(['"])(?<translate>.+?)(?<!\\)\1\s*(*SKIP)\)\s*(?s)~]]></item>
69+
<item name="mage_translation_static" xsi:type="string"><![CDATA[~(?s)\$t\(\s*(['"])(?<translate>.+?)(?<!\\)\1\s*(*SKIP)\)(?s)~]]></item>
7070
<item name="translate_args" xsi:type="string"><![CDATA[~translate args\=("|'|"')([^\'].*?)('"|'|")~]]></item>
7171
</argument>
7272
</arguments>
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Translation\Model\Js;
9+
10+
use Magento\Framework\App\AreaList;
11+
use Magento\Framework\Phrase;
12+
use Magento\Framework\Phrase\RendererInterface;
13+
use Magento\Framework\Translate;
14+
use Magento\Framework\View\FileSystem;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use Magento\TestFramework\Helper\CacheCleaner;
17+
18+
/**
19+
* Class for testing translation.
20+
*/
21+
class PreProcessorTest extends \PHPUnit\Framework\TestCase
22+
{
23+
/**
24+
* @var PreProcessor
25+
*/
26+
private $model;
27+
28+
/**
29+
* @var RendererInterface
30+
*/
31+
private $origRenderer;
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
protected function setUp()
37+
{
38+
$viewFileSystem = $this->createPartialMock(FileSystem::class, ['getLocaleFileName']);
39+
$viewFileSystem->expects($this->any())
40+
->method('getLocaleFileName')
41+
->willReturn(
42+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
43+
dirname(__DIR__) . '/_files/Magento/Store/i18n/en_AU.csv'
44+
);
45+
46+
$objectManager = Bootstrap::getObjectManager();
47+
$objectManager->addSharedInstance($viewFileSystem, FileSystem::class);
48+
$translator = $objectManager->create(Translate::class);
49+
$objectManager->addSharedInstance($translator, Translate::class);
50+
$areaList = $this->getMockBuilder(AreaList::class)
51+
->disableOriginalConstructor()
52+
->getMock();
53+
$this->origRenderer = Phrase::getRenderer();
54+
Phrase::setRenderer(
55+
$objectManager->get(RendererInterface::class)
56+
);
57+
58+
$this->model = $objectManager->create(
59+
PreProcessor::class,
60+
[
61+
'translate' => $translator,
62+
'areaList' => $areaList,
63+
]
64+
);
65+
66+
$translator->setLocale('en_AU');
67+
$translator->loadData();
68+
}
69+
70+
/**
71+
* @inheritdoc
72+
*/
73+
protected function tearDown()
74+
{
75+
Phrase::setRenderer($this->origRenderer);
76+
}
77+
78+
/**
79+
* Test for backend translation strategy.
80+
*
81+
* @param string $content
82+
* @param string $translation
83+
* @return void
84+
* @dataProvider contentForTranslateDataProvider
85+
*/
86+
public function testProcess(string $content, string $translation)
87+
{
88+
CacheCleaner::cleanAll();
89+
$this->assertEquals($translation, $this->model->translate($content));
90+
}
91+
92+
/**
93+
* Data provider for translation.
94+
*
95+
* @return array
96+
*/
97+
public function contentForTranslateDataProvider(): array
98+
{
99+
return [
100+
'i18n_js_file_error' => [
101+
'setTranslateProp = function (el, original) {
102+
var location = $(el).prop(\'tagName\').toLowerCase(),
103+
translated = $.mage.__(original),
104+
translationData = {
105+
shown: translated,
106+
translated: translated,
107+
original: original
108+
},
109+
translateAttr = composeTranslateAttr(translationData, location);
110+
$(el).attr(\'data-translate\', translateAttr);
111+
setText(el, translationData.shown);
112+
},',
113+
'setTranslateProp = function (el, original) {
114+
var location = $(el).prop(\'tagName\').toLowerCase(),
115+
translated = $.mage.__(original),
116+
translationData = {
117+
shown: translated,
118+
translated: translated,
119+
original: original
120+
},
121+
translateAttr = composeTranslateAttr(translationData, location);
122+
$(el).attr(\'data-translate\', translateAttr);
123+
setText(el, translationData.shown);
124+
},',
125+
],
126+
'checkTranslationWithWhiteSpaces' => [
127+
<<<i18n
128+
title: $.mage.__(
129+
'Original value for Magento_Store module'
130+
),
131+
title: \$t(
132+
'Original value for Magento_Store module'
133+
);
134+
title: jQuery.mage.__(
135+
'Original value for Magento_Store module'
136+
);
137+
i18n
138+
,
139+
<<<i18n
140+
title: 'Translated value for Magento_Store module in en_AU',
141+
title: 'Translated value for Magento_Store module in en_AU';
142+
title: 'Translated value for Magento_Store module in en_AU';
143+
i18n
144+
,
145+
],
146+
'checkTranslationWithReplace' => [
147+
<<<i18n
148+
$.mage.__('The maximum you may purchase is %1.').replace('%1', params.maxAllowed);
149+
\$t('The maximum you may purchase is %1.').replace('%1', params.maxAllowed);
150+
i18n
151+
,
152+
<<<i18n
153+
'The maximum you may purchase is %1.'.replace('%1', params.maxAllowed);
154+
'The maximum you may purchase is %1.'.replace('%1', params.maxAllowed);
155+
i18n
156+
,
157+
],
158+
'checkAvoidingMatchingWithJsInString' => [
159+
<<<i18n
160+
\$t('Payment ' + this.getTitle() + ' can\'t be initialized')
161+
\$t(
162+
'Set unique country-state combinations within the same fixed product tax. ' +
163+
'Verify the combinations and try again.'
164+
)
165+
i18n
166+
,
167+
<<<i18n
168+
\$t('Payment ' + this.getTitle() + ' can\'t be initialized')
169+
\$t(
170+
'Set unique country-state combinations within the same fixed product tax. ' +
171+
'Verify the combinations and try again.'
172+
)
173+
i18n
174+
,
175+
],
176+
'checkAvoidMatchingPhtml' => [
177+
<<<i18n
178+
globalMessageList.addErrorMessage({
179+
message: \$t(<?= /* @noEscape */ json_encode(\$params['error_msg'])?>)
180+
});
181+
i18n
182+
,
183+
<<<i18n
184+
globalMessageList.addErrorMessage({
185+
message: \$t(<?= /* @noEscape */ json_encode(\$params['error_msg'])?>)
186+
});
187+
i18n
188+
,
189+
]
190+
];
191+
}
192+
}

0 commit comments

Comments
 (0)