UIDatePicker change locale - ios

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.

Related

Prevent dates from being converting when localizing in iOS

I am localizing an app. The app makes calls to an API that I do not have control over. When posting a request to the API, one of the expected values is a date string in the following format:
"Fri, 15 Mar 2019 15:53:14"
When switching application language to Spanish the date is applied to the date parameter as:
"vie, 15 mar 2019 15:48:42"
which makes sense. However, it causes the API to fail. If I had control over it, I would be passing the date in seconds, rather than a string, but unfortunately I do not own the API.
Does anyone know how to prevent dates from being converted when localizing?
Thanks!
On most the formatters, there is locale property which is the locale of the device by default. You can modify it :
dateFormatter.locale = Locale(identifier: "en_US")
Thanks to rmaddy
Prefer "en_US_POSIX" which is invariant in time.
if the US, at some point in the future, changes the way it formats
dates, "en_US" will change to reflect the new behaviour, but
"en_US_POSIX" will not)
https://developer.apple.com/library/archive/qa/qa1480/_index.html

localizing + customizing the date format of a UIDatePicker

I need to customize and localize the date format of a UIDatePicker's DateAndTime mode, so that it will display the date numbers in Arabic language plus time in 24 hours format and in Arabic numbers too.
the equivalent row in English would be: 2017 11 15 17:00
I've tried all the solutions proposed on SO from changing the locale to changing device language but to no avail.
Let's breaking it down:
For displaying the content of the date picker to be in Arabic language you'd need to set it your desired locale:
The locale used by the date picker.
For changing the format of the date picker, well... it depends on the used locale; As mentioned in the UIDatePicker - Internationalization:
Date pickers handle their own internationalization; the only thing you
need to do is specify the appropriate locale. You can choose a
specific locale for your date picker to appear in by setting the
Locale (locale) field in Attributes Inspector. This changes the
language that the date picker uses for display, but also the format of
the date and time (for example, certain locales present days before
month names, or prefer a 24-hour clock over a 12-hour clock)...
which means that setting the desired locale for changing the language also affects the format date of the date picker.
Roughly speaking, setting both language and format is unavailable for your case, you should choose the desired language or the desired date format.
However, applying the first point (changing the date picker locale):
datePicker.locale = Locale(identifier: "ar")
should leads to the following output:
Also, the following question:
Show Time in 12 and 24 hour format in UIDatePicker on the basis of app settings not on device Settings
is also related to changing the date picker format.

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

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.

IOS: date format by device language

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.

Resources