IOS: date format by device language - ios

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.

Related

How to use correct regional format (date & time) of Windows Phone 8.1

In WP8.1 region settings, one can set a country/region and a regional format. In my case I have United States as the country/region and German (Germany) as the regional format because of Cortana.
However, the time picker as well as the date picker use 12hrs AM/PM format and the US date format mm-dd-yyyy.
I can't find any property which tells me the regional format that's visible in the settings dialog of WP8.1.
Is there any localization/globalization feature of Windows Phone Store apps, that I am missing? How can I get the exact time/date output that's printed in the region settings dialog in my own app, without letting the user select the date/time format itself again?
You can take a look at the Windows.Globalization.DateTimeFormatting namespace on how to get specific formatting date masks.
Also, take a look at the Windows.Globalization(http://msdn.microsoft.com/en-us/library/windows/apps/windows.globalization.aspx) namespace

How to detect correctly language user of device objective-c?

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).

UIDatePicker change locale

I am trying to change locale for UIDatePicker like this:
picker.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
It is working, but after setting locale the picker does not meet the system settings of time format. I mean 24hr/(AM/PM). What I am doing wrong?
In xCode:-
in local section you can set it;
You are not doing anything wrong. Date/time format is dictated by locale. And en_US uses 12hr AM/PM format: http://demo.icu-project.org/icu-bin/locexp?_=en_US
The marked answer is not quite correct if you want to support the time format desired by the user.
If you set the picker locale to the default (that of the device) the picker will use the 12/24 hr format of that particular locale. In the US this would be 12hr and in the UK for example this would be 24hr.
However, if the user changes their device manually to 12/24 your picker will not use this style.
If your picker is only using time and not date+time you can resolve this using a couple of approaches:
Detect if the device is 12/24 manually and then select locale en_US for 12hr and en_GB for 24hr.
Don't set the locale, only set the timeStyle to NSDateFormatterShortStyle, this will automatically choose the time format selected from the device settings.
If you're using the date+time then you need to set a locale to that of the device to get the date in the right language and format but this will then ignore the time format set on the device.

NSDateFormatter: Only show localized dates if app is localized for the language

I'm currently writing an app for the iPhone that heavily works with dates. I'm using NSDateFormatter to convert dates to strings. Conveniently, NSDateFormatter automatically creates strings in the user's language based on the region format.
Because I can't localize my app in all possible languages and regions, the app is only localized in English, German, Spanish and a few others.
When the app is running for a French user for example, the app defaults to English. That is fine, but the dates will still be converted using the French language. This results in strings like "Month: Juillet" instead of "Month: July".
How can I make the NSDateFormatter always use the language the app runs in?
I know that I could use NSLocalizedString() to localize NSLocale identifiers, but this will yield in incorrect region settings.
When the user usually uses a "en_GB" region and my app is localized for English in general, I want the NSDateFormatter to use "en_GB" and not just "en" or "en_US". When the user runs French, I want the locale to be "en" and if he runs the app in the "de_DE" region, I want the date formats to be "de_DE", too, because the app supports German.
Regards,
Fabian
When you create the date formatter, it initialises it's style from the current locale, but you can override this.
[not tested]
You can get the best locale from the users list and your available locale using
NSBundle:
+ (NSArray *)preferredLocalizationsFromArray:(NSArray *)localizationsArray
NSFormatter:
+ (NSString *)dateFormatFromTemplate:(NSString *)template options:(NSUInteger)opts locale:(NSLocale *)locale
Should give you a localised format string for the specified locale.
You can then use setDateFormat to override the initial date format for the formatter.

Xcode iOS Localization - trying to learn how to understand/use location codes

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).

Resources