This little library helps you to make multi-language applications.
Before you code something you need to create the language files. For this you need to use javas .properties
files, which
have a key - value structure. Here is an example of an english file.
startup=Hello World!
You can locate it anywhere in your resources
folder. If you create a subfolder (for example resources/languages/
) you
need to specify that in the Kotlingua
object. More on this here
Take a look at the Lang
enum. There you can find all languages, which all have an iso-code.
If you created a file for an English (british) translation search for the entry first. In this case it would
be Lang.ENGLISH_UNITED_KINGDOM
. As an argument you see the iso-code: en_GB
. Copy it and name your file like that. That's
it.
Before using any strings make sure to load your current languages!
fun main(ags: Array<String>) {
Kotlingua.loadLanguages()
}
Now to get a string get your language from the enum and call the get
method.
fun main(ags: Array<String>) {
Kotlingua.loadLanguages()
val startupString = Lang.ENGLISH_UNITED_KINGDOM.get("startup")
println(startupString)
}
Console output:
Hello World!
Using the builder you can set up more detailed things:
When adding custom folders in the resource
directory you need to specify this as well. In the example below the new folder
of the language files is in resources/languages/
.
fun main(ags: Array<String>) {
kotlingua {
directory = "languages/"
}.also { it.loadLanguages() }
}
If you're trying to get a string from a language, where the string key doesn't it normally throws a NullPointerException
.
With a default language you can prevent those exceptions by adding a default language. So if the key you're looking for
doesn't exist for the specified language, Kotlingua uses the value from the specified default language. Note that this can
be null too if the key doesn't exist there as well.
fun main(ags: Array<String>) {
kotlingua {
defaultLang = Lang.ENGLISH_UNITED_KINGDOM
}.also { it.loadLanguages() }
}