Magento – Magento2 Knockout js – translate variable text

knockoutjslocalisationmagento2translate

I'm trying to translate text that comes from a variable in Knockout html file, but I can;t find the right way. Here's some things I tried:

<dt class="label" data-bind="html: i18n(option.label)"></dt>
<dt class="label" data-bind="i18n: option.label"></dt>
<dt class="label"><!-- ko i18n: option.label --><!-- /ko --></dt>

option.label returns a string like 'width' or 'height'. There words are included in a .csv translation file.

Best Answer

both the following answer you give are good:

<dt class="label" data-bind="i18n: option.label"></dt>
<dt class="label"><!-- ko i18n: option.label --><!-- /ko --></dt>

The i18n attribute in Magento javascript need a string. So the important point to verify is that the value given by option.label is a string and that string is in your *.csv.

It's like doing:

<dt class="label" data-bind="i18n: 'My label'"></dt>
<dt class="label"><!-- ko i18n: 'My label' --><!-- /ko --></dt>

More details in the Magento Dev documentation: https://devdocs.magento.com/guides/v2.3/frontend-dev-guide/translations/translate_theory.html#add_strings_ui_html

But sometime, you need to check if your translation is well generated in your pub/static folder.

Related Topic