Well I spent a lot of hours looking how templates and other stuffs are related with knockout.js
, i will try to explain how subtotal price format is displayed to order summary at checkout module, but is same to other things too. My current version is Magento ver. 2.0.4.
First if you navigate to the corresponding layout file for checkout at
vendor/magento/module-checkout/view/frontend/layout/checkout_index_index.xml
you will see at line 392 inside cart_items
block the subtotal block like this:
As you can see , Magento calls a js template to display subtotal of product right after product details.
If you navigate to
vendor/magento/module-checkout/view/frontend/web/js/view/summary/subtotal.js
you will see at line 15 is called subtotal template , and a function called getValue()
,
getValue: function () {
return this.getFormattedPrice(this.getPureValue());
}
which later i saw it is called in subtotal template , which is located to
vendor/magento/module-checkout/view/frontend/web/template/summary/subtotal.html
with this code:
Subtotal.js
file uses
Magento_Checkout/view/frontend/web/js/view/summary/abstract-total.js
getFormattedPrice()
function, while this file uses
Magento_Checkout/view/frontend/web/js/model/quote.js getPriceFormat()
function read about require.js dependencies ,
which returns a price format, and here i saw that this variable is populated like this,
var priceFormat = window.checkoutConfig.priceFormat;
So where does window.checkoutConfig.priceFormat comes from?
Magento 2 did this:
If you navigate to
/vendor/magento/module-checkout/view/frontend/templates/onepage.phtml
at line 26 is initialized window.checkoutConfig with $block->getCheckoutConfig()
, which function is at
/vendor/magento/module-checkout/Block/Onepage.php
line 89, here it takes all config providers and returns them.
Where are config providers defined?
They are defined at
vendor/magento/module-checkout/etc/frontend/di.xml
at line 46, at sends us to
Magento\Checkout\Model\DefaultConfigProvider.php
which has getConfig()
function, at line 254 you can see getConfig()
function and at line 271 is priceFormat populated like this:
$output['priceFormat'] = $this->localeFormat->getPriceFormat(
null,
$this->checkoutSession->getQuote()->getQuoteCurrencyCode()
);
This means priceFormat is declared at populated here at getConfig()
function, so you can later use it with window.checkouConfig.priceFormat
.
For Magento to apply the bindings you need to add your Knockout JS using the UI Components like so:
Inside the PHTML Template file
Pay extra attention to example-scope
as it needs to match the scope of the markup.
Component links to your JS file, and template links to your HTML (or PHTML) file that contains the data-binds (and scope).
<script type="text/x-magento-init">
{
"*": {
"Magento_Ui/js/core/app": {
"components": {
"example-scope": {
"component": "VENDOR_MODULE/js/name-of-your-js-file",
"template": "Magento_Checkout/summary/item/details"
}
}
}
}
}
</script>
<div data-bind="scope: 'example-scope'">
<!-- ko template: getTemplate() --><!-- /ko -->
</div>
Inside your JS file
define(['jquery', 'ko'], function($, ko) {
return function(config) {
// YOUR SCRIPT HERE
}
});
Best Answer
To ensure that the text you add in a .js file is collected by the i18n tool and added to the dictionary:
Link the mage/translate library
define (['jquery', 'mage/translate'], function ($) {...});
Use the
$.mage.__('')
function when adding a string:$.mage.__('<string>');
If your string contains a variable, to add a placeholder for this variable to the string stored in the dictionary, use the syntax similar to the following:
In this example, the 'Hello %1' string is added to the dictionary when the i18n tool is run.
Reference : https://devdocs.magento.com/guides/v2.3/frontend-dev-guide/translations/translate_theory.html
For phtmls
$t
does the same thing asmage/translate
so you can do following ;