[RCD] Internationalization in JavaScript

Brendan brendan at tucows.com
Wed Sep 28 18:24:42 CEST 2016


> I am using |rcmail.gettext('somelabel', 'plugin-name') to label
> something within my java script file (as described in Wiki).
> But my label is always 'somelabel' and not the translated text.
> My plugin name has underscores in the name, but that should not matter?
> 
> What am I doing wrong?

you also need to pass through those strings to the javascript from the
php backend. in the php bits you'd have these :

// load in the language strings from plugindir/localization/
$this->add_texts('localization/', false);
// send the strings to the client
$rcmail->output->add_label(['somelabel']);

likely, you'll call add_texts in your init() function, and add_label
inside a function you are referencing with register_action or add_hook
inside the init() function. you can call it in init() if need be, you
might just need to wrap it inside a test to make sure the add_label
function exists:

$this->rc = rcube::get_instance();
if (method_exists($this->rc->output,'add_label')) {
  $rcmail->output->add_label(['somelabel']);
}

then inside your javascript you can call
rcmail.get_label('pluginname.somelabel') to get the localized version of
that string. (you can see the labels stored in rcmail.labels)

doing it this way allows the php backend and js frontend scripts to both
have access to the localized strings.


More information about the dev mailing list