NSLocale Language Issue - ios

So in my app I am trying to get the devices currently set language and its acronym. So I do this:
NSString *fullLanguage = [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:[[NSLocale preferredLanguages] objectAtIndex:0]];
NSString *abrlanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
However some users report that the language is returning something like: en_UK or something similar, which in turn is messing up the functionality of my app.
Anyway is there a way to get the currently set language of the device regardless if the devices regional settings?
Thanks!

To get the language code, use:
NSString *languageCode = [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode];
NSLog(#"%#", languageCode); // Prints "en"
To get the full name of the language:
NSString *languageName = [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:languageCode];
NSLog(#"%#", languageName); // Prints "English"
Note that you were using the region code (which provides for regional variations of languages), and could be gotten easier like this:
NSString *regionCode = [[NSLocale currentLocale] objectForKey:NSLocaleIdentifier];
NSLog(#"%#", regionCode); // Prints "en_US"
Region codes start with the language code, followed by the underscore, and then the regional variation. (This is standardized per Apple's documentation.)
Also, if you use currentLocale, know that it is not updated as the users preferences are changed. Use autoupdatingCurrentLocale instead if you want to keep it in sync if they change.

Related

NSLocale.currentLocale in iOS always returns en_US. Does not return the language of iPad

I am using the following code:
NSLocale *locale = NSLocale.currentLocale;
NSString *countryCode = [locale objectForKey:NSLocaleCountryCode];
NSString *langaugeCode =[locale objectForKey:NSLocaleLanguageCode];
and the language code always comes back to be en_US even if I switch the language of my iPad. Any suggestions of what I might be doing wrong?

Get device current language in English language in ios 10 and above?

I would like to get language name selected by the user device.
I got language id and it's display name
NSString *langID = [[NSLocale preferredLanguages] objectAtIndex:0];
NSLog(#"langID %#",langID);
lang = [[NSLocale currentLocale] displayNameForKey:NSLocaleLanguageCode value:langID];
NSLog(#"lang %#",lang);
If I select device language as 'Chinese Traditional',the output is
langID zh-Hant-US display name 中文.
I want to display name in the English language like 'Chinese', not in the Chinese language. So my question is "Is it possible to get language name in English?" If yes then "How can we achieve it?" any suggestions would be greatly appreciated.
Thanx in Advance.
Try this :
NSString *langID = [[NSLocale preferredLanguages] objectAtIndex:0];
NSLocale *local = [NSLocale localeWithLocaleIdentifier:#"EN"];
NSString * lang = [local displayNameForKey:NSLocaleLanguageCode value:langID];

how to retrive the country code ios example

I want to display the country code in a uitextfield.
Is there some source code example on iOS about this?
I did some search, but i can t find anything concrete.
I need a start point.
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
That will give you the country/language code (assuming that's what you're looking for).
This will give just the country code. E.g. US, AU, etc.
NSString *countryCode = [[[NSLocale currentLocale] objectForKey:NSLocaleCountryCode] uppercaseString];

Get iOS current language (including country code)

I need to get a string like en_US or es_ES in iOS.
The obvious method is to use [NSLocale currentLocale] and get the language info from there. However, that gives you the "region format" and not the actual device language, which means that if you have the device language in english but the region format as "spain", it'll erroneously report es_ES.
If you need the device language you must do this instead:
[[NSLocale preferredLanguages] objectAtIndex:0]
However, that only gives you the language, so you get something like en or es, without the country code at the end.
How would I get the country code correctly, like Safari does?
Try with this code:
NSString *locale = [[NSLocale currentLocale] localeIdentifier];
NSRange startRange = [locale rangeOfString:#"_"];
NSString *result = [locale stringByReplacingCharactersInRange:NSMakeRange(0, startRange.length+1) withString:[[NSLocale preferredLanguages] objectAtIndex:0]];
DebugLog(#"current locale: %#", result);
Ok, seeing getting the right info out of iOS is probably not possible, here's a hackish solution but which gives the output I needed. It's not complete and it won't give precise output in some cases (like for arabic), but it's the best I've been able to get.
const string lang = [[[NSLocale preferredLanguages] objectAtIndex:0] UTF8String];
// Search the language with country code in the langs map
static std::map<string, string> langMap;
static bool initialized = false;
if(!initialized) {
#define LANG(l, c) langMap.insert(std::make_pair(#l, #c))
LANG(en, en-us); LANG(es, es-es); LANG(fr, fr-fr); LANG(de, de-de);
LANG(ja, ja-jp); LANG(nl, nl-nl); LANG(it, it-it); LANG(pt, pt-br);
LANG(da, da-dk); LANG(fi, fi-fi); LANG(nb, nb-no); LANG(sv, sv-se);
LANG(ko, ko-kr); LANG(ru, ru-ru); LANG(pl, pl-pl); LANG(tr, tr-tr);
LANG(uk, uk-ua); LANG(hr, hr-hr); LANG(cs, cs-cz); LANG(el, el-gr);
LANG(he, he-il); LANG(ro, ro-ro); LANG(sk, sk-sk); LANG(th, th-th);
LANG(ca, ca-es); LANG(hu, hu-hu); LANG(vi, vi-vn);
LANG(zh-Hans, zh-cn); LANG(pt-PT, pt-pt); LANG(id, id); LANG(ms, ms);
LANG(zh-Hant, zh-tw); LANG(en-GB, en-gb); LANG(ar, ar);
#undef LANG
initialized = true;
}
map<string,string>::iterator it = langMap.find(lang);
if( it != langMap.end() ){
return it->second;
}
// Didn't find a country code, default to the lang name
return lang;
Check the below code:
NSArray *langs = [NSLocale preferredLanguages];
for (NSString *lang in langs) {
NSLog(#"%#: %# %#",lang, [NSLocale canonicalLanguageIdentifierFromString:lang], [[[NSLocale alloc] initWithLocaleIdentifier:lang] displayNameForKey:NSLocaleIdentifier value:lang]);
}
Swift
NSLocale.preferredLanguages()[0] as String
Output would be like
en-GB
Source
An alternative would be to reconstruct the locale from the components of the current locale, swapping in the language that you want.
For example, to get the current locale but modified to use the language which UIKit is currently using to display the app:
let languageCode = Bundle.main.preferredLocalizations.first ?? "en"
var components = Locale.components(fromIdentifier: Locale.current.identifier)
components[NSLocale.Key.languageCode.rawValue] = languageCode
let locale = Locale(identifier: Locale.identifier(fromComponents: components))
I am using this at the moment. I have run a few test cases and it seems ok, However I am not convinced that it is a robust solution. I am surprised not to find a clear answer to this simple problem. I would not recommend my solution but I hope it can generate discussion.
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSString *language = [[NSLocale preferredLanguages] firstObject];
NSString *myLocale = [NSString stringWithFormat:#"%#_%#",language,countryCode];
NSLocale *userLocale = [[NSLocale alloc] initWithLocaleIdentifier:myLocale];
NSDate* now = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:#"E MMM d yyyy HH:mm" options:0 locale:userLocale];
Simply do this:
NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString *locale = [[NSLocale currentLocale] objectForKey: NSLocaleCountryCode];
NSString *formattedStr = [NSString stringWithFormat:#"%#_%#",language, locale];
NSLog(#"%#", formattedStr); // Display en_US for example

Set device language as locale?

In one of my localized apps, Spanish is the default language. However, the user could have set English as default language, and its region to to "Spain".
The problem is that within my app I use this code:
int day = (60*60*24);
NSDate *nextNextDay = [[NSDate alloc] initWithTimeIntervalSinceNow:(day*2)];
NSString *someString = [[dateFormatter stringFromDate:nextNextDay] capitalizedString];
NSLog(#"The day: %#", someString);
The result is the name of the day in the default locale, which is (in this scenario) spanish. So, instead of that I would get "Monday" as result, I will get "Lunes" instead. This, of course, is very ugly when the rest of the app is in English.
How can I solve this without hard-coding any locales?
Why not hardcode locales?
You could do this:
NSString *userLocale = [[NSLocale currentLocale]localeIdentifier];
NSString *userLanguage = [userLocale substringToIndex:2];
if([userLanguage isEqualToString:#"en"]){
//English
}
if([userLanguage isEqualToString:#"es"]){
//Spanish
}
Hope this helped

Resources