icerockdev / moko-resources

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

Is it possible to use moko resources with AppIntent on iOS? #750

Open zacharee opened 4 months ago

zacharee commented 4 months ago

I've been looking into adding Shortcuts integration to my app on iOS. The Apple documentation shows an example for AppIntent here:

struct OpenFavorites: AppIntent {

    static var title: LocalizedStringResource = "Open Favorite Trails"

    static var description = IntentDescription("Opens the app and goes to your favorite trails.")

    static var openAppWhenRun: Bool = true

    @MainActor
    func perform() async throws -> some IntentResult {
        navigationModel.selectedCollection = trailManager.favoritesCollection

        return .result()
    }

    @Dependency
    private var navigationModel: NavigationModel

    @Dependency
    private var trailManager: TrailDataManager
}

You're then supposed to add that struct to an AppShortcutsProvider implementation: https://developer.apple.com/documentation/appintents/appshortcutsprovider.

The problem I'm running into is that LocalizedStringResource is something that can only handle compile-time values, which means I can't use MR.strings().someString.desc().localized(). I did try passing the generated key someString, but it doesn't seem like the framework knows to look in non-main resource bundles.

Is there a way for moko resources to write its strings to the main resource bundle, or a way I can tell my iOS app to search other bundles when looking for localization keys?

Alex009 commented 4 months ago

hi. moko-resources not write strings into main bundle. it write strings and other resources to self bundle inside framework.

as i see LocalizedStringResource works on bundle base, so you can create LocalizedStringResource instance with data from moko's StringResource https://developer.apple.com/documentation/foundation/localizedstringresource/3988421-init https://github.com/icerockdev/moko-resources/blob/71095fc691d8769b581b1b48c489701f0fd2c344/resources/src/appleMain/kotlin/dev/icerock/moko/resources/StringResource.kt

let str: StringResource
LocalizedStringResource(
    String.LocalizationValue(str.resourceId),
    bundle: LocalizedStringResource.BundleDescription.atURL(str.bundle.bundleURL)
)

i think it should work

zacharee commented 4 months ago

I tried that too. It doesn't let you use that initializer for this.