How to get language locale of the user in Objective-C?

后端 未结 6 1241
野性不改
野性不改 2021-02-06 05:41

I am developing an application for Mac OS X. I want to change indication contents by the language locale (English, Spanish, etc.) of the application user, how do I get informati

相关标签:
6条回答
  • 2021-02-06 06:05
    NSLog(@"localeIdentifier: %@", [[NSLocale currentLocale] localeIdentifier]);
    
    0 讨论(0)
  • 2021-02-06 06:05

    code snippet

     NSLocale *locale = [NSLocale currentLocale]; 
     [locale objectForKey:NSLocaleLanguageCode]
    
    0 讨论(0)
  • 2021-02-06 06:19

    You can use the NSLocale API to get that information, but it isn't necessary to do what you want to do. OS X has support for localization built into the OS — all you need to do is supply the appropriate language files and the user can select which language he wants.

    0 讨论(0)
  • 2021-02-06 06:22

    To be exact there is a change with iOS 9 and greater where [NSLocale preferredLanguages] now return - instead of only . So it's better to do:

    NSString *languageOS = [[NSLocale preferredLanguages] objectAtIndex:0];
    
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {
        languageOS = [[languageOS componentsSeparatedByString:@"-"] objectAtIndex:0];
    }
    
    0 讨论(0)
  • 2021-02-06 06:24

    You're looking to "localize" your application. To get started, check out the Apple docs here: Internationalization - Apple Developer Docs. Without knowing more about your specific application, it'd be hard to suggest anything more here!

    0 讨论(0)
  • 2021-02-06 06:26

    you can use any way of both ways below:

    NSString *language = [[NSLocale currentLocale] localeIdentifier];
    NSLog(@"Language: %@", language);
    

    output: Language: en_US

    or this:

    NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];
    NSLog(@"Language: %@", language);
    

    output: Language: en

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