Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,13 @@ creating `commonMain/moko-resources/ru` for a Russian localization:
</resources>
```

The directory name is a BCP 47 language tag, so it can also contain a region or a script,
e.g. `en-US` (or `en-rUS`), `sr-Latn`, `sr-Latn-RS` or `zh-Hans`. On Android a locale with a script
is generated into `values-b+sr+Latn` style directories, which use the
[BCP 47 resource qualifier](https://developer.android.com/guide/topics/resources/providing-resources#LocaleQualifier)
documented for Android 7.0 (API 24) and above. If you filter locales, use `b+sr+Latn` in Android
`resourceConfigurations` and `sr-Latn` in `CFBundleLocalizations`.

After adding the resources we can call a gradle sync or execute a gradle
task `generateMRcommonMain`. This will generate a `MR` class containing `MR.strings.my_string`,
which we can use in `commonMain`:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package dev.icerock.gradle.generator.localization

import java.util.ResourceBundle
import java.util.Locale as JvmLocale

internal sealed interface LanguageType {
Expand Down Expand Up @@ -31,30 +32,39 @@ internal sealed interface LanguageType {
override fun language(): String = languageTag

fun toBcpString(): String = jvmLocale.toLanguageTag()
private fun toLocaleString(): String = jvmLocale.toString()

override val androidResourcesDir: String = buildString {
append("values")
append("-")
append(jvmLocale.getISO639Language())
if (jvmLocale.country.isNotBlank()) {
append("-r")
append(jvmLocale.country)
if (jvmLocale.script.isBlank()) {
append(jvmLocale.getISO639Language())
if (jvmLocale.country.isNotBlank()) {
append("-r")
append(jvmLocale.country)
}
} else {
// Only the BCP 47 qualifier supports scripts, eg `values-b+sr+Latn+RS`
append("b+")
listOf(jvmLocale.getISO639Language(), jvmLocale.script, jvmLocale.country)
.filter { it.isNotBlank() }
.joinTo(this, separator = "+")
}
}

override val appleResourcesDir: String = "${toBcpString()}.lproj"
override val jsResourcesSuffix: String = "_${toBcpString()}"

// JVM ResourceBundle uses locale format, eg `en_US`, instead of BCP format
// JVM ResourceBundle uses locale format, eg `en_US` or `sr_Latn`, instead of BCP format
// like `en-US`.
override val jvmResourcesSuffix: String = "_${toLocaleString()}"
override val jvmResourcesSuffix: String = ResourceBundle.Control
.getControl(ResourceBundle.Control.FORMAT_DEFAULT)
.toBundleName("", jvmLocale)

/**
* Throw an error here so that we safeguard ourselves from implicitly calling `Local.toString`.
* You should always use the more explicit methods defined above.
*/
override fun toString(): String = error("Use toLocaleString or toBcpString instead!")
override fun toString(): String = error("Use toBcpString instead!")

override fun hashCode(): Int = jvmLocale.hashCode()
override fun equals(other: Any?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright 2026 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.gradle.generator.localization

import java.util.Locale
import kotlin.test.Test
import kotlin.test.assertEquals

class LanguageTypeTest {

@Test
fun `base language`() {
assertOutputs(
fileName = "base",
android = "values",
apple = "Base.lproj",
jvm = "",
js = ""
)
}

@Test
fun `language only`() {
assertOutputs(
fileName = "en",
android = "values-en",
apple = "en.lproj",
jvm = "_en",
js = "_en"
)
}

@Test
fun `language and region in android format`() {
assertOutputs(
fileName = "en-rUS",
android = "values-en-rUS",
apple = "en-US.lproj",
jvm = "_en_US",
js = "_en-US"
)
}

@Test
fun `language and region in bcp47 format`() {
assertOutputs(
fileName = "en-US",
android = "values-en-rUS",
apple = "en-US.lproj",
jvm = "_en_US",
js = "_en-US"
)
}

@Test
fun `language with legacy iso 639 code`() {
assertOutputs(
fileName = "he",
android = "values-iw",
apple = "he.lproj",
// JDK before 17 returns the legacy `iw` code for `he`
jvm = "_${Locale.forLanguageTag("he").language}",
js = "_he"
)
}

@Test
fun `language and script`() {
assertOutputs(
fileName = "sr-Latn",
android = "values-b+sr+Latn",
apple = "sr-Latn.lproj",
jvm = "_sr_Latn",
js = "_sr-Latn"
)
}

@Test
fun `language script and region`() {
assertOutputs(
fileName = "sr-Latn-RS",
android = "values-b+sr+Latn+RS",
apple = "sr-Latn-RS.lproj",
jvm = "_sr_Latn_RS",
js = "_sr-Latn-RS"
)
}

@Test
fun `language script and region in android format`() {
assertOutputs(
fileName = "sr-Latn-rRS",
android = "values-b+sr+Latn+RS",
apple = "sr-Latn-RS.lproj",
jvm = "_sr_Latn_RS",
js = "_sr-Latn-RS"
)
}

@Test
fun `chinese simplified script`() {
assertOutputs(
fileName = "zh-Hans",
android = "values-b+zh+Hans",
apple = "zh-Hans.lproj",
jvm = "_zh_Hans",
js = "_zh-Hans"
)
}

private fun assertOutputs(
fileName: String,
android: String,
apple: String,
jvm: String,
js: String
) {
val languageType = LanguageType.fromFileName(fileName)
assertEquals(expected = android, actual = languageType.androidResourcesDir, message = "android")
assertEquals(expected = apple, actual = languageType.appleResourcesDir, message = "apple")
assertEquals(expected = jvm, actual = languageType.jvmResourcesSuffix, message = "jvm")
assertEquals(expected = js, actual = languageType.jsResourcesSuffix, message = "js")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package dev.icerock.moko.resources.desc

import android.content.Context
import android.os.Build
import java.util.Locale

actual interface StringDesc {
Expand All @@ -21,23 +22,30 @@ actual interface StringDesc {
private val locale: String
) : LocaleType() {
override val systemLocale: Locale
get() {
val languageTagParts: List<String> = locale.split("-")
return when (languageTagParts.size) {
LANGUAGE -> Locale(languageTagParts[0])
LANGUAGE_AND_COUNTRY -> Locale(languageTagParts[0], languageTagParts[1])
LANGUAGE_AND_COUNTRY_AND_VARIANT -> Locale(
languageTagParts[0],
languageTagParts[1],
languageTagParts[2]
)

else -> throw IllegalArgumentException(
"Invalid language tag $locale which has more than three parts."
)
}
get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Locale.forLanguageTag(locale)
} else {
parseLegacyLocale()
}

// Locale.forLanguageTag is API 21+, and older devices can't load script resources anyway
private fun parseLegacyLocale(): Locale {
val languageTagParts: List<String> = locale.split("-")
return when (languageTagParts.size) {
LANGUAGE -> Locale(languageTagParts[0])
LANGUAGE_AND_COUNTRY -> Locale(languageTagParts[0], languageTagParts[1])
LANGUAGE_AND_COUNTRY_AND_VARIANT -> Locale(
languageTagParts[0],
languageTagParts[1],
languageTagParts[2]
)

else -> throw IllegalArgumentException(
"Invalid language tag $locale which has more than three parts."
)
}
}

private companion object {
private const val LANGUAGE = 1
private const val LANGUAGE_AND_COUNTRY = 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,19 @@ class AppleLocalizationBundleTests {
)
StringDesc.localeType = StringDesc.LocaleType.System
}

@Test
fun localizedStringWithScriptLocalizationCaseTest() {
val resource = StringResource(
resourceId = "noResultsFound",
bundle = NSBundle.bundleWithPath(NSBundle.mainBundle.bundlePath + "/tests.bundle")!!
)
StringDesc.localeType = StringDesc.LocaleType.Custom("sr-Latn")
val stringDesc = ResourceStringDesc(resource)
assertEquals(
expected = "Nisu pronađeni rezultati",
actual = stringDesc.localized()
)
StringDesc.localeType = StringDesc.LocaleType.System
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"noResultsFound" = "Nisu pronađeni rezultati";
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<plural name="chars_count">
<item quantity="one">%d znak</item>
<item quantity="few">%d znaka</item>
<item quantity="other">%d znakova</item>
</plural>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<string name="hello_world">Zdravo moko-resources svete</string>
</resources>
Loading