Stack Exchange Network
Stack Exchange network consists of 183 Q&A communities including
Stack Overflow
, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Visit Stack Exchange
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this community
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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 ?
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
–
–
–
–
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");
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
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 :)
Thanks for contributing an answer to Drupal Answers!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.