如何使用 kotlin js 支持 i18n
How to support i18n with kotlin js
我目前正在尝试向 kotlin js 项目添加 i18n 支持(无反应)。
return document.create.div {
span("mid-title") {
+i18n here (ResourceBundle.getBundle()?)
}....
- 添加了资源包文件(strings.properties、strings_en.properties)
- 试图通过 ResourceBundle.getBundle 获取它们 <- kotlin 不知道如何使用
- 找到了 github 但不喜欢那种方法
那么kotlin js有没有方便的方法呢?
更新:
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)
}
更新 2:
知道了
就像我在使用 javascript 加载文件之前写的那样。之后我有了 Json 对象,我可以在其中按键获取值。我还添加了一个重载来替换字符串中的参数。此外,我添加了一个 MuatableStateFlow var 来观察语言的加载状态。但请记住,此概念始终需要在服务器上 运行。在本地你会收到一个 CORS 错误
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()
}
而且因为我使用 Koin
single { (folder: String, language: String) ->
LanguageSupport(
folder = folder,
lang = language,
)
}
我目前正在尝试向 kotlin js 项目添加 i18n 支持(无反应)。
return document.create.div {
span("mid-title") {
+i18n here (ResourceBundle.getBundle()?)
}....
- 添加了资源包文件(strings.properties、strings_en.properties)
- 试图通过 ResourceBundle.getBundle 获取它们 <- kotlin 不知道如何使用
- 找到了 github 但不喜欢那种方法
那么kotlin js有没有方便的方法呢?
更新:
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)
}
更新 2:
知道了
就像我在使用 javascript 加载文件之前写的那样。之后我有了 Json 对象,我可以在其中按键获取值。我还添加了一个重载来替换字符串中的参数。此外,我添加了一个 MuatableStateFlow var 来观察语言的加载状态。但请记住,此概念始终需要在服务器上 运行。在本地你会收到一个 CORS 错误
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()
}
而且因为我使用 Koin
single { (folder: String, language: String) ->
LanguageSupport(
folder = folder,
lang = language,
)
}