On iPhone NSLocalizedString
returns the string in the language of the iPhone.
Is it possible to force NSLocalizedString
to use a specific language to have the app
in a different language than the device ?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
- How can I add media attachments to my push notific
I had the same problem recently and I didn't want to start and patch my entire NSLocalizedString nor force the app to restart for the new language to work. I wanted everything to work as-is.
My solution was to dynamically change the main bundle's class and load the appropriate bundle there:
Header file
Implementation
So basically, when your app starts and before you load your first controller, simply call:
When your user changes his preferred language in your setting screen, simply call it again:
To reset back to system defaults, simply pass nil:
Enjoy...
For those who need a Swift version:
As said earlier, just do:
But to avoid having to restart the app, put the line in the main method of
main.m
, just beforeUIApplicationMain
(...).Maybe you should complement with this (on .pch file after #import ):
You can do something like this:
As Brian Webster mentions, the language needs to be set "sometime early in your application's startup". I thought
applicationDidFinishLaunching:
of theAppDelegate
should be a suitable place to do it, since it's where I do all other initialization.But as William Denniss mentions, that seems to have an effect only after the app is restarted, which is kind of useless.
It seems to work fine if I put the code in the main function, though:
I'd appreciate any comments on this.
In a nutshell :
Localize your application
It's the first thing you have to do is to localise your app with at least two languages (english and french in this example).
Override NSLocalizedString
In your code, instead of using
NSLocalizedString(key, comment)
, use a macroMYLocalizedString(key, comment)
defined like this :#define MYLocalizedString(key, comment) [[MYLocalizationSystem sharedInstance] localizedStringForKey:(key) value:(comment)];
This
MYLocalizationSystem
singleton will :Set user language
When user changed application language in french, call
[[MYLocalizationSystem sharedInstance] setLanguage:@"fr"];
In this example this method set localized bundle to fr.lproj
Return localized string
Once you've set the localized bundle, you'll be able to get the right localised string from him with this method :
Hope this will help you.
You'll find more details in this article from NSWinery.io