// get a French locale instance
NSLocale *frLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"] autorelease];
// use it to get translated display names of fr_FR and en_US
NSLog(@"%@", [frLocale displayNameForKey:NSLocaleIdentifier value:@"fr_FR"]);
NSLog(@"%@", [frLocale displayNameForKey:NSLocaleIdentifier value:@"en_US"]);
这将打印出:
français (France)
anglais (États-Unis)
如果你为initWithLocaleIdentifier:和displayNameForKey:value:方法指定了相同的区域设置标识符,那么它将为你提供该语言的本机名称。我发现,如果你删除国家代码,只使用fr和en,它也会从显示名称中省略国家(至少在Mac OS X上,不确定iOS)。
+ (NSArray *)preferredLanguages NS_AVAILABLE(10_5, 2_0); // note that this list does not indicate what language the app is actually running in; the [NSBundle mainBundle] object determines that at launch and knows that information
2013-03-02 20:01:57.457 xx[12334:907] zh-Hans: zh-Hans 中文(简体中文)
2013-03-02 20:01:57.460 xx[12334:907] en: en English
2013-03-02 20:01:57.462 xx[12334:907] ja: ja 日本語
2013-03-02 20:01:57.465 xx[12334:907] fr: fr français
2013-03-02 20:01:57.468 xx[12334:907] de: de Deutsch
2013-03-02 20:01:57.472 xx[12334:907] nl: nl Nederlands
2013-03-02 20:01:57.477 xx[12334:907] it: it italiano
2013-03-02 20:01:57.481 xx[12334:907] es: es español
func deviceLanguage() -> String? {
let systemBundle: NSBundle = NSBundle(forClass: UIView.self)
let englishLocale: NSLocale = NSLocale(localeIdentifier: "en")
let preferredLanguages: [String] = NSLocale.preferredLanguages()
for language: String in preferredLanguages {
let languageComponents: [String : String] = NSLocale.componentsFromLocaleIdentifier(language)
guard let languageCode: String = languageComponents[NSLocaleLanguageCode] else {
continue
}
// ex: es_MX.lproj, zh_CN.lproj
if let countryCode: String = languageComponents[NSLocaleCountryCode] {
if systemBundle.pathForResource("\(languageCode)_\(countryCode)", ofType: "lproj") != nil {
// returns language and country code because it appears that the actual language is coded within the country code aswell
// for example: zh_CN probably mandarin, zh_HK probably cantonese
return language
}
}
// ex: English.lproj, German.lproj
if let languageName: String = englishLocale.displayNameForKey(NSLocaleIdentifier, value: languageCode) {
if systemBundle.pathForResource(languageName, ofType: "lproj") != nil {
return languageCode
}
}
// ex: pt.lproj, hu.lproj
if systemBundle.pathForResource(languageCode, ofType: "lproj") != nil {
return languageCode
}
}
return nil
}
if let deviceLanguageCode: String = deviceLanguage() {
let printOutputLanguageCode: String = deviceLanguageCode
let printOutputLocale: NSLocale = NSLocale(localeIdentifier: printOutputLanguageCode)
if let deviceLanguageName: String = printOutputLocale.displayNameForKey(NSLocaleIdentifier, value: deviceLanguageCode) {
// keep in mind that for some localizations this will print a language and a country
// see deviceLanguage() implementation above
print(deviceLanguageName)
}
}
// Get language prefered by user
let langageRegion = NSLocale.preferredLanguages().first!
let languageDic = NSLocale.componentsFromLocaleIdentifier(langageRegion)
let language = languageDic[NSLocaleLanguageCode]
// gets the language code without country code in uppercase format, i.e. EN or DE
NSString* GetLanguageCode()
{
static dispatch_once_t onceToken;
static NSString* lang;
dispatch_once(&onceToken, ^
{
lang = [[[NSLocale preferredLanguages] objectAtIndex:0] uppercaseString];
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"^[A-Za-z]+" options:0 error:nil];
NSTextCheckingResult* match = [regex firstMatchInString:lang options:0 range:NSMakeRange(0, lang.length)];
if (match.range.location != NSNotFound)
{
lang = [lang substringToIndex:match.range.length];
}
});
return lang;
}
extension Locale {
static var preferredLanguageIdentifier: String {
let id = Locale.preferredLanguages.first!
let comps = Locale.components(fromIdentifier: id)
return comps.values.first!
}
static var preferredLanguageLocalizedString: String {
let id = Locale.preferredLanguages.first!
return Locale.current.localizedString(forLanguageCode: id)!
}
}
// To get device default selected language. It will print like short name of zone. For english, en or spain, es.
let language = Bundle.main.preferredLocalizations.first! as NSString
print("device language",language)
// Returns a list of the user's preferred languages.
// Maybe more than (or none of) your app supports!
Locale.preferredLanguages
// a subset of this bundle's localizations, re-ordered into the preferred order
// for this process's current execution environment; the main bundle's preferred localizations
// indicate the language (of text) the user is most likely seeing in the UI
Bundle.main.preferredLocalizations
// The current running app language
Bundle.main.preferredLocalizations.first
// list of language names this bundle appears to be localized to
Bundle.main.localizations