如何在Swift中使NSBundle中的缓存无效

时间:2017-01-16 17:15:01

标签: ios swift

当我尝试本地化字符串时,它返回先前的值。我在this post中发现你实际上必须使缓存无效。

或多或少这是我试过的代码。在localizableStringsPath内,文件实际显示我从inet下载的翻译,但bundle返回之前的值。我必须关闭应用,然后捆绑从localizableStringsPath返回先前的值。

func translateFromInet(key: String) -> String {
    let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first ?? ""
    let localizableStringsPath = documentsPath + "/Localizable.strings" // This file is downloaded from the inet with custom translations
    let bundle = Bundle(path: documentsPath)
    return bundle!.localizedString(forKey: key, value: nil, table: nil)
}

1 个答案:

答案 0 :(得分:1)

将可本地化的文件保存为Localizable.strings而不是Localizable.nocache.strings。 您可以在Apple文档中找到更多详细信息: developer.apple.com - Resource Programming Guide - String Resources

  • 您可以将下载的文件重命名为func translateFromInet(key: String) -> String { let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first ?? "" let localizableStringsPath = documentsPath + "/Localizable.nocache.strings" // This file is downloaded from the inet with custom translations let bundle = Bundle(path: documentsPath) return bundle!.localizedString(forKey: key, value: nil, table: nil) } ,然后按以下方式编辑代码:

    Convention Plugin