Reputation: 16430
I'm building an application that will be localized in a future version, so I want to setup it to be ready for that.
At the moment I have only one language (French) and the fr.lproj folder contains the Localizable.strings
with french translations for the related keys.
The problem is that If I set my device to English I don't receive the French default translations, but I see the Keys
name that I use in NSLocalizedString
.
For example if I try to get the title for a View Controller with:
NSLocalizedStrings(@"viewController_Title",nil);
The view controller, for device with English language shows "viewController_title" as title, while if I set the French language it works with no problem.
How can I deal with that?
Upvotes: 4
Views: 945
Reputation: 4614
In this string file " Localizable.strings" you need to declare localization like this
French.strings
"viewController_Title" = "ViewController_Title_In_Frech";
English.strings
"viewController_Title" = "ViewController_Title_In_English";
You need to use the localized string like this
NSLocalizedStringFromTable(Key, LanguageType, @"N/A")
ex:
NSLocalizedStringFromTable("viewController_Title", English, @"N/A");
Note : Change the language type programmatically then you can get the respective Localized string. And localized declaration is must in the relevant strings file.
Upvotes: 1
Reputation: 995
As mentioned in the other answers, By default English is used in case of missing language.
There are two solutions:
1. Add localization string file for english as well (with the same content as french localized string has)
2. Or add the following code in main method of main.m before calling UIApplicationMain
//sets the french as default language
NSArray *lang = [[NSUserDefaults standardUserDefaults] stringArrayForKey:@"AppleLanguages"];
if ([lang count] > 0 && (![[lang objectAtIndex:0] isEqualToString:@"fr"]) ) {
NSLog(@"language is neither de nor fr. forcing de");
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"fr", @"en", nil] forKey:@"AppleLanguages"];
}
Upvotes: 0
Reputation: 2013
Your problem is that you need a language to fallback to. As far as I know, there is no official way around it, I've written methods like this in the past:
NSString * L(NSString * translation_key) {
NSString * s = NSLocalizedString(translation_key, nil);
if (![[[NSLocale preferredLanguages] objectAtIndex:0] isEqualToString:@"en"] && [s isEqualToString:translation_key]) {
NSString * path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
NSBundle * languageBundle = [NSBundle bundleWithPath:path];
s = [languageBundle localizedStringForKey:translation_key value:@"" table:nil];
}
return s;
}
In this case, using L(@"viewController_Title");
would return the string for the default language, in this case being English.
Upvotes: 1
Reputation: 554
Your project is set to use English as the default language.
In your Info.plist file: Set "Localization native development region" to French.
Missing translations will now fall back to French instead of English.
Upvotes: 0