I'm trying to show a different string for Mexican Spanish than US Spanish. The app always displays the US Spanish phrase. FYI - English, French and Portuguese work fine - but they don't have locales specified.

I created the localization folder in Build Settings->Localizations by clicking the + button, and selecting Spanish (Mexico)(es-MX) and Spanish (United States)(es-US) from the "Other" menu. I then updated the phrases appropriately. In the project I see 5 Localizable.strings files, including (Spanish-Mexico) and (Spanish-United States). Similarly I see the expected folders in Finder, including es-MX and es-US.

I put some NSLog messages in application didFinishLaunchingWithOptions:

    NSLog(@"preferredLocalizations=%@", [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0]);
    NSLog(@"localization array=%@, count=%d", [[NSBundle mainBundle] preferredLocalizations], [[[NSBundle mainBundle] preferredLocalizations] count]);
    NSLog(@"preferredLanguage = %@", [[NSLocale preferredLanguages] objectAtIndex:0]);
    NSLog(@"language array=%@, count=%d", [NSLocale preferredLanguages], [[NSLocale preferredLanguages] count]);
    NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
    NSArray* languages = [defs objectForKey:@"AppleLanguages"];
    NSString *current = [languages objectAtIndex:0];
    NSLog(@"user default language=%@", current);

The output is:

2013-07-30 22:21:24.911 ecatalog[33497:907] application:didFinishLaunchingWithOptions
2013-07-30 22:21:24.918 ecatalog[33497:907] preferredLocalizations=es
2013-07-30 22:21:24.920 ecatalog[33497:907] localization array=(
    es
), count=1
2013-07-30 22:21:24.922 ecatalog[33497:907] preferredLanguage = es
2013-07-30 22:21:24.924 ecatalog[33497:907] language array=(
es,
en,
fr, <snipped a bunch for brevity>
ms,
"en-GB",
ca,
hu,
vi
), count=34
2013-07-30 22:21:24.926 ecatalog[33497:907] user default language=es

The language array seems to the list of available languages on the phone, in my preferred order - i.e., if I set the phone for French, fr will be at the top of the list.

I don't know if its related, but my project includes two 3rd party frameworks which support Spanish, but just the es.lproj variation - not es-MX.lproj or es-US.lproj.

One other "symptom" - running the app in the simulator alternates between English and whatever language I have set the simulator to.

Thanks!

有帮助吗?

解决方案

In my case I only have one string that needs to be different between Mexican and US Spanish, and it is only used in one place. So my work-around is to manually check the locale and hard-code the Mexican Spanish string:

    NSString *startString;
    // HACK - to diferentiate between Mexican Spanish and all others
    NSLocale* curentLocale = [NSLocale currentLocale];
    NSString *localId = [curentLocale localeIdentifier];
    if ([localId isEqualToString: @"es_MX"]) {
        // hard code a string
        startString = @"Para seleccionar un folleto da clic +";
    } else {
        startString = NSLocalizedString(@"Click + to start!", nil);
    }

It doesn't feel right, so if anyone has "the right way" I'd appreciate it.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top