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

Help to get localization to work on iOS #53

Closed
mediavrog opened this issue Apr 1, 2020 · 2 comments
Closed

Help to get localization to work on iOS #53

mediavrog opened this issue Apr 1, 2020 · 2 comments
Labels
question Further information is requested

Comments

@mediavrog
Copy link

mediavrog commented Apr 1, 2020

Hi, I setup the library according to the documentation in version 0.9.0 and kotlin 1.3.70.

Now on Android, everything works as expected and I can localize my strings. On iOS though, only the translation key is displayed.

shared lib build.gradle.kts

kotlin {
    //select iOS target platform depending on the Xcode environment variables
    val iOSTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
        if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
            ::iosArm64
        else
            ::iosX64

    iOSTarget("ios") {
        binaries {
            framework {
                baseName = "MyLibShared"
                export("dev.icerock.moko:resources:${Versions.Libs.MultiPlatform.mokoResources}")
            }
        }
    }
}
...
multiplatformResources {
    multiplatformResourcesPackage = "jp.mylib.shared"
}

I verified the framework is correctly created and contains the localized files:
Screen Shot 2020-04-01 at 11 59 11

I tried a coupl of approaches to get the string:

// in swift; does not work
Localizer().getMyString().localized() // displays "my_string"
// shared code in iosMain: Localizer object: fun getMyString(): StringDesc = StringDesc.Resource(MR.strings.my_string)

// in swift; does not work; unresolved StringDesc
StringDesc.Resource(MR.strings.my_string).localize()

// in swift; this works though, so the files are loaded
NSLocalizedString("my_string", tableName: nil, bundle: Bundle(identifier: "jp.mylib.shared.MyLibShared")!, value: "", comment: "") // displays "My String!"

Do you have an idea on what the issue could be? Any pointer?

@Alex009
Copy link
Member

Alex009 commented Apr 1, 2020

hello! thanks for feedback!

in your case you not properly configure export of library. Kotlin/Native require use in export function platform artifact. so you should use
export("dev.icerock.moko:resources-iosx64:${Versions.Libs.MultiPlatform.mokoResources}") on x64 and
export("dev.icerock.moko:resources-iosarm64:${Versions.Libs.MultiPlatform.mokoResources}") on Arm64

@Alex009 Alex009 added the question Further information is requested label Apr 1, 2020
@mediavrog
Copy link
Author

mediavrog commented Apr 1, 2020

Thank you for your quick reply. It worked like a charm!

Edit: For those looking for a copyandpaste solution

val libType = when(target.preset?.name) {
  "iosArm64" -> "-iosarm64"
  "iosX64" -> "-iosx64"
  else -> ""
}           
export("dev.icerock.moko:resources${libType}:${Versions.Libs.MultiPlatform.mokoResources}")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants