Skip to content

Commit

Permalink
[#48] JS: Check if window is defined in systemLocale
Browse files Browse the repository at this point in the history
  • Loading branch information
comahe-de committed Nov 16, 2023
1 parent 945eee8 commit b500e25
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
7 changes: 7 additions & 0 deletions i18n4k-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ kotlin {
}
}
}
// test in a nodeJS environment without browser
nodejs {
testTask {
useMocha {
}
}
}
}
// ##### native targets...
// # out commented targets are not supported by a used library
Expand Down
16 changes: 12 additions & 4 deletions i18n4k-core/src/jsMain/kotlin/de/comahe/i18n4k/Locale.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package de.comahe.i18n4k

import kotlinx.browser.window

actual data class Locale actual constructor(
val language: String,
val country: String,
Expand All @@ -23,5 +21,15 @@ actual data class Locale actual constructor(
override fun toString(): String = toTag()
}

actual val systemLocale: Locale =
forLocaleTag(window.navigator.language,"-")
actual val systemLocale: Locale = run {
var locale: Locale? = null
try {
if (jsTypeOf(kotlinx.browser.window) != "undefined")
locale = forLocaleTag(kotlinx.browser.window.navigator.language, "-")
} catch (ignore: Throwable) {
}
if (locale == null) {
locale = Locale("en")
}
return@run locale
}

0 comments on commit b500e25

Please sign in to comment.