diff --git a/app/code/Magento/Translation/Test/Unit/Model/Js/DataProviderTest.php b/app/code/Magento/Translation/Test/Unit/Model/Js/DataProviderTest.php index b5bfbbc29a603..146e1c06782ce 100644 --- a/app/code/Magento/Translation/Test/Unit/Model/Js/DataProviderTest.php +++ b/app/code/Magento/Translation/Test/Unit/Model/Js/DataProviderTest.php @@ -15,7 +15,7 @@ use Magento\Framework\Phrase\Renderer\Translate; /** - * Class DataProviderTest + * Verify data provider translation * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ @@ -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 ', + '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*(["\'])(.*?)(?appStateMock->expects($this->once()) ->method('getAreaCode') diff --git a/app/code/Magento/Translation/etc/di.xml b/app/code/Magento/Translation/etc/di.xml index d17dac23933ee..de476f52a53fe 100644 --- a/app/code/Magento/Translation/etc/di.xml +++ b/app/code/Magento/Translation/etc/di.xml @@ -64,7 +64,7 @@ - + .+?)(? .+?)(? diff --git a/lib/web/mage/translate.js b/lib/web/mage/translate.js index 65ec33266aebd..0a385da8dcf38 100644 --- a/lib/web/mage/translate.js +++ b/lib/web/mage/translate.js @@ -6,8 +6,9 @@ define([ 'jquery', 'mage/mage', - 'mageTranslationDictionary' -], function ($, mage, dictionary) { + 'mageTranslationDictionary', + 'underscore' +], function ($, mage, dictionary, _) { 'use strict'; $.extend(true, $, { @@ -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.__; });