Internationalization
Features
Automatic language detection (based on the browser language settings).
Dynamic language change.
Multi-language support for built-in advanced components:
rich date and time picker - 26 languages
rich text editor - 2 languages
Minimal impact of internationalization process on the application source code.
Automatic extraction of text for translation from the application code.
Support for well known translation files format (gettext
*.po
files).
Requirements
Apply kotlinx-gettext
Gradle plugin and add kilua-i18n
module to your dependencies.
Using multi-language text in the application sources
To mark some text for translation use one of the methods of the dev.kilua.i18n.I18n
class instead of plain string literals. You can start your work with an empty instance of the class.
There are a few different methods in the I18n
class, which can be used for singular and plural forms in different contexts.
tr()
Translate a singular form
trn()
Translate plural forms
trc()
Translate a singular form with a given context
trnc()
Translate plural forms with a given context
marktr()
Mark text for translation
These methods are @Composable
, and they are bound to the application current locale. When the locale changes, all methods using translated texts are recomposed.
This is an example of multi-language code with singular and plural forms.
Translation files
The whole idea of gettext translations is that the translation key is the actual text in the primary language. Until you create and initialize translation files for some other language, your application will work correctly just by using 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 pass the translation files as constructor parameters of the I18n
class.
Changing the application locale
The current locale is stored in the LocaleManager.currentLocale
property. It is initialized with the default locale from the browser language settings. It can be changed at any time using LocaleManager.setCurrentLocale()
method.
Last updated