Skip to content
This repository has been archived by the owner on Feb 17, 2023. It is now read-only.

Latest commit

 

History

History
41 lines (28 loc) · 2.11 KB

I18N.md

File metadata and controls

41 lines (28 loc) · 2.11 KB

I18N Module

When writing bot you usually need texts for messages and it is not cool to store them in your code. Also when you want to make your bot global, you usually need to localize your app and this module helps you with this too.

interface I18NTrait {
    var language: String
    fun pickLocale(supported: Array<String>, usersLocales: Array<String>): String
    fun initLocalize(fileName: String)
    fun initLocalize(name: String, supported: Array<String>, usersLocales: Array<String>)
    fun localized(key: String): String
}

Language Files

Language files are simple java .properties files, stored in resoruces directory of your project.

If you want to show one random text from finite number of text you can write them with random integer in the end and I18N library will rotate them for you

Example for message.hello string:

message.hello.0=Hello!
message.hello.1=Hi!
message.hello.2=Alloha!

Initialization

I18N module can run in two modes: localized and plain text. First one is localized version for each user and second one is just useful text source.

Localization support is enabled by calling fun initLocalize(name: String, supported: Array<String>, usersLocales: Array<String>), where name is base name of language files, supported is list of supported languages(lowcase) and userLocales - destination user locales.

Language files are simple java .properties files, stored in resoruces path of your project. You can see example in bot father strings. Naming convention for language files is <base_name>_<lang_code_capitalized>.properties for translations and <base_name>.properties for englis.

If you don't want to have localization support, just run fun initLocalize(fileName: String) with exact file name (with .properties extension).

Usage

Jsut call fun localized(key: String): String to get string. If string is not present exception is thrown.

In some cases it might be useful to know what locale was selected during init, then you can use language variable for this.