How can I get the current ISO language code in IOS?
[[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".
If you want a more standard looking "en-US", you can take use [[[NSLocale currentLocale] localeIdentifier] stringByReplacingOccurrencesOfString:#"_" withString:#"-"] instead.
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.
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!
Related
I'm using a webservice which provides me a list of countries to choose from as 2 letters ('us' for USA, 'es' for Spain etc.).
I need to present it to the user and send back the selection.
Is there an API on iOS to convert the country code to a country name - based on the current language? I know I can have a static mapping of codes to countries, but I would like to support other languages than English.
For example, I would like to display "Spain" for 'es' when using english locale and "Espanya" when using spanish locale.
I used this, maybe it suits your needs too:
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSString *countryName = [locale displayNameForKey: NSLocaleCountryCode value: countryCode];
[locale release];
I've found this one, but I suggest you to build your own solution. Free APIs give you limits that maybe limit your app usage Link
OK, apparently it's easier than I though: I'm posting the answer if anyone else will have the same problem:
NSString* name=[[NSLocale currentLocale] displayNameForKey:NSLocaleCountryCode value:#"ca"];
That's it :)
I want to be able to make the app change depending on the users location. Im using the code below:
NSLocale *locale = [NSLocale autoupdatingCurrentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSString *countryName = [locale displayNameForKey: NSLocaleCountryCode value: countryCode];
NSLog(#"countryName %#", countryName);
which works great, but I want to know how the countryName's will be displayed, so I can set up switch case's, which is hard if you dont know how exactly each country is spelt: USA, United States, United States of America, etc. Is there a list of countryCode from Apple, I cant find one.
Also is there a way to make sure the result is in English?
Apple uses the ISO-3166 standard.
"ISO standard ISO-3166" is accurate in most cases, but try selecting "Europe" as a region in iOS settings. You will get a return value of "150". Why "150"? Seems like a region code from here: http://en.wikipedia.org/wiki/UN_M.49. Or from here: http://site.icu-project.org/design/t/territory-region-apis
NSLocale gets its data from CFLocale which in turn gets its data from the
ICU - International Components for Unicode (Apple keep copies here). The file /icuSources/common/uloc.cpp contains almost all the information we usually see returned.
However, /cldrFiles/supplementalData.xml may be the primary source. This file comes from the CLDR - Unicode Common Locale Data Repository.
I want to get current selected iphone language. If using "NSLocale" it returns always the same language. It seems that this is not the one you choose inside iphone settings.
NSLocale * locale = [NSLocale currentLocale];
NSString * localLanguage = [locale objectForKey:NSLocaleLanguageCode];
NSLog (#"Language : %#", localLanguage); // Returns always : "en_US"
How to obtain the current language?
The answer is to check the preferred language :
NSString *preferredLang = [[NSLocale preferredLanguages] objectAtIndex:0];
This question is old, but the reason is new since upgrading to Xcode 6.1
Due to a bug, the iOS8 simulator's [NSLocale currentLocale] always returns "en_US". It is alright on the device though.
Hence, I recommend doing a test on a real device before bashing your head against a wall in frustration.
Sources: https://stackoverflow.com/a/26510914/3099609
https://developer.apple.com/library/ios/releasenotes/DeveloperTools/RN-Xcode/Chapters/xc6_release_notes.html#//apple_ref/doc/uid/TP40001051-CH4-DontLinkElementID_23
Locales encapsulate information about linguistic, cultural, and technological conventions and standards.
Your code returns the "Region Format" information, if set "China", it will return "zh".
Settings | General | International | Region Format
See “NSLocale Class Reference” for more information.
To add to what SteamTrout suggested:
To get your language to respond to changes in iPhone settings you're going to want to use autoupdatingCurrentLocale:
https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSLocale_Class/Reference/Reference.html#//apple_ref/occ/clm/NSLocale/autoupdatingCurrentLocale
If you read closely in the currentLocale reference you'll see that this value will not update from the settings panel:
https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSLocale_Class/Reference/Reference.html#//apple_ref/occ/clm/NSLocale/currentLocale
Try [[NSLocale autoupdatingCurrentLocale] objectForKey:NSLocaleLanguageCode]
In particular I'm interested in shortStandaloneWeekdaySymbols. I'm using this code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSArray *shortWeekdaySymbols = [dateFormatter shortStandaloneWeekdaySymbols];
But if iPhone region format is set to US but language to French/German/any other, NSDateFormatter returns English strings (mon tue ...). But I want to respect the language settings and get weekday names in the current language (standard Clock app does this for example). Is this possible?
The only way I can think of to do this would be to get the current user language (how?) and set locale on the date formatter to this language's region.
The only way you can think of, "get the current user language (how?) and set locale on the date formatter to this language's region" is correct. This is how.
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]] autorelease]];
Try to look here
Detecting current iPhone input language
NSLocale *curentLocale = [NSLocale currentLocale];
NSString *locale = [curentLocale displayNameForKey:NSLocaleIdentifier
value:[curentLocale localeIdentifier]];
NSLog(#"locale = %#",locale);
I'm getting on console locale = English (India) .this comes because my device is from india.
if i take the country with maximum character and language with maximum character, then how much characters a locale can have? this is my question.
You could use [NSLocale availableLocaleIdentifiers] to get an NSArray with all available locale identifiers. Then loop through the array and search for the one with the most characters.
I'd be careful with this, though, and cater for longer strings, because I would imagine that the list of available locales could change in the future.