Skip to content

Commit

Permalink
ENGCOM-6708: #18012: added i18n wrapper to be used in underscore temp…
Browse files Browse the repository at this point in the history
…lates for translation #26435

 - Merge Pull Request #26435 from sergiy-v/magento2:18012-add-translation-to-underscore
 - Merged commits:
   1. 819c3f2
   2. 064ee0e
   3. 3a19f17
   4. c32b643
   5. e26bbc1
   6. 69d07c1
   7. 58adcc5
  • Loading branch information
magento-engcom-team committed Jan 27, 2020
2 parents 06a0dd0 + 58adcc5 commit 6576820
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
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.__;
});

0 comments on commit 6576820

Please sign in to comment.