Change locale for current iphone application (custom locale for NSBundle?) - iphone

i want to change the locale programmatically in my iphone app (basically because not all languages are supported by iphone itself, it should be possible for the user to switch language only for my application). I have seen on a different question How to force NSLocalizedString to use a specific language that on OSX apps? it should work with [[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObjects:#"de", nil] forKey:#"AppleLanguages"]; - but for me it doesn't...
[NSLocale currentLocale] still returns the system wide locale and the 'main bundle' gives me files from the system wide locale..
so.. is there a way to set a custom locale for my application? or - which would be sufficient for me - create a new NSBundle instance with a different locale?

ok, stupid me.. as the question linked already answered.. that code requires a restart.. ie. when the user selects another language call [[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObjects:#"de", nil] forKey:#"AppleLanguages"]; - the next time the application is launched the [NSBundle mainBundle] will return the correct resources for the given locale.. (although [NSLocale currentLocale] will still return the iphone-wide locale.. but i think this doesn't matter to me anyway...)

Related

UIImagePickerController language not changed

In my app used
[[NSUserDefaults standardUserDefaults] setObject:#[#"fr"] forKey:#"AppleLanguages"];
this func to change app language.
It working fine all labels,button and all other elements. but, it is not replicated in UIImagePickerController.
Language change when i kill and reopen the app.
pls any suggestion.
Advance thanks
You need to synchronize your NSUserDefaults and change it like this:
[[NSUserDefaults standardUserDefaults] setValue:#"fr" forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
Next you need to use the macro :
NSLocalizedString(string, nil)
for the text inside your UIImagePickerController.
I change in Info.plist file string Localization native development region to my language, and then in app UIImagePickerController changed language. Maybe and for you it will work.

iOS: override NSLocale language

If you have a localized version of your app in several languages but if the user is not using any of the languages the app is localize.
How to set a default language? Or what will be the best practice to define a default language in this case?
Here are the scenarios I was trying to tackle:
The application has be localize in en/fr/es but if language locale is not en/fr/es how you define a default?
In case the the language locale is either one of this en/fr/es and the user whants to use a different language define on there NSUserDefaults. Let's say is using english as NSUserDefaults but wants to use the fr localized version of the aplication. There is a way to overwrite the language on NSUserDefaults ?
write the NSUserDefaults before the apps start
question1 : you can set your prefer order as below.
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"de", #"en", #"fr", nil] forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
Question2: force to use fr
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"de", nil] forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
I'm finding this no longer works in iOS 9 (and probably 10). We have several targets in our code base. For a couple of them I always want it to come up in English even though localized strings may be present in the bundle.
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"en", nil] forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
I have added the above code in main(). The first time the app runs it comes up in the user's chosen language. On subsequent runs it will come up in English as desired.
Can anyone confirm this doesn't work in iOS 9 and suggest an alternative? I'd rather not delete the other lproj entries if I don't have to.

Language change after selecting a option from the drop down list in iphone

I was working with language localisation .I want to change the language of my application whenever i select a language from a drop down list which is present in my application , that is it should change the language without changing the language of device .please suggest me how can i implement
Thanks in advance
Have a look at this: How to force NSLocalizedString to use a specific language
You can do this, but it won't play nicely with the built in localization functions in Foundation (e.g. NSLocalizedString), so you'll need to ignore them and write your own string-getting function, XIB-loading path, etc.
The question and answer here covers how to access localizations in the bundle on the fly like this:
Overriding preferred strings localization on the fly for testing
You can force the "AppleLanguages" user default.
The example below shows how can be done; in particular here I'm switching the first (default) with the second language.
NSArray *lang = [[NSUserDefaults standardUserDefaults] arrayForKey:#"AppleLanguages"];
NSLog(#"current lang: %#",lang);
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:
[lang objectAtIndex:1],
[lang objectAtIndex:0],
nil]
forKey:#"AppleLanguages"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *languages = [NSMutableArray arrayWithArray:[defaults objectForKey:#"AppleLanguages"]];
[languages replaceObjectAtIndex:0 withObject:#"fr"];
[defaults setObject:languages forKey:#"AppleLanguages"];
[defaults synchronize];
This is setting the language to French. Just put on position 0 what language you want to be loaded.

Localisation_setting.bundle

Just wanted to ask, how to localize Xibs via settings.bundle? Actually, I need to make my Xib arabic through settings.bundle. I have searched a lot for this. Please give me proper suggestions otherwise i need to recode my application and remove all contents of Xibs and do it via coding which would take a lot of time.
Thanx in advance.
I followed this post to get apps localised.
you can localize you nib file by keeping you xib file in specific language folder, e.g.
en.lproj/View1.xb is the english version
and
ar.lproj/View.xib is the arabic version
for more detailed instructions see this
UPDATE:
for that you simply create
View-en.xib
View-ar.xib
and then based on your custom settings
NSString *xibName = [NSString stringWithFormat:#"View-%#", #"en"];
id localVC = [[LocalViewController alloc] initWithNibName:xibName bundle:nil];
hit 'get info' by right clicking the xib you wish to localize... and within the general screen you can see the 'Make File Localizable' button on there.
#aamritrao if you make two separate XIB files, one for each language, you shouldn't give them different names as that just complicates things unnecessarily. Just name them the same but put them in different localized project folders.
So:
en.lproj/View1.XIB
and
ar.lproj/View1.XIB
Then when your app needs View1.XIB it will always use the appropriate one for the language the user has set in their device settings.
Sorry if this is not what you are looking for. Not sure what you mean by 'Localization via settings bundle'... could you explain more?
Hey just paste this following code in your main.m file
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *strLang=[[NSUserDefaults standardUserDefaults] stringForKey:#"PSMultiValueSpecifier"];
NSLog(#"Hi%#",strLang);
//
if ([strLang isEqualToString:#"0"]) {
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"en", #"ar", nil] forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
else if([strLang isEqualToString:#"1"])
{
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"ar", #"en", nil] forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;

localisation of iphone without changing iphone settings

i had done localisation by changing the language of iPhone through localisableString and by different xib which works only with NSLocale method ,but the requirement of app is to localised it by changing the language in app setting view irrespective of iPhone language..
Sounds like a stupid requirement, but anyway: You can change the language of your app by setting an array with the order of preferred languages for the user defaults key #"AppleLanguages", e.g.:
//Set language to German:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSArray arrayWithObject:#"de"]
forKey:#"AppleLanguages"];
Note however that this only affects strings/nibs you load afterwards, so you might need to reload your UI after changing the setting.