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 811 forks source link

Localize calendar date cells? #970

Closed cluelessoodles closed 5 years ago

cluelessoodles commented 5 years ago

IMPORTANT - BEFORE CREATING YOUR ISSUE PLEASE READ THE FOLLOWING:

  1. PLEASE STATE THE VERSION NUMBER you are using.
  2. Are you using the latest version of JTAppleCalendar? Latest version is currently 7.1.6

Using 7.1.6

  1. PROVIDE ENOUGH INFORMATION SO I CAN RECREATE THE PROBLEM.

I have localized my app to several non-latin languages and everything works except the dates in the calendar which remain in English. Is there a built-in way to localize the collectionview calendar cell dates? Year and month are localizing just fine, but for some reason, the cell dates are not.

Here's what I have in the method that sets up the cell:

func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
        let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "CalendarCell", for: indexPath) as! CalendarCell
        cell.dateLabel.text = cellState.text
        configureCalendarCell(cell:cell, cellState: cellState)
        return cell
}
patchthecode commented 5 years ago

Can you paste the code of your configureCalendar function?

cluelessoodles commented 5 years ago

Yes, it is:

    //Mark: Configure Calendar Cell
    func configureCalendarCell(cell: JTAppleCell?, cellState: CellState) {
        guard let myCustomCell = cell as? CalendarCell else { return }
            handleCellTextColor(cell: myCustomCell, cellState: cellState)
            handleCellVisibility(cell: myCustomCell, cellState: cellState)
            handleCellSelected(cell: myCustomCell, cellState: cellState)
            getEventDates(cell: myCustomCell, cellState: cellState)
    }

    //Mark: Calendar set cell selection colors, today's Date selection
    func handleCellTextColor(cell: CalendarCell, cellState: CellState) {
        cell.dateLabel.textColor = cellState.isSelected ? UIColor.primaryOrange : UIColor.white

        formatter.dateFormat = "yyyy MM dd"
        formatter.timeZone = Calendar.current.timeZone
        formatter.locale = Calendar.current.locale
        let todaysDateString = formatter.string(from: todaysDate)
        let monthDateString = formatter.string(from: cellState.date)
        if todaysDateString == monthDateString {
            cell.dateLabel.layer.cornerRadius = 25
            cell.dateLabel.clipsToBounds = true
            cell.dateLabel.backgroundColor = cellState.isSelected ? UIColor.clear : #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0.5)
        } else {
        }
    }

    //Mark: Calendar hide outside Month dates
    func handleCellVisibility(cell: CalendarCell, cellState: CellState) {
        cell.isHidden = cellState.dateBelongsTo == .thisMonth ? false : true
    }

    //Mark: Calendar cell selection
    func handleCellSelected(cell: CalendarCell, cellState: CellState) {
        cell.selectedView.isHidden = cellState.isSelected ? false : true
    }
patchthecode commented 5 years ago

I meant the code for the configureCalendar function

func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters
patchthecode commented 5 years ago

take a look at the configureCalendar function in my sample app attached to this github project.

    func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {

        formatter.dateFormat = "yyyy MM dd"
        formatter.timeZone = testCalendar.timeZone
        formatter.locale = testCalendar.locale

        let startDate = formatter.date(from: "2018 01 01")!
        let endDate = formatter.date(from: "2019 02 01")!

        let parameters = ConfigurationParameters(startDate: startDate,
                                                 endDate: endDate,
                                                 numberOfRows: numberOfRows,
                                                 calendar: testCalendar,
                                                 generateInDates: generateInDates,
                                                 generateOutDates: generateOutDates,
                                                 firstDayOfWeek: firstDayOfWeek,
                                                 hasStrictBoundaries: hasStrictBoundaries)
        return parameters
    }

You can see that i return a ConfigurationParameters object. There is a parameter of that object called calendar.

This is a Calendar() object. You create a calendar object, configure that calendar object to what ever geo/locale/etc. Then pass that calendar object to the library just like i did.

cluelessoodles commented 5 years ago

Oh I see. I do have something similar but it's not working. I went through the sample project code to see if you used testCalendar to set up the dates anywhere but I'm not seeing anything other than where you use dateComponent to get the month is the dateLabel.text is 1.

patchthecode commented 5 years ago

in the sample project, its in the TestPersianCalendar.swift file.

here i configure a persian calendar.

    func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {

        let persianCalendar = Calendar(identifier: .persian)

        let testFotmatter = DateFormatter()
        testFotmatter.dateFormat = "yyyy/MM/dd"
        testFotmatter.timeZone = persianCalendar.timeZone
        testFotmatter.locale = persianCalendar.locale

        let startDate = testFotmatter.date(from: "2017/01/01")!
        let endDate = testFotmatter.date(from: "2017/09/30")!

        let parameters = ConfigurationParameters(startDate: startDate, endDate: endDate, calendar: persianCalendar)
        return parameters
    }

I left the language in english so that it will be easier to debug for me.

    func calendar(_ calendar: JTAppleCalendarView, willDisplay cell: JTAppleCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
        // The code here is the same as cellForItem function
        let cell = cell as! CellView
        cell.dayLabel.text = cellState.text
        ...
        ...
    }
patchthecode commented 5 years ago

Paste me one last thing. Paste me the code for the function where you set the text to the cell.

I want to see what code you used there which you think should change the language. Because to set the language text for your cell, your code should be something like this

let testCalendar = Calendar(...... // create a islamic calendar here)
let dateFormatter = DateFormatter()
dateFormater.calendar = testCalendar
let date = cellState.date
let islamicTextDate = dateFormatter.string(from: date)

keep in mind that you should not declare testCalendar inside of the delegate function, because it will be instantiated too many times.

cluelessoodles commented 5 years ago
  1. I've been fiddling with this for a bit but am still running into some issues. I'm trying to get the calendar to appear as Calendar(identifier: .indian). There isn't much in the description in apple documents for what this calendar is supposed to look like, but I'm assuming that if I change my app's language to Hindi and set Calendar(identifier: .indian), then it should show me Hindi numbers in the calendar. Is that assumption correct? Is that what it does for .persian? Or do the calendar date numbers remain in English?

  2. When I set var myCalendar = Calendar(identifier: .indian), the calendar starts at March '19 instead of the current month of January. In my code, I have it set to show and scroll from 2019-01-01 to 2020-01-31 but it instead shows from March 2019 to Feb 2020. Apparently, the Indian calendar year starts with the western month of March and ends in February. Still, I'm not sure why it wouldn't scroll to today's date anyway since that is the date range I have coded.

patchthecode commented 5 years ago

The most i can do is show you an example of how to display a date in persian.

        let date = Date()
        let df = DateFormatter()
        df.locale = Locale(identifier: "fa_IR")
        df.calendar = persianCalendar
        df.dateFormat = "dd"
        print(df.string(from: date))

I guess you can apply the same for hindi.

patchthecode commented 5 years ago

closing this issue. assumed resolved.