Table of contents
Internationalization (i18n) is the process of developing products in such a way that they can be localized for languages and cultures easily. OpenPaaS supports i18n completely in both backend and frontend sides:
- In backend, we use i18n-node
- In frontend, we use angular-translate
Where are the locale files?
Locale files of the core module can be found in backend/i18n/locales
directory.
Each module can have its own locale files, usually placed in backend/lib/i18n/locales
directory as in Calendar
module for example.
Currently, supported languages are English, French and Vietnamese.
Backend translation
When to use:
- Translate static string in .pug files
- Translate string in Node.js files, for example: translate an email before sending it to recipients.
Translate static string in .pug files
It is pretty easy to translate phrases in .pug files:
h2 #{__('Good morning!')}
Simple string interpolation:
h2 #{__('Hello %s, good morning!', 'John')}
Angular expression string interpolation:
h2 #{__('Hello %s, good morning!', '{{user.name}}')}
HTML string interpolation:
p.empty-text !{__('No application, click %s to add a new one', '<span class="mdi mdi-plus"></span>')}
Translate string in Node.js files
Simply require the module in backend/core/i18n
:
const i18n = require('...');
const subject = i18n.__('Your password has been changed!');
Check the documentation of i18n-node for more examples.
Frontend translation
When to use:
- Translate Angular variable in .pug files
- Translate string in Javascript files
You can either use esnI18n
filter or esnI18nService
of esn.i18n
module to
translate your phrases.
Translate Angular variable in .pug files
span {{mailbox.name | esnI18n}}
Translate string in Javascript files
// inject the esnI18nService service
var subject = esnI18nService.translate('Email sent to %s', $scope.recipient.email).toString();
The esnI18nService.translate
function returns an instance of EsnI18nString
, which
will be useful when you want to know whether the string is translated or not:
function notify(message) {
message = esnI18nService.isI18nString(message) ? message.toString() : esnI18nService.translate(message).toString();
window.alert(message);
}
// Usage
notify('Hello world!');
// OR
notify(esnI18nService.translate('Hello %s!', user.name));
Translation validator
OpenPaaS uses a built-in validator based on i18n-checker package to ensure the quality of internationalization in product.
To use the validator, simply run: grunt i18n