Ability to add country/region-specific localizations
Is it possible to provide different strings by region? E.g. distinguishing en_US and en_GB? If so, can you add an example to the docs?
There is an issue when specified region. I think this must be fixed in upcoming releases

Is there anything I can do to help push this forward? I'm in need of differentiating between en-US and en-GB as well.
hi @kevincianfarini you can try to implement this feature and send PR. i think changes required in this places:
- https://github.com/icerockdev/moko-resources/blob/c59420bee59e99f6d0481226db86cdc4661c91df/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/android/AndroidStringsGenerator.kt#L43
- https://github.com/icerockdev/moko-resources/blob/c59420bee59e99f6d0481226db86cdc4661c91df/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/android/AndroidPluralsGenerator.kt#L43
- https://github.com/icerockdev/moko-resources/blob/c59420bee59e99f6d0481226db86cdc4661c91df/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/apple/AppleStringsGenerator.kt#L38
- https://github.com/icerockdev/moko-resources/blob/c59420bee59e99f6d0481226db86cdc4661c91df/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/apple/ApplePluralsGenerator.kt#L81
and maybe it's already works? as i see full directory name used as language name so if you create MR/en-US/strings.xml it's should create en-US resources
Done! @Alex009. Unfortunately it was a bit more complicated than that, though.
@Alex009 If you have some time to chat (either via email, Slack) I'd like to pick your brain on how we can proceed with #356. Android is expressing different behavior than every other platform, and I'd like to formulate a plan forward that you're happy with.
Not sure about the progress so I share my current workaround (a simple file renaming via gradle) if ever it can help someone.
task("patchMokoStringLocalizationsForAndroid") {
afterEvaluate {
project.tasks.forEach {
if (it.name.endsWith("generateDebugResValues")) {
it.finalizedBy("patchMokoStringLocalizationsForAndroid")
}
}
}
doLast {
val resDir = File(buildDir.absolutePath + "/generated/moko/androidMain/res")
if (resDir.exists()) { // No android resources -> nothing to do
val regionSpecificLocalizationRegex = Regex("^values-([a-z]+)-([A-Z]+)$")
resDir.listFiles()?.forEach {
val matchResult = regionSpecificLocalizationRegex.find(it.name)
if (matchResult != null) {
val newDirName = "values-${matchResult.groupValues[1]}-r${matchResult.groupValues[2]}"
it.renameTo(File(it.parent + "/$newDirName"))
}
}
}
}
}
will be released in 0.21.0
Not sure about the progress so I share my current workaround (a simple file renaming via gradle) if ever it can help someone.
task("patchMokoStringLocalizationsForAndroid") { afterEvaluate { project.tasks.forEach { if (it.name.endsWith("generateDebugResValues")) { it.finalizedBy("patchMokoStringLocalizationsForAndroid") } } } doLast { val resDir = File(buildDir.absolutePath + "/generated/moko/androidMain/res") if (resDir.exists()) { // No android resources -> nothing to do val regionSpecificLocalizationRegex = Regex("^values-([a-z]+)-([A-Z]+)$") resDir.listFiles()?.forEach { val matchResult = regionSpecificLocalizationRegex.find(it.name) if (matchResult != null) { val newDirName = "values-${matchResult.groupValues[1]}-r${matchResult.groupValues[2]}" it.renameTo(File(it.parent + "/$newDirName")) } } } } }
This was very helpful actually in case you have language-region based translations.
I ended up with declaring the translation files as usual en-XX, etc (without the -r), so it would work in iOS nicely (and pass app store review as well)..
And then added the following to build.gradle.kts file:
project.afterEvaluate {
val pattern = Regex("generate.*ResValues")
tasks.configureEach {
val task = this
if (task.name.matches(pattern)) {
task.finalizedBy("patchMokoStringLocalizationsForAndroid")
}
}
}
tasks.register("patchMokoStringLocalizationsForAndroid") {
doLast {
val resDir = File(rootDir.absolutePath + "/shared/build/generated/moko/androidMain/res")
if (resDir.exists()) {
val regionSpecificLocalizationRegex = Regex("^values-([a-z]+)-([A-Z]+)$")
resDir.listFiles()?.forEach {
val matchResult = regionSpecificLocalizationRegex.find(it.name)
if (matchResult != null) {
val newDirName = "values-${matchResult.groupValues[1]}-r${matchResult.groupValues[2]}"
it.renameTo(File(it.parent + "/$newDirName"))
}
}
} else {
throw (RuntimeException("Cannot run patchMokoStringLocalizationsForAndroid as $resDir does not exist"))
}
}
}
@kaidotarma glad to hear, but did you get an issue with 0.21.0? On my side I was able to remove this workaround after the upgrade. (If you still have an error with higher version, you may want to open an issue for the moko team to be able to help.)
@kaidotarma glad to hear, but did you get an issue with 0.21.0? On my side I was able to remove this workaround after the upgrade. (If you still have an error with higher version, you may want to open an issue for the moko team to be able to help.)
You are correct! My library version was not up to date in all places. Thanks for pointing this out! Now I can also remove this hack.