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 :


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.


Any ideas ?

5 Answers 5


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


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:


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


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:


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
drush cr

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()").

  • 2
    If you run into that: fix in your webpack.config.js with externals: {"drupal": "Drupal" }, Commented Feb 5, 2021 at 14:51

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.

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

As a last resort, you can load your javascript files through php, and use the drupal php t()-function instead. Your javascript could look something like this:

original file where translation dont work for whatever reason: test.js

console.log(Drupal.t("Am I translated?")); 

Changed to php: test.js.php

// Bootstrapping the drupal installation: 
$drupal_root = '/path/to/drupal'; // Can be relative to your file
define('DRUPAL_ROOT', $drupal_root);
require_once(DRUPAL_ROOT . "/includes/bootstrap.inc");
require_once(DRUPAL_ROOT . "/includes/common.inc");
header('Content-type: application/javascript');
// Get the language as a GET-parameter
$lang = (string) @$_GET['langcode'];
// Sanitize input:
if (!in_array($lang, array_keys(language_list()))) {
  $lang = 'en';
$opt = array('langcode' => $lang);
$n = array();

// Here code can start
echo "console.log('" . t('Am I translated?', $n, $opt) . "')"

add the script as: path/to/test.js?langcode=nb (example for norwegian language)

So, there is some overhead loading drupal, printing the js-code from a php-file, and translating whatever needs to be translated. But all in all, it works! Which was the point for me, at least :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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