According to the documentation, we can use contexts with Drupal.t()
since Drupal 8.
Drupal.t('May', {}, {context: "Long month name"});
works fine, but Drupal.t('May', {}, {context: "Abbreviated month name"});
doesn't translate May.
Why?
According to the documentation, we can use contexts with Drupal.t()
since Drupal 8.
Drupal.t('May', {}, {context: "Long month name"});
works fine, but Drupal.t('May', {}, {context: "Abbreviated month name"});
doesn't translate May.
Why?
For performance, Drupal do not load all translation string into Javascript. It need to specifically identify the translation string exists in a Javascript to load. Then the locale module will load your translation if the relevant JS file is loaded through the usual asset loading method (i.e. *.libraries.yml declaration).
To check if any of the translation text from the given context is loaded, you may check the window.drupalTranslations
global object to see if it has loaded anything in your context.
Object.getPwnPropertyNames(druaplTranslations.strings);
Without any context loaded, the only available key of druaplTranslations.strings
here is ""
:
This means the core locale module doesn't know that your module Javascript has a Drupal.t()
call.
To manually fix this:
Drupal.t()
call is rewritten into something else.drush php:eval "_locale_parse_js_file('modules/custom/MODULENAME/js/path.to.your.js');"
drush cr
(Also created #3460518 to see if there is a better way)