how to retrive the country code ios example - ios

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];

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];

iOS get locale based description of number

Not sure if this is possible, but for my app I would like to get a locale based string that describes a number.
For example, if I had the number 10,000,000.
In english, I would expect the phrase "Ten Million". However, in Hindi, it would be One crore. Is there any properties in NSNumberFormatter, or NSLocale that could help me with this?
I have checked the docs (NSNumberFormatter, NSLocale), and havent found what I'm looking for yet. Obviously I could write some code to handle these two cases, but I'd like a way that could work for any locale.
Edit: Thanks to leo for the answer! Here is a small snippet of code that will get anyone looking for the same thing started:
NSNumberFormatter formatter = [[NSNumberFormatter alloc] init];
[self.formatter setNumberStyle:NSNumberFormatterSpellOutStyle];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"hi_hi"];
[self.formatter setLocale:locale];
NSNumber * myNumber = [NSNumber numberWithInt:10000];
self.numberLabel.text = [self.formatter stringFromNumber:myNumber];
What you are looking for is the NSNumberFormatterSpellOutStyle style.
NSString* spelledOutString = [NSNumberFormatter localizedStringFromNumber:#10000000 numberStyle:NSNumberFormatterSpellOutStyle];

NSLocale Language Issue

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.

iOS: How to Get the Device Current Language Setting?

There are some features within my application that are supposed to be based on the language settings of the device where it's running.
I want to get the actual language and not some country settings. Foe example, if the language is English, I don't care if it's US, UK, Australia, etc...
I'm familiar with the NSLocale object, but it seems to relate to the Region Format setting and not to the Language setting (see screen shot below) so when I try to retrieve the language out of it using [locale displayNameForKey:NSLocaleIdentifier value:[locale localeIdentifier] I get things like English (United States) instead of English; also, I think that what I need is the Language data and not the Region Format (am I right?).
Can anyone direct me to how to retrieve the language setting?
User preferred languages are stored can be retrieved from locale as array and current language identifier is the first object in that array:
NSString *currentLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
If you want language in more readable form then use displayNameForKey:value: method of NSLocale:
NSString *langID = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString *lang = [[NSLocale currentLocale] displayNameForKey:NSLocaleLanguageCode value:langID];
Try this:
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSArray* arrayLanguages = [userDefaults objectForKey:#"AppleLanguages"];
NSString* currentLanguage = [arrayLanguages objectAtIndex:0];
Getting language and region in Swift:
LF.log("language", NSLocale.preferredLanguages())
LF.log("locale", NSBundle.mainBundle().preferredLocalizations)
In my case I'm getting:
language: '(
"zh-Hans"
)'
locale: '(
en
)'
In Swift 4:
let currentLanguage = Locale.current.languageCode
It will give you just the language code, no country code.
Swift:
let language = NSBundle.mainBundle().preferredLocalizations[0] as NSString
Working solution:
let language = NSLocale.preferredLanguages()[0]
let languageDic = NSLocale.componentsFromLocaleIdentifier(language) as NSDictionary
//let countryCode = languageDic.objectForKey("kCFLocaleCountryCodeKey")
let languageCode = languageDic.objectForKey("kCFLocaleLanguageCodeKey") as! String
print(languageCode)
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
Find the solution in XCode's helper document, it wrote:
Getting the Current Language
To get the language that the app is using from the main application bundle, use the preferredLocalizations method in the NSBundle class:
NSString *languageID = [[NSBundle mainBundle] preferredLocalizations].firstObject;
Use below code to fetch Localised language without having trouble to the en-india, en-us etc..
NSString *Ph = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
In and After ios9 this code need to take in cosideration
To know the current language selected within your localizations use
[[NSBundle mainBundle] preferredLocalizations]
Example:
NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
To get two letter word
NSString *language = [[[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0] substringToIndex:2];
Swift:
let language = NSBundle.mainBundle().preferredLocalizations.first as NSString

Resources