NSLocale currentLocale always returns “en_US” not user's current language

后端 未结 9 1650
夕颜
夕颜 2020-11-30 21:28

I\'m in the processes of internationalizing an iPhone app - I need to make programmatic changes to certain views based on what the user\'s current locale is. I\'m going nut

相关标签:
9条回答
  • 2020-11-30 21:31

    For us the issue was that we were overriding the application language and application region in our dev scheme. Make sure that the Application Language is set to System Language in the scheme options (Edit Scheme -> Options).

    0 讨论(0)
  • 2020-11-30 21:33

    for me, both

    NSString *localeString = [[NSLocale currentLocale] localeIdentifier];
    

    and

    NSArray *array = [NSLocale preferredLanguages];
    self.label.text = array[0];
    

    yield the same result when you're in a simulator.

    0 讨论(0)
  • 2020-11-30 21:41

    I had an issue where formatting month names came out in english on a device set to french language.

    My solution was to use this:

        NSLocale *locale = [NSLocale localeWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0] ];
        [self.monthNameFormatter setLocale:locale];
        [self.monthNameFormatter setDateFormat:@"MMMM"];
        [self.monthNameFormatter stringFromDate:myDate];
    
    0 讨论(0)
  • 2020-11-30 21:44

    Question is too old, but this code may help to many:

    The main.m should look like this:

        NSString *localecode=[[NSLocale currentLocale] localeIdentifier];
        localecode=[localecode substringToIndex:2]; //en_GB -> en
        NSArray *arr = [NSLocale preferredLanguages];
        NSMutableArray * mutable = [arr mutableCopy];
        NSString *prefered = [mutable firstObject];
        if(![localecode isEqualToString:prefered]){
            if(![prefered isEqualToString:@"es"] && ![prefered isEqualToString:@"en"] && ![prefered isEqualToString:@"pt"]){
                int index = [mutable indexOfObject:@"en"];
                [mutable replaceObjectAtIndex:0 withObject:@"en"];
                [mutable replaceObjectAtIndex:index withObject:prefered];
                [[NSUserDefaults standardUserDefaults] setObject:mutable forKey:@"AppleLanguages"];
    
            }
            else{
                int index = [mutable indexOfObject:localecode];
                [mutable replaceObjectAtIndex:0 withObject:localecode];
                [mutable replaceObjectAtIndex:index withObject:prefered];
                [[NSUserDefaults standardUserDefaults] setObject:mutable forKey:@"AppleLanguages"];
            }
        }
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    

    What do this? If the current language of the device es Spanish, English or Portuguese use the the application uses the localized strings, by the other hand if the current language is not one of those and is not supported by application is set to English.

    0 讨论(0)
  • 2020-11-30 21:48

    Instead of querying defaults directly using an undocumented key, ask the NSLocale class for the array of preferred languages.

    0 讨论(0)
  • 2020-11-30 21:50

    i found that if i leave "en_US" out , but have a "en" localization that is a copy of "en_US" the simulator automagically starts respecting the language settings, but as soon as "en_US" is an option it always picks it regardless of the settings.

    0 讨论(0)
提交回复
热议问题