patchthecode / JTAppleCalendar

The Unofficial Apple iOS Swift Calendar View. Swift calendar Library. iOS calendar Control. 100% Customizable
https://patchthecode.com
MIT License
7.57k stars 812 forks source link

Display numbers on calendar in Arabic. #1313

Closed mansibarodia10057 closed 2 years ago

mansibarodia10057 commented 3 years ago

(Required) Version Number: 8.0.3

Description

Showing numbers in Arabic.

Steps To Reproduce

Whenever I change the device language to Arabic, the numbers still shows up in English. Any code changes I need to make to display numbers in the language selected on the device?

Screen Shot 2021-03-02 at 9 20 27 AM

Expected Behavior

Display numbers in Arabic.

Additional Context

patchthecode commented 3 years ago

does this relaed issue help? https://github.com/patchthecode/JTAppleCalendar/issues/533

mansibarodia10057 commented 3 years ago

@patchthecode: I went through #533 and tried out few things, but still didn't see numerals showing up in Arabic or the selected language.

My custom calendar class already has a dateFormatter defined and I set locale to it in viewDidLoad() i.e. every time the JTAppleCalendar shows up current locale gets set on the dateFormatter. I even tried setting locale on dateFormatter again in 'cellForItemAt' API. Other thing I tried was to use a number formatter in 'cellForItemAt', set locale to it and use that to set the value on the cell.

Sharing snippet of code below:

fileprivate var calendarView: JTAppleCalendarView {
        get {
            return collectionView as! JTAppleCalendarView
        }
    }

    fileprivate let dateFormatter = DateFormatter()

  override func viewDidLoad() {
        dateFormatter.locale = Locale.autoupdatingCurrent
        dateFormatter.setLocalizedDateFormatFromTemplate("MMMM yyyy")

        calendarView.scrollDirection = .vertical
        calendarView.scrollingMode = .none
        calendarView.calendarDataSource = self
        calendarView.calendarDelegate = self
    }

    func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
        let anchorDate = Date() 

        let startDate = Calendar.current.date(byAdding: Calendar.Component.year, value: -10, to: anchorDate)
        let endDate = Calendar.current.date(byAdding: Calendar.Component.year, value: 10, to: anchorDate)

        let parameters = ConfigurationParameters(startDate: startDate!,
                                                 endDate: endDate!,
                                                 numberOfRows: 6,
                                                 generateInDates: InDateCellGeneration.forAllMonths,
                                                 generateOutDates: OutDateCellGeneration.off,
                                                 firstDayOfWeek: DaysOfWeek.sunday,
                                                 hasStrictBoundaries: true)

        return parameters
    }

    func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
        guard let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "calendarMonthCell", for: indexPath) as? CalendarMonthCellView else {
            return JTAppleCell()
        }

        cell.dayLabel.text = cellState.text
        return cell
    }

Number formatter code that I tried:

fileprivate let numberFormatter = NumberFormatter()

func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
        guard let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "calendarMonthCell", for: indexPath) as? CalendarMonthCellView else {
            return JTAppleCell()
        }

        numberFormatter.locale = Locale.autoupdatingCurrent
        cell.dayLabel.text = numberFormatter.string(from: NSNumber(value: Int.init(cellState.text) ?? 00))

        return cell
 }
rs658726 commented 3 years ago

@mansibarodia10057 Add these lines to your cellForItemAt and willDisplay functions:

        numberFormatter.locale = Locale.current
        if let dateInteger = Int.parse(from: cellState.text) {
               cell.dateLabel.text = numberFormatter.string(from: NSNumber(value: dateInteger))
        } else {
              cell.dateLabel.text = cellState.text
        }
mansibarodia10057 commented 2 years ago

I just had to add the above mentioned lines of code to cellForItemAt function and was able to fix this issue. Didn't have to add it to 'willDisplay' function and I noticed it wasn't even getting called. Closing this issue since it's resolved.

patchthecode commented 2 years ago

If will display is not getting called, its because you probably have the incorrect function parameters. Keep in mind that upgrading to the latest library version had a breaking change. The WillDisplay function should look like this

func calendar(_ calendar: JTACMonthView, willDisplay cell: JTACDayCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath)
mansibarodia10057 commented 2 years ago

If will display is not getting called, its because you probably have the incorrect function parameters. Keep in mind that upgrading to the latest library version had a breaking change. The WillDisplay function should look like this

func calendar(_ calendar: JTACMonthView, willDisplay cell: JTACDayCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath)

We are using 8.0.3 version and willDisplay function is same as what you shared. If there is an issue logged for the breaking change, then I will go through it just incase if I am missing anything else in the code anywhere.

patchthecode commented 2 years ago

hmm.. its not getting called... ok, i know you resolved your issue, but if at any point you are free, maybe you can send an empty project sample app that has the problem. I'll take a look