简体   繁体   中英

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?

Localization settings apply based on the settings you set in your phone not the location. If your phone is setup to display an Application Language of Spanish then you will see the appropriate string. To configure Application Language settings so you can test this you need to edit your Scheme.

In Xcode 7.1+ Goto Product > Scheme > Edit Scheme

Then change the Application Language and/or Region to simulate what a user would see who has an iPhone configured for a region or language other than English in the US.

(Switch it to Spanish and then debug the app on the device again to see the updated localized strings) .

1

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM