Internationalization
Features
Automatic language detection (based on the browser language settings).
Dynamic language change with automatic re-rendering of the whole application GUI.
Multi-language support for built-in complex components:
datetime control - 46 languages
select control - 12 languages
upload control - 39 languages
richtext editor - 2 languages
Contributions are welcomed.
Minimal impact of internationalization process on the application source code.
Automatic extraction of text to be translated from the application code.
Support for well known translation files format (gettext
*.po
files).
Requirements
To create a multi-language application you have to add the kvision-i18n
module as a dependency for your project.
Using multi-language text in application sources
To mark some text for translation just use one of the four available helper methods from the io.kvision.i18n.I18n
object instead of plain string literals.
Method
Description
tr
Dynamic translation for singular form.
ntr
Dynamic translation for plural forms.
gettext
Static translation for singular form.
ngettext
Static translation for plural forms.
Dynamic translations are bound to the components they are part of (tag content, button text etc.). They are dynamically changed when the current language for the application is changed.
Static translations are evaluated only when the helper method is called. They can be used to translate text not bound to any KVision component.
KVision has support for plural language forms, so you can use ntr
or ngettext
methods when plural forms are necessary.
The gettext
and ngettext
methods allow you to use additional runtime parameters, using gettext.js substitution mechanism and the following format of translation files:
Use this translation in your application like this:
Translation files
Until you create and initialize translation files for some other language, your application will use string literals used in the source code. It's probably a good practice to use English literals in your code and other languages in the translation files.
To generate basic translation files run the command:
Initializing translations
To initialize translations for one or more languages, you need to initialize the I18n.manager
property during application initialization (preferably in the start
method of your Application
object). You should add all supported languages to the map given as the parameter to the DefaultI18nManager
constructor.
Changing the current language
The current language is bound to the I18n.language
property. This property is initialized with the default country code from the browser language settings. It can be changed at any time from your code.
Last updated