6

I am tring to use Drupal.t() within some javascript, and I am not able to do it.

Actually, it throws no errors, Doing something like :

console.log(Drupal.t('supplier')); 

returns 'supplier', even though I am in another language and the alternative value was supplied for such language (The PHP code with the t() function works).

Extra question : Is there a way, to know from Drupal Javascript utilities the current locale, I am in ?

At the moment, I found a workaround by testing the following (pure jQuery) :

if(jQuery('body').hasClass('i18n-fr')){//so that we can 
 locale = '/fr';                       //make localized ajax calls.
} 

Cheers,

Any ideas ?

7

After using Drupal.t(), clear the cache once, and search text to translate from admin/build/translate/search.

| improve this answer | |
3

You can't call console.log from your firebug/chrome console as drupal generates a js translation file for all texts found in your javascript source.

| improve this answer | |
  • Hello, the following code : jQuery('body').text(Drupal.t('some translated text')); does not work either. Thank you for your input anyway. – Cypher Jul 2 '12 at 6:49
  • Do you use drupal_add_js? – jantimon Jul 2 '12 at 7:29
  • Yes, I do : 'my_file.js' is added via drupal_add_js. Everything works fine, except this... – Cypher Jul 2 '12 at 14:36
  • Same issue here! – Luca Oct 7 '12 at 3:12
3

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

  1. 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');        
    }
};
  1. Clear caches

  2. 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.

  3. Clear caches

  4. Go to the Translate page (/admin/config/regional/translate)

  5. You should now find the string you want to translate for example 'Go to checkout' and translate the string

  6. Hit the save translation button at the bottom of the page

  7. 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
| improve this answer | |
  • Upvote for "myThemeTranslateStrings.js". A nice tip to force Drupal generate translation strings instead of do the test one by one to generate them. – Trà Dương Aug 7 '19 at 9:55
0

Note that when using a javascript minifier you should disable obfuscation or Drupal won't be able to find "Drupal.t" (it will be obfuscated to something like "e.t()").

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.