开发者

Drupal.t doesn't show up among translations

开发者 https://www.devze.com 2023-03-19 11:20 出处:网络
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

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 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.com/i/ce75ca.png


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消