javascript - Drupal.t doesn't show up among translations - Stack Overflow

I'm trying to make a translation in JavaScript on a Drupal site. I'm using the js function Dr

I'm trying to make a translation in JavaScript on a Drupal site. I'm using the js function Drupal.t(). Everything seems to work; Drupal is loaded, the function gets called, placeholders get replaced, but the translation doesn't happen. The words stay in English, and the words aren't added to the translations database. Does anybody know why this happens and how to get solve it?

I'm trying to make a translation in JavaScript on a Drupal site. I'm using the js function Drupal.t(). Everything seems to work; Drupal is loaded, the function gets called, placeholders get replaced, but the translation doesn't happen. The words stay in English, and the words aren't added to the translations database. Does anybody know why this happens and how to get solve it?

Share Improve this question edited Jan 11, 2014 at 12:44 Mikkel R. Lund asked Jul 12, 2011 at 12:31 Mikkel R. LundMikkel R. Lund 2,6372 gold badges33 silver badges50 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

I know your post is rather old, but my answer might help others.

I've noticed that Drupal.locale.strings doesn't get populated with the JavaScript calls.

So what I usually do is simply create a portion of PHP code that does this work on the server side. You can do it anywhere in your PHP code. But the best is to do it in the module you are coding. It will then be easier to export it with the Potx module so that you can have your strings in some *.po files for a later use of your module.

If you are in a hurry, you can simply do it in the body of a dummy node (just do a "preview") with the PHP input format:

<?php

print t('Example : %variable', array('%variable' => 'test'));

?>

Once this has been done, you should be able to find your strings and translate them in the admin page.

To regenerate the JavaScript file, you'll have to clear all your cache (with Devel or by visiting the modules page).

Not sure about the javascript part but I've used the t funciton a lot. If you want the strings to show up in the translation table you have to load the corresponding page in two different languages before it will allow you to translate them. Hope that helps.

The Drupal.t() function is very small, so I bet we can just dissect it here.

function (str, args) {
  // Fetch the localized version of the string.
  if (Drupal.locale.strings && Drupal.locale.strings[str]) {
    str = Drupal.locale.strings[str];
  }

  if (args) {
    // Transform arguments before inserting them
    for (var key in args) {
      switch (key.charAt(0)) {
        // Escaped only
        case '@':
          args[key] = Drupal.checkPlain(args[key]);
        break;
        // Pass-through
        case '!':
          break;
        // Escaped and placeholder
        case '%':
        default:
          args[key] = Drupal.theme('placeholder', args[key]);
          break;
      }
      str = str.replace(key, args[key]);
    }
  }
  return str;
}

So you can see that it checks Drupal.locale.strings to find the translatable strings for the current locale. Can you pop open a console on your Drupal site and type Drupal.locale and see what it spits out?

In Drupal, to add a new language, you must first enable the Locale module. Have you done that?

Next, you have to import your language through the Translate Interface at admin/build/translate. Have you done that?

Here you can see I've imported French and German: http://grimhappy./i/ce75ca.png

¿Are you pressing out the js? Check at the performance section of your site.

Clearing the cache should regenerate the js with the right translations now.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745251549a4618707.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信