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 usually do this in this way, but you MUST have all localization files in your project.
NSLocalizedString()
reads the value for the keyAppleLanguages
from the standard user defaults ([NSUserDefaults standardUserDefaults]
). It uses that value to choose an appropriate localization among all existing localizations at runtime. When Apple builds the user defaults dictionary at app launch, they look up the preferred language(s) key in the system preferences and copy the value from there. This also explains for example why changing the language settings in OS X has no effect on running apps, only on apps started thereafter. Once copied, the value is not updated just because the settings change. That's why iOS restarts all apps if you change then language.However, all values of the user defaults dictionary can be overwritten by command line arguments. See
NSUserDefaults
documentation on theNSArgumentDomain
. This even includes those values that are loaded from the app preferences (.plist) file. This is really good to know if you want to change a value just once for testing.So if you want to change the language just for testing, you probably don't want to alter your code (if you forget to remove this code later on ...), instead tell Xcode to start your app with a command line parameters (e.g. use Spanish localization):
No need to touch your code at all. Just create different schemes for different languages and you can quickly start the app once in one language and once in another one by just switching the scheme.
What do you think about this solution for Swift 3?
Simple usage:
Swift 3 extensions:
Usage:
I like best Mauro Delrio's method. I also have added the following in my Project_Prefix.pch
So if you ever want to use the standard method (that uses NSLocalizedString) you can make a quick syntax substitution in all files.
I came up with a solution that allows you to use
NSLocalizedString
. I create a category ofNSBundle
callNSBundle+RunTimeLanguage
. The interface is like this.The implementation is like this.
Than just add import
NSBundle+RunTimeLanguage.h
into the files that useNSLocalizedString
.As you can see I store my languageCode in a property of
AppDelegate
. This could be stored anywhere you'd like.This only thing I don't like about it is a Warning that
NSLocalizedString
marco redefined. Perhaps someone could help me fix this part.