UIDatePicker - Problem Localizing
Asked Answered
T

5

14

I've created a UIDatePicker in my app and I also have support for several languages. My UIDatePicker is created in Interface Builder, and I have created a seperate localization XIB so I can customize my UIDatePicker.

Setting the "Locale" option in IB appears to do nothing. Attempting to change my DatePicker programatically with Locale and NSCalender also do nothing via the following code:

NSLocale * locale = [[NSLocale alloc] initWithLocaleIdentifier:@"es_ES"];
datePicker.locale = locale;
datePicker.calender = [locale objectForKey:NSLocaleCalender];

This results in an english picker.

Here's the really weird thing though. The word for "Today" is translated. As seen in the attached screenshot. (OK I'm not allowed to post images. But imagine a Date & Time picker with "May" in English and "Today" written "Ajourd'hui".

Based on what I've read, adding the UIDatePicker programatically doesn't seem to help much.

Tevere answered 24/5, 2010 at 0:41 Comment(0)
T
12

After further investigation, this is actually not handled by the language setting of the iPhone. The UIDatePicker is automatically localized based on the "Region Format" setting and not the language. This is odd because the dates are localized, so you would think the localization of the picker would be linked to the iDevice's language not the Region Format.

Anyway, to simulate the localized DatePicker in the Simulator:

  1. Exit your app, go to Settings.
  2. Select General
  3. Select International
  4. Select the bottom option, Region Format.
  5. Change to the desired region.
  6. Relaunch your app and see the new UI Picker!
Tevere answered 27/5, 2010 at 4:39 Comment(0)
J
3

As of iOS 6 UIDatePicker no longer looks at the device's settings for locale information. You must may set the NSLocale manually or it will use the default locale.

See this answer: https://mcmap.net/q/452236/-can-i-localize-a-uidatepicker

A simple solution:

   datePicker.locale = [NSLocale currentLocale];

Updated

While the locale can now be set on UIDatePicker, by default it is set to 'Default' instead of any particular language. 'Default' will check the device's locale, and set itself to that. (see Dave DeLong's comment below). Therefore, most new projects shouldn't be impacted by this change.

If you are working with older projects, migrating to iOS 6, you may find that the UIDatePickers which are created in xibs have their locale defaulted to 'English' or another language instead of 'Default'. (I've found all of my old projects, pre iOS6, followed this behavior). To fix this, simply change the locale to 'Default' in IB.

Juvenescent answered 19/4, 2013 at 23:41 Comment(2)
The default locale is the device's locale. (ie, the +currentLocale)Disentwine
Great point Dave. I saw your other comment about setting the locale in a xib if created there, 1. I looked a little more into it, and it seems that any UIDatePicker created in newer Xcode versions, by default sets the Locale to 'Default'. But all of my older projects, which included iOS 5 or 4 builds, set the Locale to 'English' by default. So for most new projects this shouldn't be an issue, but older projects moving to iOS 6 only would be wise to check their xibs Locale settings.Juvenescent
P
1

What I am given to understand is that the UIDatePicker automatically shows in the locale the device is set as. I'm not sure how to override it, but it seems like you probably shouldn't override it.

Piercy answered 24/5, 2010 at 3:14 Comment(1)
Sometimes it just needs to be overriden. I.e. one-language-only-application.Sulk
P
0

From my experience the "Today" is following the settings you have in your info.plist. For some reasons they've forgotten to adapt this.

Primalia answered 9/11, 2010 at 21:33 Comment(0)
A
0

You can set the Date picker localization like this(I am using a language manager) in code.

func configurePickers() {
    let languageID = AppLanguageManager.shared.currentLanguage
    let loc = Locale(identifier: languageID)
    [outgoingPicker, backPicker].forEach {
        $0?.locale = loc
        $0?.minimumDate = Date()
    }
}

From interface builder, you also can set date picker localization: enter image description here

Arsonist answered 24/1, 2022 at 20:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.