Getting current device language in iOS?

前端 未结 30 2745
星月不相逢
星月不相逢 2020-11-22 09:10

I\'d like to show the current language that the device UI is using. What code would I use?

I want this as an NSString in fully spelled out format. (Not

30条回答
  •  独厮守ぢ
    2020-11-22 09:29

    Translating language codes such as en_US into English (United States) is a built in feature of NSLocale and NSLocale does not care where you get the language codes from. So there really is no reason to implement your own translation as the accepted answer suggests.

    // Example code - try changing the language codes and see what happens
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
    NSString *l1 = [locale displayNameForKey:NSLocaleIdentifier value:@"en"];
    NSString *l2 = [locale displayNameForKey:NSLocaleIdentifier value:@"de"];
    NSString *l3 = [locale displayNameForKey:NSLocaleIdentifier value:@"sv"];
    NSLog(@"%@, %@, %@", l1, l2, l3);
    

    Prints: English, German, Swedish

提交回复
热议问题