How can I get the current ISO language code in IOS?
如何在IOS中获取当前的ISO语言代码?
3 个解决方案
#2
3
[[NSLocale currentLocale] localeIdentifier]
will give you a string that's closer to what you are looking for. For my device here in the US, I get "en_US".
[[[NSLocale currentLocale] localeIdentifier]将为您提供一个字符串,该字符串更接近您要查找的字符串。对于我在美国的设备,我得到“en_US”。
If you want a more standard looking "en-US", you can take use [[[NSLocale currentLocale] localeIdentifier] stringByReplacingOccurrencesOfString:@"_" withString:@"-"]
instead.
如果您想要更标准的外观“en-US”,您可以使用[[[[NSLocale currentLocale] localeIdentifier] stringbyreplacingocurrencesofstring:@"_" withString:@"-"]代替。
If you just want the language code, you can pass the output of [[NSLocale currentLocale] localeIdentifier]
to [NSLocale componentsFromLocaleIdentifier:]
which will give you an NSDictionary back. One of the keys in the dictionary will be NSLocaleLanguageCode
which will have a NSString object with just the language code.
如果您只想要语言代码,可以将[[NSLocale currentLocale] localeIdentifier]的输出传递给[NSLocale componentsFromLocaleIdentifier:],这会返回一个NSDictionary。字典中的一个键是NSLocaleLanguageCode,它有一个只有语言代码的NSString对象。
#3
0
NSString *isoCode = [[NSLocale preferredLanguages] objectAtIndex:0];
This will return a 2 letter iso code for the selected language on the device. "en" for english, "de" for german, and so on. I hope that helps!
这将返回设备上所选语言的两个字母iso代码。“en”表示英语,“de”表示德语,等等。我希望会有帮助!
#1
#2
3
[[NSLocale currentLocale] localeIdentifier]
will give you a string that's closer to what you are looking for. For my device here in the US, I get "en_US".
[[[NSLocale currentLocale] localeIdentifier]将为您提供一个字符串,该字符串更接近您要查找的字符串。对于我在美国的设备,我得到“en_US”。
If you want a more standard looking "en-US", you can take use [[[NSLocale currentLocale] localeIdentifier] stringByReplacingOccurrencesOfString:@"_" withString:@"-"]
instead.
如果您想要更标准的外观“en-US”,您可以使用[[[[NSLocale currentLocale] localeIdentifier] stringbyreplacingocurrencesofstring:@"_" withString:@"-"]代替。
If you just want the language code, you can pass the output of [[NSLocale currentLocale] localeIdentifier]
to [NSLocale componentsFromLocaleIdentifier:]
which will give you an NSDictionary back. One of the keys in the dictionary will be NSLocaleLanguageCode
which will have a NSString object with just the language code.
如果您只想要语言代码,可以将[[NSLocale currentLocale] localeIdentifier]的输出传递给[NSLocale componentsFromLocaleIdentifier:],这会返回一个NSDictionary。字典中的一个键是NSLocaleLanguageCode,它有一个只有语言代码的NSString对象。
#3
0
NSString *isoCode = [[NSLocale preferredLanguages] objectAtIndex:0];
This will return a 2 letter iso code for the selected language on the device. "en" for english, "de" for german, and so on. I hope that helps!
这将返回设备上所选语言的两个字母iso代码。“en”表示英语,“de”表示德语,等等。我希望会有帮助!