1

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)
}
Community
  • 1
  • 1
Godfather
  • 4,040
  • 6
  • 43
  • 70
  • What do you mean by "it returns previous value"? The question that you linked to is about a very unusual requirement of that poster and isn't likely to apply to you. Can you specify more concretely what you're trying to do, what you expect to happen and what's actually happening? Please share some code. – Dave Weston Jan 16 '17 at 19:58
  • I need to download a file with remote localized strings and apply them in my app. So when the app launches, i download the file, add to documentsDictory, then i create a bundle with the previous path, and get the translations. But i need to close and relaunch the app to get the actual data since those values are cached. Read the link i posted. – Godfather Jan 16 '17 at 20:29
  • 1
    Ok, I think I understand the issue. Can you provide the code that you've tried so far? Are you using NSLocalizedStringFromTableInBundle? – Dave Weston Jan 18 '17 at 03:50
  • I added some code to explain what i'm trying to do. @DaveWeston – Godfather Jan 18 '17 at 13:31
  • And does that work after you restart the app? I don't know what you mean by "previous value" that you mention twice. The documents directory shouldn't be a valid bundle, you don't have any `.lproj` directories so I'm confused how that would work. – Dave Weston Jan 18 '17 at 16:27
  • why you say a valid path is not valid for create a new Bundle? – Godfather Jan 18 '17 at 16:37
  • A bundle is a set of files in a directory organized in a certain way. `localizedStringForKey` (and all the other instance methods on the Bundle class) look in a predetermined location within that directory to return the requested information. If you want to download localized strings from your server, you need to recreate the expected directory structure somehow. I'll find some time to build a simple example today. – Dave Weston Jan 18 '17 at 16:52

1 Answers1

2

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)
    }
    
  • 2
    Note, you can't create a bundle with a file like this. Create a bundle in the folder that contains it, and use `table: "Localizable.nocache"` instead – netdigger Apr 15 '19 at 06:52