如何在Swift中使NSBundle中的缓存无效 [英] How to invalidate cache from NSBundle in Swift
问题描述
当我尝试本地化字符串时,它会返回以前的值.我在此帖子中实际上必须使缓存无效.
When I try to localize a string it returns previous value. I found in this post that you actually have to invalidate the cache.
这或多或少是我尝试的代码.在 localizableStringsPath
内部,该文件实际上显示了我从inet下载的翻译,但bundle返回了先前的值.我必须关闭该应用程序,然后从 localizableStringsPath
捆绑返回以前的值.
More or less this is the code I tried. Inside localizableStringsPath
the file actually shows the translations I downloaded from inet, but bundle returns the previous value. I have to close the app, and then bundle return the previous value from 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)
}
推荐答案
将可本地化的文件另存为 Localizable.nocache.strings
而不是 Localizable.strings
.您可以在Apple文档中找到更多详细信息: developer.apple.com-资源编程指南-字符串资源
Save the localizable file as Localizable.nocache.strings
instead of Localizable.strings
.
You can find more details inside the apple documentation:
developer.apple.com - Resource Programming Guide - String Resources
-
您可以将下载的文件重命名为
Localizable.nocache.strings
,然后按如下所示对代码进行
You can rename the downloaded file to
Localizable.nocache.strings
then make edit to your code as the following:
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)
}
这篇关于如何在Swift中使NSBundle中的缓存无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!