I need to get current locale. Not user locale but my application locale.
我需要获得当前的语言环境。不是用户区域设置,而是我的应用程序区域
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).
假设我的应用程序有两个本地化(在项目设置中):英语(默认)和法语。如果用户在iPhone上设置法语,那么我的应用程序将显示法语界面。如果用户在iPhone上设置德语,那么我的应用程序将显示英文界面(因为英语是默认的)。
So how do I get current application locale that is showing right now? Thanks in advance.
那么我如何获得现在正在显示的当前应用程序区域设置?提前致谢。
5 个解决方案
#1
19
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);
#2
23
There may be an easier way than this (see other answers), but this one is most robust, and as long as the general principle of localizing an app through a strings
-file doesn't get obsoleted, this method will work.
可能有一种比这更简单的方法(参见其他答案),但这个方法最强大,只要通过字符串文件本地化应用程序的一般原则不会被废弃,这种方法就可以了。
Generally, you don't need to get the application locale (but read on, it's possible!) If you want localized text, you use NSLocalizedString()
. If you need to localize images you use localized resources, and so on. However, there are reasons that I can think of which would make it nice to get the "application locale", as you call it: for example for analytics (you want to know in which language your app is used), or for providing a consistent one-language interface to the user if you use server-based communication (e.g. to localize the server error messages in the same language that the user is seeing inside the app.)
通常,您不需要获取应用程序区域设置(但请继续阅读,这是可能的!)如果您想要本地化文本,则使用NSLocalizedString()。如果需要本地化映像,则使用本地化资源,等等。但是,有一些原因可以让我想到哪个会让你更好地获得“应用程序区域设置”,就像你所说的那样:例如,对于分析(你想知道你的应用程序使用的是哪种语言),或提供一个如果您使用基于服务器的通信,则为用户提供一致的单语言界面(例如,使用用户在应用程序内部看到的相同语言本地化服务器错误消息。)
If you want to get the localization of the app that is currently visible, I suppose you have a Localizable.strings
file for each supported locale. So, in the Englisch strings-file you can add the line
如果您想获得当前可见的应用程序的本地化,我想您有一个Localizable.strings文件用于每个支持的语言环境。因此,在Englisch字符串文件中,您可以添加该行
"lang" = "en";
and in the French string-file you add the line
并在法语字符串文件中添加该行
"lang" = "fr";
then, you can always get the application-locale by calling NSLocalizedString("lang")
(swift) or NSLocalizedString(@"lang")
(objective-C). And of course, whenever you add a new localization to your app, you have to set a "lang" entry into the new localizations strings-file.
然后,您始终可以通过调用NSLocalizedString(“lang”)(swift)或NSLocalizedString(@“lang”)(objective-C)来获取应用程序区域设置。当然,无论何时向应用添加新的本地化,都必须在新的本地化字符串文件中设置“lang”条目。
#3
8
In Swift 3 use:
在Swift 3中使用:
let language = Bundle.main.preferredLocalizations.first
let language = Bundle.main.preferredLocalizations.first
#4
3
You can get country code from locale, this way,
您可以通过这种方式从区域设置获取国家/地区代码,
NSLocale *currentLocale = [NSLocale currentLocale]; // get the current locale.
NSString *countryCode = [currentLocale objectForKey:NSLocaleCountryCode];
Get country code, e.g. es
(Spain), fr
(France), etc.
获取国家代码,例如es(西班牙),fr(法国)等
You can map this code for your interface selection.
您可以映射此代码以进行界面选择。
Hope this help you.
希望这对你有所帮助。
#5
0
you can get selected language using following code.
您可以使用以下代码获取所选语言。
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
if ([language isEqualToString:@"fr"]){//French
[do your stuff]
}
else{//English
[do your stuff]
}
List_of_ISO_639-1_codes
Hope this will help you.
希望这会帮助你。
#1
19
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);
#2
23
There may be an easier way than this (see other answers), but this one is most robust, and as long as the general principle of localizing an app through a strings
-file doesn't get obsoleted, this method will work.
可能有一种比这更简单的方法(参见其他答案),但这个方法最强大,只要通过字符串文件本地化应用程序的一般原则不会被废弃,这种方法就可以了。
Generally, you don't need to get the application locale (but read on, it's possible!) If you want localized text, you use NSLocalizedString()
. If you need to localize images you use localized resources, and so on. However, there are reasons that I can think of which would make it nice to get the "application locale", as you call it: for example for analytics (you want to know in which language your app is used), or for providing a consistent one-language interface to the user if you use server-based communication (e.g. to localize the server error messages in the same language that the user is seeing inside the app.)
通常,您不需要获取应用程序区域设置(但请继续阅读,这是可能的!)如果您想要本地化文本,则使用NSLocalizedString()。如果需要本地化映像,则使用本地化资源,等等。但是,有一些原因可以让我想到哪个会让你更好地获得“应用程序区域设置”,就像你所说的那样:例如,对于分析(你想知道你的应用程序使用的是哪种语言),或提供一个如果您使用基于服务器的通信,则为用户提供一致的单语言界面(例如,使用用户在应用程序内部看到的相同语言本地化服务器错误消息。)
If you want to get the localization of the app that is currently visible, I suppose you have a Localizable.strings
file for each supported locale. So, in the Englisch strings-file you can add the line
如果您想获得当前可见的应用程序的本地化,我想您有一个Localizable.strings文件用于每个支持的语言环境。因此,在Englisch字符串文件中,您可以添加该行
"lang" = "en";
and in the French string-file you add the line
并在法语字符串文件中添加该行
"lang" = "fr";
then, you can always get the application-locale by calling NSLocalizedString("lang")
(swift) or NSLocalizedString(@"lang")
(objective-C). And of course, whenever you add a new localization to your app, you have to set a "lang" entry into the new localizations strings-file.
然后,您始终可以通过调用NSLocalizedString(“lang”)(swift)或NSLocalizedString(@“lang”)(objective-C)来获取应用程序区域设置。当然,无论何时向应用添加新的本地化,都必须在新的本地化字符串文件中设置“lang”条目。
#3
8
In Swift 3 use:
在Swift 3中使用:
let language = Bundle.main.preferredLocalizations.first
let language = Bundle.main.preferredLocalizations.first
#4
3
You can get country code from locale, this way,
您可以通过这种方式从区域设置获取国家/地区代码,
NSLocale *currentLocale = [NSLocale currentLocale]; // get the current locale.
NSString *countryCode = [currentLocale objectForKey:NSLocaleCountryCode];
Get country code, e.g. es
(Spain), fr
(France), etc.
获取国家代码,例如es(西班牙),fr(法国)等
You can map this code for your interface selection.
您可以映射此代码以进行界面选择。
Hope this help you.
希望这对你有所帮助。
#5
0
you can get selected language using following code.
您可以使用以下代码获取所选语言。
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
if ([language isEqualToString:@"fr"]){//French
[do your stuff]
}
else{//English
[do your stuff]
}
List_of_ISO_639-1_codes
Hope this will help you.
希望这会帮助你。