I have a US Phone and I'm localizing my app. I have set the preferences > General > International to French.
My app comes up in French. Great.. I used the code here to see the language codes.
The code is:
NSString *locale = [[NSLocale currentLocale] localeIdentifier];
//NSString *language = [[NSLocale currentLocale] objectForKey: NSLocaleLanguageCode];
//NSString *countryCode = [[NSLocale currentLocale] objectForKey: NSLocaleCountryCode];
However, the console comes back with en-US. I guess I was expecting fr-US. Can someone explain why it's "en"..
Thanks..
If you use [[NSLocale preferredLanguages] objectAtIndex:0], you will get your currently selected language
language is different to region format.
NSLocale is not related to language. NSLocale provides a way to tailor your programs behavior to the what is customary for particular regions. This includes formatting number, dates, and currency, for example.
Locales encapsulate information about linguistic, cultural, and
technological conventions and standards. Examples of information
encapsulated by a locale include the symbol used for the decimal
separator in numbers and the way dates are formatted.
Locales are typically used to provide, format, and interpret
information about and according to the user’s customs and preferences.
They are frequently used in conjunction with formatters (see Data
Formatting Guide).
[NSLocale currentLocale] depends on where the device is located, an dis independent of the language that you pick in your phone's settings.
As Audun said about getting the devices current language, you can get that from [[NSLocale preferredLanguages] objectAtIndex:0]. You can look at other members of that array, and it will show you the language seettings that were most recently used first (followed by all the other languages that the device supports, in no particular order, as far as I know).
Related
I have a query about iOS. I want to detect language of my device, for change any texts in my app if language is english and other if isn't english. But when I detect language, always detect region and not detect language. In others words if I go to change language, this don't change. If I change region, language changes.
My code is:
NSString *userLocale = [[NSLocale currentLocale] localeIdentifier];
NSString *userLanguage = [userLocale substringToIndex:2];
NSLog(#"%#", userLanguage); // return format "en", "es"... (english or spanish)
Could be that this problem appear because with [NSLocale currentLocale] get en_Us and this parameters depends of region?
Thanks!
If you want the language try
[NSLocale preferredLanguages];
It will return an array of the user's language preference and the most preferred language will be the first in the list.
NSLocale encapsulates a lot more than just the current language. Things like currency identifier and what to use for the decimal separator are just a few. A Spanish speaking user in the northwestern United States may want to see things in Spanish but would probably still expect to see the $ for currency.
With that said I suspect what your really looking for is full fledged localization support in which case there is tons of information out there on how to localize your app. The macro
NSLocalizedString
and its siblings allow you to write language agnostic code (for the most part).
I am developing an iPhone app. At one point, the user types phone numbers. These can be in any format, international or local. Local format would be something like 514-123-4567 (or worse, just 123-4567), is there a way to convert local phone numbers to the international format (E.164): +15141234567? This means finding out what the country is (and perhaps the region too, if possible).
One way would be to require the user to always enter phone numbers using the international format, or to select the country she's in. But if I can avoid that, I think it would be more user friendly.
I would also like to avoid asking the user for the authorization to geolocate her: a lot of users refuse geolocation.
Ideally, I would like to get the country and region of her own phone number. Or perhaps the current carrier's country (in case the user is roaming in another country than hers).
But what if she installed the app on an iPod or iPad without a SIM card? Maybe I could use the locale? Or I could try to geolocate the IP address?
Any better ideas?
For "converting" phone numbers - you have an example here: STPhoneFormatter
You can use NSLocale to determine how to localize your customer - but not allays determine where the user is (locale and language are both user-configurable). If you really want to find out what country the phone is in right now, use geolocation.
here a NSLocale example for countryCode/country:
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSString *country = [locale displayNameForKey: NSLocaleCountryCode value: countryCode];
Would any one let me know whether or not, we have an API for Currency Converting. I can't convert it mathematically because the currency rates changes too frequently.So probably i need to parse some string.It would be great if you come up with some cool ideas to do the same.
Also i need an API which would generate a list of Different Currencies' across the World,as of-course, I cannot use an array to populate the list.
Getting a list of currencies is trivial:
NSArray* currencyISOCodes = [NSLocale commonISOCurrencyCodes]
...
NSLocale* locale = [NSLocale currentLocale];
NSString* currencyName = [locale displayNameForKey:NSLocaleCurrencyCode
value:currencyISOcode];
Currencies don't change often and the list will be updated with new iOS system update.
Currency conversion is not trivial but there are MANY free server APIs you can use, e.g. Google Finance API (How do I get currency exchange rates via an API such as Google Finance?).
In my app I should set a label with a date (today), but the problem is that format date change in some countries; then I want to know waht's the way to obtain information about device language or other information that say me that device is used in usa or france or italy or exc...
can you help me?
Try dateFormatFromTemplate:options:locale: in NSDateFormatter
Returns a localized date format string representing the given date
format components arranged appropriately for the specified locale.
For example, you can specify a template like "MMMM D, YYYY" and it will change the order of the terms for the locale that you specify.
UPDATE
If you want to know the language and the region, then you should try using this:
[NSLocale preferredLanguages] objectAtIndex:0]
for the current language. (Languages are coded and in the order of most recently used first.)
[NSLocale currentLocale] localeIdentifier]
for the region. Locale identifiers are coded (like it_IT for Italy). For a complete list of coded locales, use [NSLocale availableLocaleIdentifiers]
See the documentation for NSLocale to find out more about language and location.
I have a special requirement to learn whether the current locale uses comma-based or period-based numbers. Is there a simple way to learn this?
- (NSString *) getDecimalSeparator
{
return [[NSLocale currentLocale] objectForKey:NSLocaleDecimalSeparator];
}
Will return current separator. I use it for generating valid CSV files.
I suggest taking a look at the NSNumberFormatter class:
NSNumberFormatter class reference
Programming guide on doing this through CoreFoundation:
Data formatting programming guide
Its good that you are spending time on this, in my application I made use of my own timestamp format, while the system allows you to pick your own timestamp format... needless to say I had some angry users.