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.
Related
Here is how I am localising days:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *dayFormat = [NSDateFormatter dateFormatFromTemplate:#"EEEE" options:0 locale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:dayFormat];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSString *day = [dateFormatter stringFromDate:date];
And yet it seems to be returning English days of the week (Monday, Tuesday etc) rather than the device language (which has been set to German in the simulator).
Any idea where I'm going wrong?
Update after doing some research on device I've realised that its actually the region setting, not the language setting which changes the date language. Odd, but I guess its done for a reason.
Thanks
The language of the date is set by the region not the language. This has to be a bug. If I'm in Germany, but an English speaker I don't want to have my dates in German, surely?
Anyway, this is why. You have to change language and region.
I've had the same issue. It wouldn't work on the simulator, but it would on a device. Can you try it ? I did not however solve it, I did not even look more into it as it was working perfectly on the device, which is truly the main target of your app.
Edit:
This comes from Apple's doc:
currentLocale
Returns the logical locale for the current user.
+ (id)currentLocale
Return Value
The logical locale for the current user. The locale is formed from the settings for the current user’s chosen system locale overlaid with any custom settings the user has specified in System Preferences.
Discussion
Settings you get from this locale do not change as a user’s
Preferences are changed so that your operations are consistent.
Typically you perform some operations on the returned object and then
allow it to be disposed of. Moreover, since the returned object may be
cached, you do not need to hold on to it indefinitely. Contrast with
autoupdatingCurrentLocale.
Maybe you can try using:
preferredLanguages
Returns the user's language preference order as an array of strings.
+ (NSArray *)preferredLanguages
Return Value
The user's language preference order as an array of NSString objects, each of which is a canonicalized IETF BCP 47 language identifier.
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 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.
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.