Skip to content

Commit dc50f17

Browse files
committed
Add native targets
1 parent 24cef4d commit dc50f17

7 files changed

Lines changed: 44 additions & 0 deletions

File tree

build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ kotlin {
7575
}
7676
}
7777
}
78+
linuxArm64()
79+
linuxX64()
80+
macosArm64()
81+
mingwX64()
7882
tvosArm64()
7983
tvosSimulatorArm64()
8084
wasmJs {

src/appleMain/kotlin/nl/jacobras/humanreadable/i18n/SystemLanguageTag.apple.kt renamed to src/appleMain/kotlin/nl/jacobras/humanreadable/i18n/systemLanguageTag.apple.kt

File renamed without changes.

src/commonMain/kotlin/nl/jacobras/humanreadable/i18n/SystemLanguageTag.kt renamed to src/commonMain/kotlin/nl/jacobras/humanreadable/i18n/systemLanguageTag.kt

File renamed without changes.

src/jvmMain/kotlin/nl/jacobras/humanreadable/i18n/SystemLanguageTag.jvm.kt renamed to src/jvmMain/kotlin/nl/jacobras/humanreadable/i18n/systemLanguageTag.jvm.kt

File renamed without changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package nl.jacobras.humanreadable.i18n
2+
3+
import kotlinx.cinterop.ExperimentalForeignApi
4+
import kotlinx.cinterop.toKString
5+
import platform.posix.getenv
6+
7+
@OptIn(ExperimentalForeignApi::class)
8+
internal actual fun systemLanguageTag(): String {
9+
val envValue = getenv("LANGUAGE")?.toKString()?.substringBefore(':')
10+
?: getenv("LC_ALL")?.toKString()
11+
?: getenv("LANG")?.toKString()
12+
return envValue
13+
?.substringBefore('.') // Remove charset: "en_US.UTF-8" » "en_US"
14+
?.substringBefore('@') // Remove modifier: "sr_RS@latin" » "sr_RS"
15+
?.takeIf {
16+
// Apparently some distros may return "C" or "POSIX" if there is no locale data
17+
it.isNotBlank() && !it.equals("C", true) && !it.equals("POSIX", true)
18+
}
19+
?.replace('_', '-') ?: "en"
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package nl.jacobras.humanreadable.i18n
2+
3+
import kotlinx.cinterop.ExperimentalForeignApi
4+
import kotlinx.cinterop.allocArray
5+
import kotlinx.cinterop.memScoped
6+
import kotlinx.cinterop.toKStringFromUtf16
7+
import platform.windows.GetUserDefaultLocaleName
8+
import platform.windows.LOCALE_NAME_MAX_LENGTH
9+
import platform.windows.WCHARVar
10+
11+
@OptIn(ExperimentalForeignApi::class)
12+
internal actual fun systemLanguageTag(): String = memScoped {
13+
val buffer = allocArray<WCHARVar>(LOCALE_NAME_MAX_LENGTH)
14+
val length = GetUserDefaultLocaleName(buffer, LOCALE_NAME_MAX_LENGTH)
15+
return if (length > 0) {
16+
buffer.toKStringFromUtf16()
17+
} else {
18+
"en"
19+
}
20+
}

src/webMain/kotlin/nl/jacobras/humanreadable/i18n/SystemLanguageTag.web.kt renamed to src/webMain/kotlin/nl/jacobras/humanreadable/i18n/systemLanguageTag.web.kt

File renamed without changes.

0 commit comments

Comments
 (0)