darkokoa / datetime-wheel-picker

Wheel Date & Time Picker in Compose Multiplatform
102 stars 4 forks source link

Support Locale in WheelDatePicker #20

Open easyhooon opened 2 months ago

easyhooon commented 2 months ago

image

As shown in the picture above, with the previous library WheelPickerCompose, we could confirm that the text for the month was applied according to the Locale settings. (Locale.KOREAN applied)


// Screen Component

WheelDatePicker(
                    modifier = Modifier.padding(horizontal = 15.dp),
                    startDate = uiState.mateRecruitDate.parseToLocalDate(),
                    minDate = LocalDate.now(),
                    maxDate = LocalDate.of(2030, 12, 31),
                    rowCount = 5,
                    textStyle = Large20_Mid,
                    textColor = Gray001,
                    selectorProperties = WheelPickerDefaults.selectorProperties(
                        enabled = true,
                        shape = RoundedCornerShape(10.dp),
                        color = Gray010,
                        border = BorderStroke(2.dp, Gray010),
                    ),
                ) { snappedDate -> run { selectedDate = snappedDate.formatToDate() } }

// Utils.kt

import java.time.LocalDate
import java.time.LocalTime
import java.time.format.DateTimeFormatter
import java.util.Locale
fun LocalDate.formatToDate(): String {
    val formatter = DateTimeFormatter.ofPattern("yyyy년 MM월 dd일", Locale.KOREAN)
    return this.format(formatter)
}

fun String.parseToLocalDate(): LocalDate {
    val formatter = DateTimeFormatter.ofPattern("yyyy년 MM월 dd일", Locale.KOREAN)
    return LocalDate.parse(this, formatter)
}
image

However, in the current library that supports Kotlin Multiplatform, these Locale properties are not applied, causing the month's text to always be displayed in English.

For some reason, the kotlinx-datetime library doesn't seem to support Java.util.Locale provided by Java (I might be mistaken about this), so Locale is not being applied. Could you possibly explain why this is the case? Also, I'm curious if this is a limitation of Kotlin or the kotlinx-datetime library.

darkokoa commented 2 months ago

You're right, Locale isn't working for this lib yet. Here's why:

When I migrated the original lib to multiplatform, I didn't include support for localization (L10N). That's why you're seeing month names based on the Month enum instead of localized names.

I originally skipped L10N partly because Kotlin-Datetime didn't have built-in Locale support. So, I decided to put L10N on the back burner for a while.

Long story short, I'm the one who limited the L10N. But I'm actively working on adding proper L10N support soon.