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 :)
Related
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.
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!
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]
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.
I would like my iPhone app to allow the input, display and storage of currency amounts using the appropriate symbol ($, €, ₤, ¥, etc) for the user.
Would NSNumberFormatter do everything I need? What happens when a user switches their locale and these amounts (dollars, yen, etc.) are stored as NSDecimalNumbers. I assume, to be safe, it's necessary to somehow capture the locale at the time of entry and then the currency symbol and store them in my instance along with the NSDecimalNumber ivar so they can be unwrapped and displayed appropriately down the road should the user changed their locale since the time when the item was created?
Sorry, I have little localization experience so hoping for a couple quick pointers before diving in. Lastly, any insight on how to you handle this kind of input given the limitations of the iPhone's keyboards?
NSNumberFormatter is definitely the way to go! You can set a NSLocale on the
NSNumberFormatter, the formatter will automatically behave according to that locale. The default locale for a number formatter is always the currency for the users selected region format.
NSDecimalNumber *someAmount = [NSDecimalNumber decimalNumberWithString:#"5.00"];
NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSLog(#"%#", [currencyFormatter stringFromNumber:someAmount]);
This will log the amount '5.00' according to the users default region format. If you want to alter the currency you can set:
NSLocale *aLocale = [[NSLocale alloc] initWithLocaleIdentifier: "nl-NL"]
[currencyFormatter setLocale:aLocale];
Which will choose the default currency for that locale.
Often though you're not charging in your user's local currency, but in your own. To force NSNumberFormatter to format in your currency, while keeping the number formatting in the user's preference, use:
currencyFormatter.currencyCode = #"USD"
currencyFormatter.internationalCurrencySymbol = #"$"
currencyFormatter.currencySymbol = #"$"
In en-US this will format as $5.00 in nl-NL it's $ 5,00.