Skip to content

JS Translation Regex leads to unexpected results and untranslatable strings #7403

@thlassche

Description

@thlassche

Preconditions

Magento version: 2.1.2
Translate a string in javascript, for example like this (taken from mage/validation.js):

                    validator.passwordErrorMessage = $.mage.__(
                        "Minimum of different classes of characters in password is %1." +
                        " Classes of characters: Lower Case, Upper Case, Digits, Special Characters."
                    ).replace('%1', passwordMinCharacterSets);

The string should be for example concatted in JavaScript with the + sine.

Steps to reproduce

Look up the page where the translated string should appear (in this case: the register account page)

Expected result

The password error message is translated

Actual result

The password error message is not translated

Cause

The root cause of this, is the regex that selects JS strings for translation, as found in: /app/code/Magento/Translation/etc/di.xml. It's is very buggy and not fail safe. It skips every string that is not strictly formatted according to this regex. In this case the '+' sign causes the string to be skipped by the regex, but there are many more cases where this regex is skipped in JS files (e.g. iteration over parts of strings to translate them). See:

<item name="i18n_translation" xsi:type="string"><![CDATA[~i18n\:\s*(["'])(.*?)(?<!\\)\1~]]></item>
<item name="mage_translation_widget" xsi:type="string">~\$\.mage\.__\((['"])(.+?)\1\)~</item>
<item name="mage_translation_static" xsi:type="string">~\$t\((["'])(.+?)\1\)~</item>

Looks like the implementation of the js-translation.json file should be a bit refactored.

Workaround

A workaround I use in production is to create a JS file which calls the$.mage.__() with the string that I would like to translate, e.g.:

    // HACK to make strings translatable, so the Mage Translate regex picks them up for JS translation
    $.mage.__("Minimum of different classes of characters in password is %1. Classes of characters: Lower Case, Upper Case, Digits, Special Characters.");
    $.mage.__("Minimum length of this field must be equal or greater than %1 symbols. Leading and trailing spaces will be ignored.");

Metadata

Metadata

Assignees

Labels

Component: TranslationEvent: distributed-cdDistributed Contribution DayFixed in 2.1.xThe issue has been fixed in 2.1 release lineFixed in 2.2.xThe issue has been fixed in 2.2 release lineFixed in 2.3.xThe issue has been fixed in 2.3 release lineIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentReproduced on 2.1.xThe issue has been reproduced on latest 2.1 releaseReproduced on 2.2.xThe issue has been reproduced on latest 2.2 releasebug report

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions