admin管理员组文章数量:1416323
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?
4 Answers
Reset to default 2I 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.
本文标签: javascriptDrupalt doesn39t show up among translationsStack Overflow
版权声明:本文标题:javascript - Drupal.t doesn't show up among translations - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745251548a2649852.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论