Magento 2 – How to Translate String in Function in Knockout Template

javascriptknockoutjslocalisationmagento2template

In a Magento Knockout template I find a function like so:

<element data-bind="something: function() {
    return 'some string';
}" />

How can I translate this string? I tried following, but it does not work:

<element data-bind="something: function() {
    return $t('some string');
}" />

The translation exists and works in other places, so that is not the problem.

Best Answer

Try adding i18n: in front of the value of data-bind attribute. Something like below;

<element data-bind="i18n: something: function() {
    return $t('some string');
}" />

Note: Please note that the translation for corresponding string should be present in your language translation csv.

Related Topic