Dirk Hoffmann
01/23/2021, 4:54 PMspechard
03/12/2021, 7:53 PMDirk Hoffmann
03/12/2021, 8:10 PMimport 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) }
}
}
spechard
03/12/2021, 9:44 PM