如何获取当前的应用程序区域设置? [英] How do I get current application locale?

查看:162
本文介绍了如何获取当前的应用程序区域设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取当前的语言环境。不是用户区域设置,而是我的应用程序区域设置。

I need to get current locale. Not user locale but my application locale.

假设我的应用程序有两个本地化(在项目设置中):英语(默认)和法语。如果用户在iPhone上设置法语,那么我的应用程序将显示法语界面。如果用户在iPhone上设置德语,那么我的应用程序将显示英文界面(因为英语是默认的)。

Let's say my application has two localizations (in project settings): English (default) and French. If user sets French language on iPhone then my application will display French interface. If user sets German language on iPhone then my application will display English interface (because English is default).

那么如何获得现在正在显示的当前应用程序区域设置?
提前致谢。

So how do I get current application locale that is showing right now? Thanks in advance.

推荐答案

所选答案返回当前设备语言,但不是应用程序中使用的实际语言。如果您未在应用中为首选语言提供本地化:

The selected answer returns the current device language, but not the actual language used in the app. If you don't provide a localization for the preferred language in your app:

NSString *language = NSBundle.mainBundle.preferredLocalizations.firstObject;

NSLocale *locale = NSLocale.currentLocale;
NSString *countryCode = [locale objectForKey:NSLocaleCountryCode];

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSString *country = [usLocale displayNameForKey:NSLocaleCountryCode 
                                          value:countryCode];

NSLog(@"country: %@", country);

这篇关于如何获取当前的应用程序区域设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆