[英]Is there a way to invalidate NSBundle localization cache, withour restarting application? [iOS]
[英]How to invalidate cache from NSBundle in Swift
當我嘗試本地化一個字符串時,它返回以前的值。 我在這篇文章中發現您實際上必須使緩存無效。
或多或少這是我試過的代碼。 在localizableStringsPath
該文件實際上顯示了我從 inet 下載的翻譯,但 bundle 返回以前的值。 我必須關閉應用程序,然后 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)
}
將本地化文件保存為Localizable.nocache.strings
而不是Localizable.strings
。 您可以在蘋果文檔中找到更多詳細信息: developer.apple.com - 資源編程指南 - 字符串資源
您可以將下載的文件重命名為Localizable.nocache.strings
然后對您的代碼進行如下編輯:
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) }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.