icerockdev / moko-resources

Resources access for mobile (android & ios) Kotlin Multiplatform development
https://moko.icerock.dev/
Apache License 2.0
1.03k stars 118 forks source link

Localization unit tests in Xcode #513

Open zbencz3 opened 1 year ago

zbencz3 commented 1 year ago

I was wondering what would be the correct approach when using the moko-resources library to add unit tests to the iOS project?

An approach that usually works on a iOS only project would be to specify the bundle:

func testFrenchLocalization() {
        let bundle = Bundle(for: type(of: self))
        let frenchLanguage = "fr"
        let path = bundle.path(forResource: frenchLanguage, ofType: "lproj")
        let frenchBundle = Bundle(path: path!)
        let localizedString = NSLocalizedString("hello_key", bundle: frenchBundle!, comment: "")
        XCTAssertEqual(localizedString, "Bonjour")
    }

With the KMM/moko-resources setup I think there is no lproj file. So that approach would not work.

Another approach would be to set the App Language and App Region in the scheme, but then again we need to switch languages multiple times.

Testing the translations in the common shared code is likely enough, but I wonder if there is a good way to still add safety tests.

Thanks!

Alex009 commented 1 year ago

hi. in sample you can check tests with different locales. https://github.com/icerockdev/moko-resources/blob/master/samples/resources-gallery/mpp-library/src/commonTest/kotlin/com/icerock/library/StringResourceEnTests.kt

it works base on

https://github.com/icerockdev/moko-resources/blob/beb3fb29dc78d4c75109ce94a1aab3abb38dae5c/resources/src/commonMain/kotlin/dev/icerock/moko/resources/desc/StringDesc.kt#L29

also you can use same way as you do in sample code - moko-resources generates native resources, so in generated bundle will be contained lproj for different languages. But you should understand that you can't use bundle of your application - moko-resources generates own bundles. and to use this bundles you can call MR.bundle property - that property will return bundle that contains resources of this MR class