Skip to content

Commit

Permalink
Merge pull request #4848 from magento-performance/MC-19247
Browse files Browse the repository at this point in the history
[Performance] Broken JS translations with advanced bundling
  • Loading branch information
vzabaznov authored Oct 3, 2019
2 parents 2cc9d29 + c466345 commit 71c781d
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 43 deletions.
3 changes: 2 additions & 1 deletion app/code/Magento/Backend/view/adminhtml/requirejs-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
var config = {
map: {
'*': {
'mediaUploader': 'Magento_Backend/js/media-uploader'
'mediaUploader': 'Magento_Backend/js/media-uploader',
'mage/translate': 'Magento_Backend/js/translate'
}
}
};
54 changes: 54 additions & 0 deletions app/code/Magento/Backend/view/adminhtml/web/js/translate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

/* eslint-disable strict */
(function (factory) {
if (typeof define === 'function' && define.amd) {
define([
'jquery',
'mage/mage'
], factory);
} else {
factory(jQuery);
}
}(function ($) {
$.extend(true, $, {
mage: {
translate: (function () {
/**
* Key-value translations storage
* @type {Object}
* @private
*/
var _data = {};

/**
* Add new translation (two string parameters) or several translations (object)
*/
this.add = function () {
if (arguments.length > 1) {
_data[arguments[0]] = arguments[1];
} else if (typeof arguments[0] === 'object') {
$.extend(_data, arguments[0]);
}
};

/**
* Make a translation with parsing (to handle case when _data represents tuple)
* @param {String} text
* @return {String}
*/
this.translate = function (text) {
return _data[text] ? _data[text] : text;
};

return this;
}())
}
});
$.mage.__ = $.proxy($.mage.translate.translate, $.mage.translate);

return $.mage.__;
}));
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<element name="cartItemsAreaActive" type="textarea" selector="div.block.items-in-cart.active" timeout="30"/>
<element name="checkMoneyOrderPayment" type="radio" selector="input#checkmo.radio" timeout="30"/>
<element name="placeOrder" type="button" selector=".payment-method._active button.action.primary.checkout" timeout="30"/>
<element name="paymentSectionTitle" type="text" selector="//*[@id='checkout-payment-method-load']//div[text()='Payment Method']" />
<element name="paymentSectionTitle" type="text" selector="//*[@id='checkout-payment-method-load']//div[@data-role='title']" />
<element name="orderSummarySubtotal" type="text" selector="//tr[@class='totals sub']//span[@class='price']" />
<element name="orderSummaryShippingTotal" type="text" selector="//tr[@class='totals shipping excl']//span[@class='price']" />
<element name="orderSummaryShippingMethod" type="text" selector="//tr[@class='totals shipping excl']//span[@class='value']" />
Expand Down
1 change: 0 additions & 1 deletion app/code/Magento/Theme/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"magento/module-widget": "*"
},
"suggest": {
"magento/module-translation": "*",
"magento/module-theme-sample-data": "*",
"magento/module-deploy": "*",
"magento/module-directory": "*"
Expand Down
2 changes: 0 additions & 2 deletions app/code/Magento/Theme/view/frontend/layout/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
<block name="require.js" class="Magento\Framework\View\Element\Template" template="Magento_Theme::page/js/require_js.phtml" />
<referenceContainer name="after.body.start">
<block class="Magento\RequireJs\Block\Html\Head\Config" name="requirejs-config"/>
<block class="Magento\Translation\Block\Html\Head\Config" name="translate-config"/>
<block class="Magento\Translation\Block\Js" name="translate" template="Magento_Translation::translate.phtml"/>
<block class="Magento\Framework\View\Element\Js\Cookie" name="js_cookies" template="Magento_Theme::js/cookie.phtml"/>
<block class="Magento\Theme\Block\Html\Notices" name="global_notices" template="Magento_Theme::html/notices.phtml"/>
</referenceContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

/** @var \Magento\Translation\Block\Js $block */
?>
<!--
For frontend area dictionary file is inserted into html head in Magento/Translation/view/base/templates/dictionary.phtml
Same translation mechanism should be introduced for admin area in 2.4 version.
-->
<?php if ($block->dictionaryEnabled()) : ?>
<script>
require.config({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

define([
'text!js-translation.json'
], function (dict) {
'use strict';

return JSON.parse(dict);
});
14 changes: 14 additions & 0 deletions app/code/Magento/Translation/view/frontend/layout/default.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="head.additional">
<block class="Magento\Translation\Block\Html\Head\Config" name="translate-config"/>
</referenceBlock>
</body>
</page>
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ var config = {
'*': {
editTrigger: 'mage/edit-trigger',
addClass: 'Magento_Translation/js/add-class',
'Magento_Translation/add-class': 'Magento_Translation/js/add-class'
'Magento_Translation/add-class': 'Magento_Translation/js/add-class',
mageTranslationDictionary: 'Magento_Translation/js/mage-translation-dictionary'
}
},
deps: [
'mage/translate-inline'
'mage/translate-inline',
'mageTranslationDictionary'
]
};
9 changes: 5 additions & 4 deletions dev/tests/js/jasmine/tests/lib/mage/translate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ define([
], function ($) {
'use strict';

// be careful with test variation order as one variation can affect another one
describe('Test for mage/translate jQuery plugin', function () {
it('works with one string as parameter', function () {
$.mage.translate.add('Hello World!');
expect('Hello World!').toEqual($.mage.translate.translate('Hello World!'));
});
it('works with translation alias __', function () {
$.mage.translate.add('Hello World!');
expect('Hello World!').toEqual($.mage.__('Hello World!'));
});
it('works with one array as parameter', function () {
$.mage.translate.add(['Hello World!', 'Bonjour tout le monde!']);
expect('Hello World!').toEqual($.mage.translate.translate('Hello World!'));
Expand All @@ -40,10 +45,6 @@ define([
$.mage.translate.add('Hello World!', 'Bonjour tout le monde!');
expect('Bonjour tout le monde!').toEqual($.mage.translate.translate('Hello World!'));
});
it('works with translation alias __', function () {
$.mage.translate.add('Hello World!');
expect('Hello World!').toEqual($.mage.__('Hello World!'));
});
});

});
60 changes: 28 additions & 32 deletions lib/web/mage/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
* See COPYING.txt for license details.
*/

/* eslint-disable strict */
(function (factory) {
if (typeof define === 'function' && define.amd) {
define([
'jquery',
'mage/mage'
], factory);
} else {
factory(jQuery);
}
}(function ($) {
define([
'jquery',
'mage/mage',
'mageTranslationDictionary'
], function ($, mage, dictionary) {
'use strict';

$.extend(true, $, {
mage: {
translate: (function () {
Expand All @@ -22,33 +18,33 @@
* @type {Object}
* @private
*/
var _data = {};
var _data = dictionary;

/**
* Add new translation (two string parameters) or several translations (object)
*/
this.add = function () {
if (arguments.length > 1) {
_data[arguments[0]] = arguments[1];
} else if (typeof arguments[0] === 'object') {
$.extend(_data, arguments[0]);
}
};
return {
/**
* Add new translation (two string parameters) or several translations (object)
*/
add: function () {
if (arguments.length > 1) {
_data[arguments[0]] = arguments[1];
} else if (typeof arguments[0] === 'object') {
$.extend(_data, arguments[0]);
}
},

/**
* Make a translation with parsing (to handle case when _data represents tuple)
* @param {String} text
* @return {String}
*/
this.translate = function (text) {
return _data[text] ? _data[text] : text;
/**
* Make a translation with parsing (to handle case when _data represents tuple)
* @param {String} text
* @return {String}
*/
translate: function (text) {
return _data[text] ? _data[text] : text;
}
};

return this;
}())
}
});
$.mage.__ = $.proxy($.mage.translate.translate, $.mage.translate);

return $.mage.__;
}));
});

0 comments on commit 71c781d

Please sign in to comment.