Skip to content

Latest commit

 

History

History
70 lines (52 loc) · 2.6 KB

i18n.md

File metadata and controls

70 lines (52 loc) · 2.6 KB

i18n

Backend

OpenPaaS uses the node-i18n module to apply locales to templates.

The default locale files are located under backend/i18n/locales. Module specific locales can be stored into a specific folder. For exemple if you have a "locales" folder and an index.js in yourAwesomeModule/backend/lib/i18n/ the index should contain at least :

module.exports = function(dependencies) {
  const i18n = dependencies('i18n');

  i18n.setDefaultConfiguration({ directory: __dirname + '/locales' });

  return i18n;
};

This code only register your translation into the default i18n registry and they will be avaiblale everywhere. However you can define a specific name for your module's tranlations with the dirName option. Check i18n-node documentation for more details.

The default options below can be overrided by the config/default.json configuration file. To achieve this, create a i18n element and defines new values: The values from the configuration file have higher priority than the default ones. For example, to define a new directory for locales, add this JSON to the default.json:

{
  "i18n": {
    "directory": "/opt/openpaas/locales"
  }
}

Note: The directory value can be relative to the root of the project or absolute.

Default configuration

{
  defaultLocale: 'en',
  locales: ['en', 'fr', 'vi'],
  directory: './backend/i18n/locales',
  multiDirectories: true,
  updateFiles: false,
  indent: '  ',
  extension: '.json',
  cookie: 'locale'
}

Frontend

For frontend translations OpenPaaS loads angular-translate and registers into it the backend i18n-node catalog. Note that this catalog is module agnostic meaning the dirName option you may have provided earlier for module specific locales will not be present in the frontend catalog.

There are 3 different ways of using angular-translate, refers to angular-translate document for more details:

  • Using filter (eg: {{ 'Hello' | translate }})
  • Using directive (eg: <span translate> TRANSLATION_ID </span>)
  • Using $translate service for more complicated cases. For example: a notification service that receives message input from several places.
$translate(keyword).then(function(translation) {
  notificationService(translation);
});

//for synchronous approach
var translation = $translate.instant(keyword);

If your translation include %s for interpolation, just pass an array as interpolation param (eg : {{ 'Hello %s' | translate:['World !'] }}).