https://kotlinlang.org logo
Title
d

Dirk Hoffmann

01/23/2021, 4:54 PM
what's some easy lib/classes/onBoard support for I18n, any recommendations?
s

spechard

03/12/2021, 7:53 PM
Hi, did you find anything on that topic?
d

Dirk Hoffmann

03/12/2021, 8:10 PM
currently just using this helper class:
import java.util.*
import kotlin.collections.HashMap
import kotlin.collections.set

object I18N  {
    private val translationBundles = HashMap<Locale, ResourceBundle>()
//    private var currentLocale = Locale("de", "DE")
    private var currentLocale = Locale("en", "GB")
    private var currentTranslations: ResourceBundle

    init {
        currentTranslations = try {
            currentTranslations = ResourceBundle.getBundle("i18n.translations", currentLocale)
            translationBundles[currentLocale] = currentTranslations
            currentTranslations
        }
        catch (except: MissingResourceException) {
            throw Exception("translation properties file not found: src/main/resources/i18n/translations_${currentLocale.toString()}.properties")
        }
    }

    operator fun get(key: String): String {
        return T(key)
    }
    fun T(key: String): String {
        return try {
            if (true == currentTranslations.containsKey(key)) currentTranslations.getString(key) else key
        }
        catch (except: ClassCastException) {
            "ClassCastException:${key}"
        }
    }
    fun T(keys: List<String>): List<String> {
        return keys.map { T(it) }
    }
}
s

spechard

03/12/2021, 9:44 PM
Thanks, I’ll try it 👍