Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to add country/region-specific localizations #329

Closed
steviek opened this issue Apr 2, 2022 · 11 comments · Fixed by #356
Closed

Ability to add country/region-specific localizations #329

steviek opened this issue Apr 2, 2022 · 11 comments · Fixed by #356
Labels
enhancement New feature or request
Milestone

Comments

@steviek
Copy link

steviek commented Apr 2, 2022

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?

@Alex009 Alex009 added the enhancement New feature or request label Apr 29, 2022
@rustamsmax
Copy link

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

@rustamsmax
Copy link

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

@kevincianfarini
Copy link
Contributor

kevincianfarini commented Jun 21, 2022

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.

@Alex009
Copy link
Member

Alex009 commented Jun 23, 2022

hi @kevincianfarini
you can try to implement this feature and send PR.
i think changes required in this places:

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

@kevincianfarini
Copy link
Contributor

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

@kevincianfarini
Copy link
Contributor

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

Alex009 added a commit to kevincianfarini/moko-resources that referenced this issue Jul 24, 2022
Alex009 added a commit to kevincianfarini/moko-resources that referenced this issue Jul 24, 2022
Alex009 added a commit to kevincianfarini/moko-resources that referenced this issue Jul 24, 2022
Alex009 added a commit to kevincianfarini/moko-resources that referenced this issue Jul 24, 2022
Alex009 added a commit to kevincianfarini/moko-resources that referenced this issue Jul 25, 2022
@glureau
Copy link

glureau commented Mar 7, 2023

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

@Alex009 Alex009 added this to the 0.21.0 milestone Mar 12, 2023
Alex009 added a commit to kevincianfarini/moko-resources that referenced this issue Mar 24, 2023
Alex009 added a commit to kevincianfarini/moko-resources that referenced this issue Mar 24, 2023
Alex009 added a commit to kevincianfarini/moko-resources that referenced this issue Mar 24, 2023
@Alex009
Copy link
Member

Alex009 commented Mar 25, 2023

will be released in 0.21.0

@kaidotarma
Copy link

kaidotarma commented Aug 16, 2023

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

@glureau
Copy link

glureau commented Aug 16, 2023

@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
Copy link

kaidotarma commented Aug 16, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

6 participants