Skip to content

Instantly share code, notes, and snippets.

@cagartner
Created June 8, 2021 14:16
Show Gist options
  • Save cagartner/d9344fa7df4b76cefe40d9a38f703057 to your computer and use it in GitHub Desktop.
Save cagartner/d9344fa7df4b76cefe40d9a38f703057 to your computer and use it in GitHub Desktop.
Translate strings on Magento 2 on Javascript components
define (['jquery', 'mage/translate'], function ($, $t) {
return function () {
// 1. $.mage.__('<string>');
var string1 = $.mage.__('<string>');
// 2. Use the $.mage.__('') function when adding a string:
var string2 = $.mage.__('<string>');
// or
var string3 = $t('<string>');
// If your string contains a variable, to add a placeholder for this variable to the string stored in the dictionary, use the following syntax:
var string4 = $.mage.__('Hello %1').replace('%1', yourVariable);
// or
var string5 = $t('Hello %1').replace('%1', yourVariable);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment