Skip to content

#18012: added i18n wrapper to be used in underscore templates for translation #26435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Magento\Framework\Phrase\Renderer\Translate;

/**
* Class DataProviderTest
* Verify data provider translation
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
Expand Down Expand Up @@ -105,24 +105,31 @@ public function testGetData()
'hello1' => 'hello1translated',
'hello2' => 'hello2translated',
'hello3' => 'hello3translated',
'hello4' => 'hello4translated'
'hello4' => 'hello4translated',
'ko i18' => 'ko i18 translated',
'underscore i18' => 'underscore i18 translated',
];

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

$translateMap = [
[['hello1'], [], 'hello1translated'],
[['hello2'], [], 'hello2translated'],
[['hello3'], [], 'hello3translated'],
[['hello4'], [], 'hello4translated']
[['hello4'], [], 'hello4translated'],
[['ko i18'], [], 'ko i18 translated'],
[['underscore i18'], [], 'underscore i18 translated'],
];

$patterns = ['~\$\.mage\.__\(([\'"])(.+?)\1\)~'];
$patterns = [
'~\$\.mage\.__\(([\'"])(.+?)\1\)~',
'~(?:i18n\:|_\.i18n\()\s*(["\'])(.*?)(?<!\\\\)\1~',
];

$this->appStateMock->expects($this->once())
->method('getAreaCode')
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Translation/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<type name="Magento\Translation\Model\Js\Config">
<arguments>
<argument name="patterns" xsi:type="array">
<item name="i18n_translation" xsi:type="string"><![CDATA[~i18n\:\s*(["'])(.*?)(?<!\\)\1~]]></item>
<item name="i18n_translation" xsi:type="string"><![CDATA[~(?:i18n\:|_\.i18n\()\s*(["'])(.*?)(?<!\\)\1~]]></item>
<item name="translate_wrapping" xsi:type="string"><![CDATA[~translate\=("')([^\'].*?)\'\"~]]></item>
<item name="mage_translation_widget" xsi:type="string"><![CDATA[~(?s)(?:\$|jQuery)\.mage\.__\(\s*(['"])(?<translate>.+?)(?<!\\)\1\s*(*SKIP)\)\s*(?s)~]]></item>
<item name="mage_translation_static" xsi:type="string"><![CDATA[~(?s)\$t\(\s*(['"])(?<translate>.+?)(?<!\\)\1\s*(*SKIP)\)(?s)~]]></item>
Expand Down
18 changes: 16 additions & 2 deletions lib/web/mage/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
define([
'jquery',
'mage/mage',
'mageTranslationDictionary'
], function ($, mage, dictionary) {
'mageTranslationDictionary',
'underscore'
], function ($, mage, dictionary, _) {
'use strict';

$.extend(true, $, {
Expand Down Expand Up @@ -46,5 +47,18 @@ define([
});
$.mage.__ = $.proxy($.mage.translate.translate, $.mage.translate);

// Provide i18n wrapper to be used in underscore templates for translation
_.extend(_, {
/**
* Make a translation using $.mage.__
*
* @param {String} text
* @return {String}
*/
i18n: function (text) {
return $.mage.__(text);
}
});

return $.mage.__;
});