mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-11-09 08:30:26 +00:00
add getCurrentLocale and compose translation
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
package dev.inmo.micro_utils.language_codes
|
||||
|
||||
expect val currentIetfLang: IetfLang?
|
||||
6
language_codes/src/jsMain/kotlin/CurrentIetfLang.js.kt
Normal file
6
language_codes/src/jsMain/kotlin/CurrentIetfLang.js.kt
Normal file
@@ -0,0 +1,6 @@
|
||||
package dev.inmo.micro_utils.language_codes
|
||||
|
||||
import kotlinx.browser.window
|
||||
|
||||
actual val currentIetfLang: IetfLang?
|
||||
get() = window.navigator.language.unsafeCast<String?>() ?.let { IetfLang(it) }
|
||||
6
language_codes/src/jvmMain/kotlin/CurrentIetfLang.jvm.kt
Normal file
6
language_codes/src/jvmMain/kotlin/CurrentIetfLang.jvm.kt
Normal file
@@ -0,0 +1,6 @@
|
||||
package dev.inmo.micro_utils.language_codes
|
||||
|
||||
import java.util.Locale
|
||||
|
||||
actual val currentIetfLang: IetfLang?
|
||||
get() = Locale.getDefault() ?.toIetfLang()
|
||||
@@ -0,0 +1,27 @@
|
||||
package dev.inmo.micro_utils.language_codes
|
||||
|
||||
import dev.inmo.micro_utils.language_codes.IetfLang
|
||||
import kotlinx.cinterop.ExperimentalForeignApi
|
||||
import kotlinx.cinterop.allocArray
|
||||
import kotlinx.cinterop.memScoped
|
||||
import kotlinx.cinterop.toKString
|
||||
import platform.posix.getenv
|
||||
import platform.windows.GetUserDefaultLocaleName
|
||||
import platform.windows.LOCALE_NAME_MAX_LENGTH
|
||||
import platform.windows.WCHARVar
|
||||
|
||||
@OptIn(ExperimentalForeignApi::class)
|
||||
actual val currentIetfLang: IetfLang?
|
||||
get() {
|
||||
val rawLocale = memScoped {
|
||||
val buffer = allocArray<WCHARVar>(LOCALE_NAME_MAX_LENGTH)
|
||||
val result = GetUserDefaultLocaleName(buffer, LOCALE_NAME_MAX_LENGTH)
|
||||
|
||||
if (result > 0) {
|
||||
// Convert WCHAR* to String
|
||||
buffer.toKString()
|
||||
}
|
||||
"en-US" // fallback
|
||||
}
|
||||
return IetfLang(rawLocale)
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package dev.inmo.micro_utils.language_codes
|
||||
|
||||
import kotlinx.cinterop.ExperimentalForeignApi
|
||||
import kotlinx.cinterop.toKString
|
||||
import platform.posix.getenv
|
||||
|
||||
@OptIn(ExperimentalForeignApi::class)
|
||||
actual val currentIetfLang: IetfLang?
|
||||
get() {
|
||||
val localeStr = getenv("LANG") ?.toKString() ?.replace("_", "-") ?: "en-US"
|
||||
return IetfLang(localeStr)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package dev.inmo.micro_utils.language_codes
|
||||
|
||||
external interface Navigator {
|
||||
val language: String
|
||||
}
|
||||
|
||||
external val navigator: Navigator
|
||||
|
||||
actual val currentIetfLang: IetfLang?
|
||||
get() = IetfLang(navigator.language)
|
||||
Reference in New Issue
Block a user