Setting default language for iPhone app on first run - iphone

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?

Related

Change language of the ios application

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);

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.

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;

iPhone app using different language than the one set in OS / device?

Is it possible to have my app running in a different language than the one that is set in the OS? I want to have a language switch in my app’s setting menu, where the user can e.g. select german for the app, while his system is running in english.
From what I’ve already read, it seems it’s not possible without having my own localization mechanism…
What do you think?
it can be done easily although it took me weeks to work out how ;-)
I have an iPhone app called iDEX which is a Romanian dictionary. On the iPad, Romanian is not available as a UI language, so I wanted to give the user the option of selecting Romanian in the application settings, as the device settings do not include it.
What you have to do is internationalize the app and localize it as usual. Then, in the main method, set the AppleLanguages key of the standard NSUserDefaults to the language and country of your choice, like so
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSArray arrayWithObject:#"ro_RO"] forKey:#"AppleLanguages"];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool drain];
return retVal;
}
It is imperative that you set the AppleLanguages key before you run UIApplicationMain(), because it is at that point that the UIKit framework is loaded and the default language determined.
It also important that you specify both language and country (eg, #"ro_RO" and not just #"ro") otherwise your app might crash when setting the key.
By doing this, all calls to NSBundle that support localization will look for the language that you have programatically established, as opposed to the one set on the device.
Hope that helps...
You need to implement your custom resource loading mechanism to achieve that. For example you will no longer be able to use NSLocalizedString macro or [NSBundle pathForResource:]. You will always have to specify paths including the localization (as in de.lproj/MyStrings.strings instead of just MyStrings.strings). I would suggest against doing this unless absolutely necessary.