Is there a way to make a completely Turkish DatePickerDilog in kotlin compose?

121 Views Asked by At

I made a Turkish DatePickerDialog, but it was not completely Turkish as you can see the below picture I share.

Even though I do Turkish in this part.

// Set the locale to Turkish
    Locale.setDefault(Locale("tr"))

enter image description here

as you can see august is english and days are english. How can I make them in Turkish?

hear is my code:

@Composable
fun TurkishDatePickerDialog(
    year: Int?,
    month: Int?,
    dayOfMonth: Int?,
    isShowDatePicker: Boolean,
    onDismiss: () -> Unit,
    onDateSelected: (formattedDate: String,formattedDisplayDate:String, year: Int, month: Int, day: Int) -> Unit
) {

    val calendar = Calendar.getInstance().apply {
        set(Calendar.YEAR, year ?: Calendar.YEAR)
        set(Calendar.MONTH, month?.minus(1) ?: Calendar.getInstance().get(Calendar.MONTH))
        set(Calendar.DAY_OF_MONTH, dayOfMonth ?: Calendar.DAY_OF_MONTH)
    }
    val year = calendar.get(Calendar.YEAR)
    val month = calendar.get(Calendar.MONTH)
    val day = calendar.get(Calendar.DAY_OF_MONTH)

    // Set the locale to Turkish
    Locale.setDefault(Locale("tr"))

    val datePickerDialog = DatePickerDialog(
        LocalContext.current,
        { _, year, month, dayOfMonth ->
            Calendar.getInstance().apply {
                set(year, month, dayOfMonth)
                val formattedMonth = if (month < 9) "0${month + 1}" else "${month + 1}"
                val formattedDayOfMonth = if (dayOfMonth < 10) "0$dayOfMonth" else "$dayOfMonth"
                val formattedDate = "$year-$formattedMonth-$formattedDayOfMonth"
                val formattedDisplayDate = "$formattedDayOfMonth/$formattedMonth/$year"
                onDateSelected(formattedDate,formattedDisplayDate, year, month + 1, dayOfMonth,)
            }
        },
        year,
        month,
        day
    )

    datePickerDialog.setOnShowListener {
        val cancelButton = datePickerDialog.getButton(DialogInterface.BUTTON_NEGATIVE)
        cancelButton.text = "İptal"
        val okButton = datePickerDialog.getButton(DialogInterface.BUTTON_POSITIVE)
        okButton.text = "Tamam"
    }

    if (isShowDatePicker)
        datePickerDialog.show()


    datePickerDialog.datePicker.maxDate = System.currentTimeMillis()

    // Set the OnDismissListener to detect when the user cancels the dialog
    datePickerDialog.setOnDismissListener {
        // Perform some action when the user cancels the dialog
        onDismiss()
    }
}
1

There are 1 best solutions below

2
Edward Moya On

Try to use the Turkish locale for both the DatePickerDialog and the DatePicker inside it