Change language of the ios application - iphone

I've put my app settings into iPhone Settings panel. I want to switch between languages independently of the iPhone system language. Is it possible? I'm using this advice:
How to force NSLocalizedString to use a specific language
But it translates only string inside app, but my nib files are same. How to reload my nibs?

The key is to override the NSUserDefaults language key before UIApplicationMain() is called in your main() function in main.m. Try something like this in your main() function:
// use whatever language/locale id you want to override
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"de_DE", nil]
forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
int retVal = UIApplicationMain(argc, argv, nil, nil);

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.

Set iPhone app language inside app settings works, but the app name doesn't change

In my iPhone app, I can successfully change language inside the app's Settings (just the user have to restart the app). I use the trick mentioned in this post: link. I made some modification. I store the selected language in NSUserDefaults, and in main.m:
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if ( [[NSUserDefaults standardUserDefaults] objectForKey:#"language"] == nil ) {
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:#"hu"] forKey:#"AppleLanguages"];
} else {
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:[[NSUserDefaults standardUserDefaults] objectForKey:#"language"]] forKey:#"AppleLanguages"];
}
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
The "default" Info.plist file is in english, I placed it in the root of the project. I made a hungarian translation of this, because of app name. This file is InfoPlist.strings, and I placed it in hu.lproj folder. In this file:
CFBundleDisplayName = "Sör";
If I change the language, after restart the app, everything is work fine (nib files, strings), except of app name. It doesn't change...
Can someone tell me what is the problem?
You have done almost everything you need to do.
You also need to instruct your app that it has a localized app name. You do this by adding the following key to your app's info.plist
LSHasLocalizedDisplayName
set it's type as boolean and it's value to true.
Once you have done this, your app should localize it's name.
You can read more here
EDIT:
One more thing you need to do is create an InfoPlist.strings file in the en.lproj folder and add the same key to it with the English equivalent
CFBundleDisplayName = "<Replace Me>";
Credit for this addition goes to this link
Well the app name is read by iOS which is not set to the language that your app is set to.
So it will display the name of the app which is set for the system language.

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;

Setting default language for iPhone app on first run

I'm developing an application that should support two languages: English and French. However because English translation is not done yet we want to deploy it in French only and later on add English translation later on.
The problem is that I don't want to strip English language out of my code since some parts are already done, there are different NIBs for that language etc. Instead I'd just want english language to be temporary disabled in my app.
What I did is I put this code as the first instruction of
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSArray arrayWithObjects:#"fr", nil] forKey:#"AppleLanguages"];
[defaults synchronize];
It works fine except for one thing. When you launch the application for the first time after installation it's still in English. That's probably because AppleLanguages preference was not yet set for it. After I quit the application and start it again it's being displayed correctly in French.
Does anyone knows a fix so that French language was applied also on the first run?
I ran into the same issue, and the only way I could fix it was to have the piece of code at the earliest stage in the app, i.e. in main.c:
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSArray arrayWithObjects:#"fr", nil] forKey:#"AppleLanguages"];
[defaults synchronize];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
I'm not really sure it's a good practice but it worked as expected in my case.
Sounds messy. Why not just uncheck the unfinished English resources from the target, so they won't get deployed? Also, have you looked into the CFBundleDevelopmentRegion setting in Info.plist?