swiftuidatepickereureka-forms

DateRow local does not change


I'm building a FormViewController thanks to Eureka forms framework but I ran into a problem for my DateRow. I'd like the UIDatePicker to be displayed in French but months always appear in english.

I tried to set up another UIDatePicker in French on another view controller and it worked normally :

let date = UIDatePicker()
date.datePickerMode = .date
date.locale = Locale(identifier: "fr")
date.frame = CGRect(x: 50, y: 200, width: 300, height: 200)
view.addSubview(date)

French Date Picker

The language is changing thanks to the the UIDatePicker local property so I tried to add this property where the date picker is set in Eureka files (because i don't think we can access the Date picker in the DateRow object).

open class DateCell: Cell<Date>, CellType {

public var datePicker: UIDatePicker

public required init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    datePicker = UIDatePicker()
    super.init(style: style, reuseIdentifier: reuseIdentifier)
}

required public init?(coder aDecoder: NSCoder) {
    datePicker = UIDatePicker()
    super.init(coder: aDecoder)
}

open override func setup() {
    super.setup()
    accessoryType = .none
    editingAccessoryType =  .none
    datePicker.datePickerMode = datePickerMode()
    datePicker.addTarget(self, action: #selector(DateCell.datePickerValueChanged(_:)), for: .valueChanged)
    datePicker.locale = Locale(identifier: "fr")
}

But the date picker always stays in english.

Do you have any ideas where I should add the local line ?

Thanks in advance !


Solution

  • You can access the datePicker of the DateRow as cell.datePicker. To change its locale use the cellSetup callback. You might also want to set the formatter of the row so that the value is displayed with the correct locale:

    <<< DateRow(){
        $0.value = Date()
        let formatter = DateFormatter()
        formatter.locale = Locale(identifier: "fr")
        formatter.dateStyle = .long
        $0.dateFormatter = formatter
    }.cellSetup({ (cell, row) in
        cell.datePicker.locale = Locale(identifier: "fr")
    })