Localizable.strings on iOS, not localized languages

I am creating an application that will be localized in a future version, so I want to configure it for this.

Currently, I have only one language (French), and the fr.lproj folder contains Localizable.stringsFrench translations for related keys.

The problem is that if I install my device in English, I don’t get the French translations by default, but I see the name Keysthat I use in NSLocalizedString.

For example, if I try to get the title for the view controller with:

NSLocalizedStrings(@"viewController_Title",nil);

The view controller for a device with English shows "viewController_title" as the title, and if I set French, it works without problems.

How can I handle this?

+4
source share
4 answers

In this string file "Localizable.strings" you need to declare localization as follows

French.strings

"viewController_Title" = "ViewController_Title_In_Frech";

English.strings

"viewController_Title" = "ViewController_Title_In_English";

You need to use a localized string like this

NSLocalizedStringFromTable (Key, LanguageType, @ "N / A")

Example:

NSLocalizedStringFromTable("viewController_Title", English, @"N/A");

Note. Change the language type programmatically, then you can get the corresponding Localized string. And the localized declaration should be in the file of the corresponding lines.

+1
source

, . , , :

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

L(@"viewController_Title"); , .

+1

.

Info.plist: " " .

, .

0

As mentioned in other answers, by default, English is used if there is no language. There are two solutions:
1. Add a localization string file for the English language (with the same contents as the French localized string)
2. Or add the following code to the main.m main method 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"];
}
0
source

All Articles