moko-resources icon indicating copy to clipboard operation
moko-resources copied to clipboard

Ability to add country/region-specific localizations

Open steviek opened this issue 3 years ago • 6 comments

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?

steviek avatar Apr 02 '22 14:04 steviek

There is an issue when specified region. I think this must be fixed in upcoming releases

rsktash avatar May 01 '22 11:05 rsktash

telegram-cloud-photo-size-2-5438105058836331128-y

rsktash avatar May 01 '22 12:05 rsktash

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.

kevincianfarini avatar Jun 21 '22 19:06 kevincianfarini

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

Alex009 avatar Jun 23 '22 07:06 Alex009

Done! @Alex009. Unfortunately it was a bit more complicated than that, though.

kevincianfarini avatar Jun 23 '22 18:06 kevincianfarini

@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.

kevincianfarini avatar Jun 27 '22 17:06 kevincianfarini

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"))
                }
            }
        }
    }
}

glureau avatar Mar 07 '23 17:03 glureau

will be released in 0.21.0

Alex009 avatar Mar 25 '23 07:03 Alex009

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 avatar Aug 16 '23 14:08 kaidotarma

@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.)

glureau avatar Aug 16 '23 14:08 glureau

@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.

kaidotarma avatar Aug 16 '23 15:08 kaidotarma