Hey guys! ```fun LocalDate.toFormattedDateString()...
# multiplatform
j
Hey guys!
Copy code
fun LocalDate.toFormattedDateString(): String =
    format(LocalDate.Format {
        dayOfWeek(DayOfWeekNames.ENGLISH_ABBREVIATED)
        chars(", ")
        dayOfMonth()
        char(' ')
        monthName(MonthNames.ENGLISH_ABBREVIATED)
        char(' ')
        year()
    })
How can I make
dayOfWeek
and
monthName
be translated based on the device language? I'm using kotlinx-datetime.
y
u probably need an expect, actual function for each platform
j
Why?
Currently, the
kotlinx-datetime
library does not support locale-aware formatting for date components like day names and month names. The formatting functions provided by
kotlinx-datetime
are locale-invariant, meaning they do not adapt to different device languages or regional settings. This limitation is acknowledged in the library's discussions, where it's noted that a good date-time formatting API should make it easy to write correct code for both machine-human and machine-machine communication, which typically involves locales.
✔️ 1
👍 1
j
Do you have any code demonstration of how I could use the expect and actual?
y
i don't, are u familiar with the concept of expect, actual?
if yes then u just need to use platform-specific libraries that offer comprehensive localization support
j
ok, thank you!
I'll search how can I do this, mainly in iOS
👍 1
l
Hi @João Henrique de Souza Silva, I think this could help you. In common main
Copy code
expect fun LocalDate.formatDateToLocale(): String
In android side
Copy code
actual fun LocalDate.formatDateToLocale(): String {
    val formatter = DateTimeFormatter
        .ofLocalizedDate(FormatStyle.MEDIUM)
        .withLocale(Locale.getDefault())
    return this.toJavaLocalDate().format(formatter)
}
In iOS side
Copy code
actual fun LocalDate.formatDateToLocale(): String {
    val formatter = NSDateFormatter().apply {
        dateStyle = NSDateFormatterMediumStyle
        timeStyle = NSDateFormatterNoStyle
        locale = NSLocale.currentLocale
    }

    val calendar = NSCalendar.currentCalendar
    val nsDate = calendar.dateFromComponents(this.toNSDateComponents()) ?: return ""
    return formatter.stringFromDate(nsDate)
}
⬆️ 2
j
Thank you, @Lucas Prioste, for your code. I also created another function that receives a pattern and uses
expect
and
actual
to implement it on both platforms.
Copy code
enum class FormatStyle {
    FULL,
    LONG,
    MEDIUM,
    SHORT,
}
Copy code
import com.my.path.FormatStyle
import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.toJavaLocalDate
import java.time.format.DateTimeFormatter
import java.util.Locale
import java.time.format.FormatStyle as FS

actual fun LocalDate.formatDateToLocale(style: FormatStyle): String {
    val style = when (style) {
        FormatStyle.FULL -> FS.FULL
        FormatStyle.LONG -> FS.LONG
        FormatStyle.MEDIUM -> FS.MEDIUM
        FormatStyle.SHORT -> FS.SHORT
    }

    val formatter = DateTimeFormatter
        .ofLocalizedDate(style)
        .withLocale(Locale.getDefault())
    return this.toJavaLocalDate().format(formatter)
}

actual fun LocalDate.formatDateToLocale(pattern: String): String {
    val formatter = DateTimeFormatter
        .ofPattern(pattern)
    return this.toJavaLocalDate().format(formatter)
}

actual fun LocalDateTime.formatDateToLocale(style: FormatStyle): String =
    date.formatDateToLocale(style)
Copy code
actual fun LocalDate.formatDateToLocale(style: FormatStyle): String {
    val style = when (style) {
        FormatStyle.FULL -> NSDateFormatterFullStyle
        FormatStyle.LONG -> NSDateFormatterLongStyle
        FormatStyle.MEDIUM -> NSDateFormatterMediumStyle
        FormatStyle.SHORT -> NSDateFormatterShortStyle
    }
    val formatter = NSDateFormatter().apply {
        dateStyle = style
        timeStyle = NSDateFormatterNoStyle
        locale = NSLocale.currentLocale
    }

    val calendar = NSCalendar.currentCalendar
    val nsDate = calendar.dateFromComponents(this.toNSDateComponents()) ?: return ""
    return formatter.stringFromDate(nsDate)
}

actual fun LocalDate.formatDateToLocale(pattern: String): String {
    val formatter = NSDateFormatter().apply {
        dateFormat = pattern
        locale = NSLocale.currentLocale
    }

    val calendar = NSCalendar.currentCalendar
    val nsDate = calendar.dateFromComponents(this.toNSDateComponents()) ?: return ""
    return formatter.stringFromDate(nsDate)
}

actual fun LocalDateTime.formatDateToLocale(style: FormatStyle): String =
    date.formatDateToLocale(style)
👌 2