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)
}
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With