简体   繁体   English

无法从应用扩展访问本地化资源

[英]Cannot access localized resource from app extension

I'm having trouble accessing localized resources from my Intents extension.我无法从我的 Intents 扩展访问本地化资源。 I've got a couple of JSON files that are localized into different languages in a framework that's shared between my app and the extension.我有几个 JSON 文件,它们在我的应用程序和扩展程序之间共享的框架中本地化为不同的语言。 When I access them though my app via path(forResource:ofType) , it accesses the proper localized version, but if I call it from the extension in the Shortcuts app, then it returns the English, even if both the app itself and the Shortcuts app are both set to another language.当我通过我的应用程序通过path(forResource:ofType)访问它们时,它会访问正确的本地化版本,但是如果我从快捷方式应用程序的扩展程序中调用它,那么它会返回英语,即使应用程序本身和快捷方式也是如此应用程序都设置为另一种语言。

The only way I can get the correct language to appear is if I change the device language - changing it just for the app or for Shortcuts doesn't have an effect.我可以显示正确语言的唯一方法是更改​​设备语言 - 仅针对应用程序或快捷方式更改它不会产生任何效果。

I'm building with the iOS 14 SDK, but the same behavior occurs on both iOS 13 and 14.我正在使用 iOS 14 SDK 进行构建,但在 iOS 13 和 14 上都会发生相同的行为。

Firstly, please check Target Membership of Localizable file, make sure it was checked your extension.首先,请检查可本地化文件的目标成员身份,确保已检查您的扩展名。

目标会员

Secondly, save your app language in UserDefaults which have AppGroups.其次,将您的应用程序语言保存在具有 AppGroups 的 UserDefaults 中。

static func setLanguageExtension(_ language: String, forKey: String) {
        if let userDefault = UserDefaults(suiteName: "APP_LANGUAGE") {
            userDefault.setValue(language, forKey: forKey)
        }
    }
    
    static func getLanguageExtension(key: String) -> String? {
        if let userDefault = UserDefaults(suiteName: "APP_LANGUAGE") {
            return userDefault.string(forKey: key)
        } else {
            return "en-US"
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM