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 ?
http://stackoverflow.com/questions/1669645/how-to-force-nslocalizedstring-to-use-a-specific-language?lq=1
| |||
add a comment
|
177
|
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 | ||||||||||||||||||||
|
111
|
I usually do this in this way, but you MUST have all localization files in your project.
| ||||||||||||||||||||
|
82
|
I had the same problem recently and I didn't want to start and patch my entire NSLocalizedStringnor 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... | ||||||||||||||||||||
|
36
|
Do not use on iOS 9. This returns nil for all strings passed through it. I have found another solution that allows you to update the language strings, w/o restarting the app and compatible with genstrings: Put this macro in the Prefix.pch:
and where ever you need a localized string use:
To set the language use:
Works even with consecutive language hopping like:
| ||||||||||||||||||||
|
30
|
As said earlier, just do:
But to avoid having to restart the app, put the line in the main method of | ||||||||||||||||||||
|
30
|
The trick to use specific language by selecting it from the app is to force the here is the post i have written for this learning advance localization in ios apps and here is the code of one sample app advance localization in ios apps | ||||||||
|
11
|
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. | ||||
|
9
|
As Brian Webster mentions, the language needs to be set "sometime early in your application's startup". I thought 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. | ||||||||||||||||
|
7
|
I came up with a solution that allows you to use
The implementation is like this.
Than just add import As you can see I store my languageCode in a property of This only thing I don't like about it is a Warning that | ||||||||||||||||
|
5
|
However, all values of the user defaults dictionary can be overwritten by command line arguments. See 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. | ||
4
|
In file .pch to define:
| ||||||||
|
4
|
In a nutshell :Localize your applicationIt'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 This
Set user language When user changed application language in french, call
In this example this method set localized bundle to fr.lproj Return localized stringOnce 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 | ||||
|
3
|
You could build a sub-bundle with the set of localized strings that you want to do this with, and then use | ||
3
|
Maybe you should complement with this (on .pch file after #import ):
| ||||
|
2
|
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.
| ||||||||
|
2
|
swift version NSUserDefaults.standardUserDefaults().setObject(["fr"], forKey: "AppleLanguages") NSUserDefaults.standardUserDefaults().synchronize() | ||
1
|
This function will try to get localized string for current language and if it's not found it will get it using english language.
| ||
1
|
You can do something like this:
| ||
0
|
whatever you all do, the best way is to take the short_name for the specified language, i.e.: fr, en, nl, de, it, etc... and assign the same to a global value. make a picker view to pop up like a drop down menu (combination of a button on click of which a picker view appears from below with a list of languages) and select the language you desire. let the short name be stored internally. make a .h + .m file named LocalisedString. Set the global value of short_name to be equal to the obtained value in LocalisedString.m When the required language is selected assign the NSBundlePath to create project sub-directories for the needed language. for eg, nl.proj, en.proj. When the particular proj folder is selected call the localised string for the respective language and change the language dynamically. no rules broken. | ||
0
|
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). | ||
0
|
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. | ||
0
|
Based on Tudorizer's answer to change language without leaving or restarting the application. Instead of a macro, use a class for accessing the preferred language in order to check if a specific language code is present. Below is a class used to obtain the current language bundle that is working for iOS 9:
Use the class above to reference a string file / image / video / etc:
Change language in-line like below or update the "changeCurrentLanguage" method in the class above to take a string parameter referencing the new language.
|