JTAppleCalendar icon indicating copy to clipboard operation
JTAppleCalendar copied to clipboard

Display numbers on calendar in Arabic.

Open mansibarodia10057 opened this issue 3 years ago • 3 comments

(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

mansibarodia10057 avatar Mar 02 '21 17:03 mansibarodia10057

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

patchthecode avatar Mar 03 '21 14:03 patchthecode

@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
 }

mansibarodia10057 avatar Mar 03 '21 21:03 mansibarodia10057

@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
        }

rs658726 avatar Apr 23 '21 12:04 rs658726

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.

mansibarodia10057 avatar Oct 19 '22 18:10 mansibarodia10057

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)

patchthecode avatar Oct 20 '22 14:10 patchthecode

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.

mansibarodia10057 avatar Oct 20 '22 15:10 mansibarodia10057

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

patchthecode avatar Oct 21 '22 14:10 patchthecode