i'm currently trying to add i18n support to a kotlin js project (no react).
return document.create.div {
span("mid-title") {
+i18n here (ResourceBundle.getBundle()?)
}....
So is there a convenient way that works with kotlin js?
Update:
fun loadJSON(callback: (response: String) -> Unit) {
val xobj = XMLHttpRequest()
xobj.overrideMimeType("application/json")
xobj.open("GET", "test.json", true)
xobj.onreadystatechange = {
if (xobj.readyState.toInt() == 4 && xobj.status.toInt() == 200) {
// Required use of an anonymous callback
// as .open() will NOT return a value but simply returns undefined in asynchronous mode
callback(xobj.responseText);
}
}
xobj.send(null)
}
Update 2:
Got it now
So like I wrote before i'm using javascript to load the file. After that I have the Json object where I can get the value by key. I've also added an overload to replace arguments in the string. Further I've added an MuatableStateFlow var to observe the loading state of the language. But keep in mind that this concept always needs to run on a server. Locally you get a CORS error
class LanguageSupport(private val folder: String = "", private val lang: String) {
private lateinit var currentLang: Json
private val languageMutableStateFlow = MutableStateFlow<LanguageSupportState>(
LanguageSupportState.LanguageInit
)
val languageStateFlow: StateFlow<LanguageSupportState> = languageMutableStateFlow
init {
loadJSON()
}
fun get(key: String): String {
val value = currentLang[key]
check(value != null) {
throw Exception("key not found to load language definition")
}
return if (value is String) {
value
} else {
throw Exception("value of key is not a string")
}
}
fun get(key: String, vararg placeholders: String): String {
val value = currentLang[key]
check(value != null) {
throw Exception("key not found to load language definition")
}
return if (value is String) {
var finalValue: String = value
for (item in placeholders) {
finalValue = finalValue.replaceFirst(PLACEHOLDER, item)
}
finalValue
} else {
throw Exception("value of key is not a string")
}
}
private fun loadJSON() {
val xobj = XMLHttpRequest()
xobj.overrideMimeType("application/json")
xobj.open("GET", "${folder}test_${lang}.json", true)
xobj.onreadystatechange = {
if (xobj.readyState.toInt() == 4 && xobj.status.toInt() == 200) {
// Required use of an anonymous callback
// as .open() will NOT return a value but simply returns undefined in asynchronous mode
currentLang = JSON.parse(xobj.responseText)
languageMutableStateFlow.value = LanguageSupportState.LanguageLoaded
}
}
xobj.send(null)
}
companion object {
const val PLACEHOLDER = "%s"
}
}
sealed class LanguageSupportState {
object LanguageLoaded : LanguageSupportState()
object LanguageError : LanguageSupportState()
object LanguageInit : LanguageSupportState()
}
And because I use Koin
single { (folder: String, language: String) ->
LanguageSupport(
folder = folder,
lang = language,
)
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments