Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invalidate cache from NSBundle in Swift

Tags:

ios

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.

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)
}
like image 426
Godfather Avatar asked Oct 23 '25 14:10

Godfather


1 Answers

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

  • 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)
    }
    
like image 137
Hossam Fathy Avatar answered Oct 26 '25 03:10

Hossam Fathy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!