Skip to content

Translation system

adam-clarey edited this page Mar 21, 2016 · 6 revisions

Translation is provided by the (i18n-node)[https://github.com/mashpie/i18n-node] module which integrates into Iris. It allows text strings to be altered when they are displayed to a user depending on context such as the user's language.

Language doesn't have to mean a national language. It can be anything that changes the format of how text is written out. A site might want to, for example, change the tone of a system message using the translation system.

The authPass language object

i18n is attached to the authPass object which should be available in all page callbacks and hooks (req.authPass in callbacks, thisHook.authPass in hooks). You would set the 'locale' of the current request to what ever logic determines the language of the current user.

To change this depending on circumstance use hook_auth_authpass to set the user's language for a session (page request etc).

iris.modules.mymodule.registerHook('hook_auth_authpass', 0, function(thisHook, authPass){

  if(thisHook.context.req.url.indexOf("/cz/") !== -1){

    authpass.setLocale("cz");    

  } 

  thisHook.pass(authPass);

})

Translating strings

Simply by wrapping text in the below, i18n will automatically output all translated text to files in the configurations directory (eg, /configurations/locales/de.json). Altering these files with the appropriate translation text will feed through the the display.

Translating text in a hook

iris.modules.myModule.registerHook("hook_myCustom_hook", 0, function (thisHook, data) {

  var ap = thisHook.authPass;

  // You can use placeholder parameters.
  ap.t('Hello {{name}}', {name: 'John'});

});

If the locale for this request had been set to 'fr' and in the /configurations/locales/fr.json the line:

"Hello {{name}}": "Hello {{name}}"

Is replaced with:

"Hello {{name}}": "Bonjour {{name}}"

The rendered page would should "Bonjour John".

Adding translations through the system

The i18n module currently doesn't support adding translations via code, only by editing the translation files manually. However, we have added a pull request to the i18n module to provide this functionality (https://github.com/mashpie/i18n-node/pull/221).

When this is merged in, the process of adding translation via code will be as such:

var cat = thisHook.authPass.getCatalog([lang code]); //eg, 'fr'
cat['Hello {{name}}'] = 'Bonjour {{name}}';
thisHook.authPass.setCatalog([lang code]);
Clone this wiki locally