ios 如何获取当前的应用程序语言环境?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29986991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get current application locale?
提问by Alexandr
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.
那么如何获取当前显示的当前应用程序区域设置?提前致谢。
回答by Michael
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.
可能有比这更简单的方法(请参阅其他答案),但这是最可靠的方法,只要通过strings
-file本地化应用程序的一般原则不会过时,此方法就可以使用。
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
为每个受支持的语言环境准备了一个文件。因此,在英语字符串文件中,您可以添加行
"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”条目。
回答by Vijay yadav
The selected answer returns the current device language, but notthe 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);
回答by Jonathan Francis
In Swift 3 use:
在 Swift 3 中使用:
let language = Bundle.main.preferredLocalizations.first
let language = Bundle.main.preferredLocalizations.first
回答by Viral Savaj
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.
希望这对你有帮助。
回答by Jatin Patel - JP
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]
}
Hope this will help you.
希望这会帮助你。
回答by Kappe
The solution is way simpler than the ones proposed:
解决方案比提议的要简单得多:
Device language
设备语言
let deviceLanguage = NSLocale.preferredLanguages.first
let deviceLanguage = NSLocale.preferredLanguages.first
Actual app translation
实际应用翻译
let language = Bundle.main.preferredLocalizations.first
let language = Bundle.main.preferredLocalizations.first
For instance, I set my iPhone in English-Indian and my app doesn't have English-Indian translation but only English-UK:
例如,我将 iPhone 设置为英语-印度语,而我的应用程序没有英语-印度语翻译,而只有英语-英国语:
Printing description of language:
"en-GB"
Printing description of deviceLanguage:
"en-IN"