簡體   English   中英

如何在 Swift 中使來自 NSBundle 的緩存失效

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM