Skip to content

Commit 9aaa225

Browse files
authored
ENGCOM-6708: #18012: added i18n wrapper to be used in underscore templates for translation #26435
2 parents 967e424 + 6576820 commit 9aaa225

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Magento\Framework\Phrase\Renderer\Translate;
1616

1717
/**
18-
* Class DataProviderTest
18+
* Verify data provider translation
1919
*
2020
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2121
*/
@@ -105,24 +105,31 @@ public function testGetData()
105105
'hello1' => 'hello1translated',
106106
'hello2' => 'hello2translated',
107107
'hello3' => 'hello3translated',
108-
'hello4' => 'hello4translated'
108+
'hello4' => 'hello4translated',
109+
'ko i18' => 'ko i18 translated',
110+
'underscore i18' => 'underscore i18 translated',
109111
];
110112

111113
$contentsMap = [
112114
'content1$.mage.__("hello1")content1',
113115
'content2$.mage.__("hello2")content2',
114-
'content2$.mage.__("hello4")content4', // this value should be last after running data provider
115-
'content2$.mage.__("hello3")content3',
116+
'content2$.mage.__("hello4")content4 <!-- ko i18n: "ko i18" --><!-- /ko -->',
117+
'content2$.mage.__("hello3")content3 <% _.i18n("underscore i18") %>',
116118
];
117119

118120
$translateMap = [
119121
[['hello1'], [], 'hello1translated'],
120122
[['hello2'], [], 'hello2translated'],
121123
[['hello3'], [], 'hello3translated'],
122-
[['hello4'], [], 'hello4translated']
124+
[['hello4'], [], 'hello4translated'],
125+
[['ko i18'], [], 'ko i18 translated'],
126+
[['underscore i18'], [], 'underscore i18 translated'],
123127
];
124128

125-
$patterns = ['~\$\.mage\.__\(([\'"])(.+?)\1\)~'];
129+
$patterns = [
130+
'~\$\.mage\.__\(([\'"])(.+?)\1\)~',
131+
'~(?:i18n\:|_\.i18n\()\s*(["\'])(.*?)(?<!\\\\)\1~',
132+
];
126133

127134
$this->appStateMock->expects($this->once())
128135
->method('getAreaCode')

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
<type name="Magento\Translation\Model\Js\Config">
6565
<arguments>
6666
<argument name="patterns" xsi:type="array">
67-
<item name="i18n_translation" xsi:type="string"><![CDATA[~i18n\:\s*(["'])(.*?)(?<!\\)\1~]]></item>
67+
<item name="i18n_translation" xsi:type="string"><![CDATA[~(?:i18n\:|_\.i18n\()\s*(["'])(.*?)(?<!\\)\1~]]></item>
6868
<item name="translate_wrapping" xsi:type="string"><![CDATA[~translate\=("')([^\'].*?)\'\"~]]></item>
6969
<item name="mage_translation_widget" xsi:type="string"><![CDATA[~(?s)(?:\$|jQuery)\.mage\.__\(\s*(['"])(?<translate>.+?)(?<!\\)\1\s*(*SKIP)\)\s*(?s)~]]></item>
7070
<item name="mage_translation_static" xsi:type="string"><![CDATA[~(?s)\$t\(\s*(['"])(?<translate>.+?)(?<!\\)\1\s*(*SKIP)\)(?s)~]]></item>

lib/web/mage/translate.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
define([
77
'jquery',
88
'mage/mage',
9-
'mageTranslationDictionary'
10-
], function ($, mage, dictionary) {
9+
'mageTranslationDictionary',
10+
'underscore'
11+
], function ($, mage, dictionary, _) {
1112
'use strict';
1213

1314
$.extend(true, $, {
@@ -46,5 +47,18 @@ define([
4647
});
4748
$.mage.__ = $.proxy($.mage.translate.translate, $.mage.translate);
4849

50+
// Provide i18n wrapper to be used in underscore templates for translation
51+
_.extend(_, {
52+
/**
53+
* Make a translation using $.mage.__
54+
*
55+
* @param {String} text
56+
* @return {String}
57+
*/
58+
i18n: function (text) {
59+
return $.mage.__(text);
60+
}
61+
});
62+
4963
return $.mage.__;
5064
});

0 commit comments

Comments
 (0)