Can I localize a UIDatePicker?
Asked Answered
B

5

9

I'm trying to localize a UIDatePicker. Apple's docs say that it should autodetect the current locale, yet the language stays the same, no matter what language I select. What do I have to do?

Boyce answered 20/7, 2009 at 0:56 Comment(0)
T
11

The locale property (and all other country specific format properties for that matter) defaults to the value returned by currentLocale, which in turns depends on the iphone's country settings, not the language settings. You need to set the appropriate country in the iphone general settings.

Truncheon answered 15/2, 2010 at 12:49 Comment(2)
locale for NSDatepicker is deprecated in ios 5.1Sourdine
Check kraag22 and Dave DeLong's comments at FunkyKat answer belowDisaccustom
T
5

NSLocale in UIDatePicker is back since iOS 6. Phone settings in iOS 6 are ignored and used en_US locale by default, so you must set locale in code

Tsosie answered 19/10, 2012 at 13:58 Comment(2)
if you set locale property in iOS 5.1.1 it works well too. Phone/Pad settings are ignored tooUnstep
If you create a UIDatePicker programmatically, it'll be initialized with [NSLocale currentLocale]. If you're creating it in a xib, then make sure the value of the "Locale" attribute is "Default", and not "English (United States)".Swanson
P
2

From my experience, in iOS5 UIDatePicker seems to ignore the set locale and use the locale provided from the general settings. But it should work fine in iOS6.

Photometry answered 13/11, 2012 at 15:5 Comment(1)
Yes, I did. It was an application developed entirely for Arabic. I set the locale from the storyboard and viewDidLoad and the date picker 's locale was English, under iOS5, as the simulator's settings were. However, it worked for iOS6 just from the storyboard without setting it from viewDidLoad. Sorry for the late reply, I just saw your comment.Photometry
H
0

the UIDatePicker has a locale property; if you don't set it it should default to the user's preferred locale. When you say "no matter what language I select" what do you mean--are you trying to change the locale programmatically, or are you changing the device's default locale using the Settings page?

Holoblastic answered 20/7, 2009 at 1:18 Comment(3)
I'm referring to the International setting in Preferences. I have everything else set up to be an NSLocalizedString, and it all translates wonderfully. However, say I set the language to French in Preferences. My UIDatePicker will not display months in French. I have not set the locale at all, so I figured that this should display the correct locale. However, it's not.Boyce
how are you initializing the date picker? are you leaving the locale and calendar properties set to nil, or are you setting them to something?Holoblastic
Also, I'm initializing the datePicker completely by code. Here's my code on pastebin. pastebin.com/m51df66f7Boyce
B
0

To show the picker with the current language setted in the device, use this:

@property (weak, nonatomic) IBOutlet UIDatePicker *pickerView;

[_pickerView setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale currentLocale] localeIdentifier]]];
Bead answered 29/1, 2020 at 10:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.