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
You could build a sub-bundle with the set of localized strings that you want to do this with, and then use
NSLocalizedStringFromTableInBundle()
to load them. (I'm assuming that this is content separate from the normal UI localization you might be doing on the app.)NSLocalizedString()
(and variants thereof) access the "AppleLanguages" key inNSUserDefaults
to determine what the user's settings for preferred languages are. This returns an array of language codes, with the first one being the one set by the user for their phone, and the subsequent ones used as fallbacks if a resource is not available in the preferred language. (on the desktop, the user can specify multiple languages with a custom ordering in System Preferences)You can override the global setting for your own application if you wish by using the setObject:forKey: method to set your own language list. This will take precedence over the globally set value and be returned to any code in your application that is performing localization. The code for this would look something like:
This would make German the preferred language for your application, with English and French as fallbacks. You would want to call this sometime early in your application's startup. You can read more about language/locale preferences here: Internationalization Programming Topics: Getting the Current Language and Locale
for my case i have two localized file , ja and en
and i would like to force it to en if the preferred language in the system neither en or ja
i'm going to edit the main.m file
i 'll check whether the first preferred is en or ja , if not then i 'll change the second preferred language to en.
Here is a decent solution for this problem, and it does not require application restart.
https://github.com/cmaftuleac/BundleLocalization
This implementation works by tweaking inside NSBundle. The idea is that you override the method localizedStringForKey on the instance of NSBundle object, and then call this method on a different bundle with a different language. Simple and elegant fully compatible with all types of resources.
Swift 3 solution:
Gave some examples of language codes that can be used. Hope this helps
I wanted to add support for a language that isn't officially supported by iOS (not listed in Language section under system settings). By following the Apple's Internationalization Tutorial and few hints here by Brian Webster and geon, I came up with this piece of code (put it in main.m):
This works well for both Storyboard and NSLocalizedString code. The code assumes that user will have an option to manually change language inside app later on.
Of course, don't forget to add proper Storyboard translations and Localizable.strings translations (see link to Apple page above for how to do that).