In some cases the Drupal.t function doesnt work because the Drupal.t function doent get properly attached to drupal and therefor not picked up by the Javascript Locale Builder
- First make sure the javascript file where the Drupal.t() function gets called is properly attached the drupal way.
If you're not sure that the file where the Drupal.t funtion is called is properly embedded. you can add the Drupal.t() function in custom a javascript file in your theme or module and make sure this custom javascript file is embedded and contains the strings you want to translate like so:
/themes/mytheme/js/myThemeTranslateStrings.js
Drupal.behaviors.myThemeTranslateStrings = {
attach: function (context, settings) {
Drupal.t('Your shopping cart is empty.');
Drupal.t('Go to checkout');
Drupal.t('Update quantities');
Drupal.t('Another string I want to translate');
}
};
Clear caches
Visit the page where the javascript file with the Drupal.t() function is called
In case you attached it to your theme and set it globaly any page is fine.
Clear caches
Go to the Translate page (/admin/config/regional/translate)
You should now find the string you want to translate for example 'Go to checkout' and translate the string
Hit the save translation button at the bottom of the page
Clear caches again
and it should work now
Debugging
If not you can debug by going to the console of your browser and type:
Drupal.t('Go to checkout');
It should then return the translated string.
Another method is to type:
window.drupalTranslations
This should return an object with all the available strings for Drupal.t() to translate. If your string is not there it won't get translated even if the string is known in the translation interface.
To force a rebuild of the javascript translation object above you can do so with drush:
drush php:cli
_locale_rebuild_js('YOUR_TWO_CHAR_LANG_CODE_HERE');
exit;
drush cr