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

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

Open
zacharee opened this issue Jul 25, 2024 · 2 comments
Open

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

zacharee opened this issue Jul 25, 2024 · 2 comments

Comments

@zacharee
Copy link
Contributor

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

Alex009 commented Jul 25, 2024

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
Copy link
Contributor Author

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

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

No branches or pull requests

2 participants