0

I have read that the string parameter passed to t() function should be literal, which excludes:

  • variables, even the parameters of a function: t($description)
  • a concatenation of strings: t('If you want to add a link, click on' . '<a href="http://example.com">this link</a>.')
  • the value returned from a function:t(get_menu_description())
  • a constant: t(MYMODULE_MY_WIDGET_TITLE), t(MyClass::WIDGET_TITLE)

as mentioned here Is it always bad to pass a variable through t()?.

So, does Drupal.t() inside JS file has the same restrictions? as I have find a string inside some module that is passed as a variable

 $(this).addClass('open').text(Drupal.t(settings.linkText));

This string settings.linkText can't be located at the translation interface and I tried to change it to

 $(this).addClass('open').text(Drupal.t('@txt',{@txt:settings.linkText}));

but still couldn't find it. Any Ideas?

1 Answer 1

0

The string will show up in the translation interface if it is found by the js code parser. And a variable can't be worked on by the parser, it has to be a string. So

$(this).addClass('open').text(Drupal.t(settings.linkText));

can't show up. It doesn't prevent the string to be translated though, if the js is aware by another way of the translation : if you write somewhere else in your js

Drupal.t('my_string');

then the parser will find 'my_string' to translate, and the js will load it next time it's called, and if settings.linkText happens to equals 'my_string', then it will be translated.

You have to flush the js cache after the js code alteration, so the parser can look again for strings. It may be why the

$(this).addClass('open').text(Drupal.t('@txt',{@txt:settings.linkText}));

doesn't show '@txt' in the translation interface.

Your Answer

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

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