Internationalization
Last updated
Last updated
Nectar is multi-lingual. It supports Internationalization (i18n) and Right-to-Left languages such as Arabic, Hebrew etc.
It uses library for implementing i18n.
The translation files in JSON format are present inside /assets/custom/i18n/
folder.
By default, Internationalization (i18n) is enabled in Nectar.
The configuration variables for i18n are present in /assets/custom/js/config.js
file, inside window.config.i18n
object.
By default, the primary language for Nectar is set to English (en)
To change the primary language from English to any other language (suppose French), follow these steps:
Open index.html
and set <html lang="fr" dir="ltr">
In case, your primary language is RTL (suppose Arabic), then set <html lang="ar" dir="rtl">
Additionally, change: <link class="f7" rel="stylesheet" href="assets/vendor/framework7/framework7-bundle.min.css" />
to <link class="f7" rel="stylesheet" href="assets/vendor/framework7/framework7-bundle-rtl.min.css" />
Now, open /assets/custom/js/config.js
file.
Inside window.config.i18n = {
change defaultLanguage
from en
to fr
Go to /assets/custom/i18n/
and create a new folder fr
Inside /assets/custom/i18n/fr/
, create a file common.json
and add translation strings.
This way, you can set your Primary Language.
When your app is multi-lingual, you might want to provide a way for your users to switch languages. We have implemented one, and it is located at /partials/select-language.html
The list of available languages is populated from window.config.i18n.languages
The code which manages i18n in Nectar is present in /assets/custom/js/init.js
inside app.i18n = {
object.
app.i18n.initialize();
This code initializes the i18n library.
app.i18n.currentLanguage();
Using this, you can get the current language of the app.
app.i18n.changeLanguage(languageCode);
Using this, you can change the current language of the app. For example, app.i18n.changeLanguage('hi');
app.i18n.localize();
This code looks for all the data-i18n attributes in HTML elements and translate them. This is useful when you dynamically add elements in DOM.
app.i18n.translate(key, fallback, options);
This is used to translate string present in JSON translation files. For example, app.i18n.translate('app-title', 'Nectar', //i18next options);
If the current language is Hindi, then this will output '
नेक्टर'
. If app-title
key is not present, then the translation will fallback to 'Nectar'
.
languageChanged
EventWhenever the current language of the app is changed, languageChanged
event is emitted by the app, which you can listen to and perform required actions. For example,
Read the official documentation of i18next for advanced usage here