Search code examples
iosswiftlocalized

Reading Localized String


I have an app that I'd like to be able to get the localized string for a certain key, regardless of what my current localization on the iPhone is.

I have the following in an en.lproj localized strings file

"Black" = "Black";

In the es.lproj localized strings file I have

"Black" = "Negro";

So what I would like to do is get the Spanish string when my phone is in the US and set up accordingly

I'm using the following code:

let bpath:String = NSBundle.mainBundle().pathForResource("es", ofType: "lproj")! as String
let bundle = NSBundle(path: bpath as String)
let thisWord="Black"
let ourWord=NSLocalizedString(thisWord, bundle: bundle!, comment: "")

I'm expecting to get "Negro" in the value for ourWord, but I always get "Black"

Am I missing something obvious?


Solution

  • Actually my code works fine. I had made the stupid mistake of not setting the "Localizable.strings" file as localized.

    Feeling pretty dumb right now