9

I use the kotlinx-datetime library to get and compare with the user's current date.

I ran into a problem that:

SomeInstant.toLocalDateTime(TimeZone.currentSystemDefault()).month.name

Returns the name of the month in English, for example: January.

How can I change the month name for the user's locale? For example, janvier in French.


I want to use this library only, without resorting to java.time classes on the Java platform.

2 Answers 2

7
+100

There's an open discussion about on GitHub about how to provide localized date-time formats. For now, a complete solution isn't possible in pure Kotlin.

If you want the ability to format dates for any user in any language, you'll need to rely on the tools from Android, Java, or whatever other platform you're running on.

However, if you know where your users are from, you do have some options. When building a DateTimeFormat, you can supply your own list of MonthNames.

val instant = Clock.System.now()
val localDateTime = instant.toLocalDateTime(TimeZone.currentSystemDefault())

val frenchMonths = listOf(
  "janvier", "février", "mars", "avril", "mai", "juin",
  "juillet", "aout", "septembre", "octobre", "novembre", "décembre"
)

val format = LocalDateTime.Format {
  dayOfMonth()
  char(' ')
  monthName(MonthNames(frenchMonths))
}

println(format.format(localDateTime)) // 30 juillet

You'd need to do this separately for each language that you want to support, unless you can find somewhere to get them programmatically.

Of course, one place to get a list of month names would be from Java. You said you don't want to resort to java.time classes at all, but what about a hybrid approach? Java can provide the month names, and Kotlin can do the formatting.

In common code:

expect fun getMonthNames(languageTag: String): List<String>

val frenchMonths = getMonthNames("fr")

val format = LocalDateTime.Format {
  dayOfMonth()
  char(' ')
  monthName(MonthNames(frenchMonths)) 
}

And on the JVM:

actual fun getMonthNames(languageTag: String): List<String> {
  val locale = Locale.forLanguageTag(languageTag)
  val symbols = DateFormatSymbols.getInstance(locale)
  return symbols.months.take(12)
}
Sign up to request clarification or add additional context in comments.

2 Comments

Great answer! Without yet jumping into how it works on iOS, I believe hybrid approach this is the best solution as you leverage it to a specific platform. Thank you!
@Sam Yes, that’s pretty much how I implemented this task. The only thing is that the strings that are transferred to the list can be adjusted so that string resources are transferred, and then each user from each country will have their own month names
2

I've faced similar issue and the easiest way to handle it using Map, simply use a names of months in English as key and French months as value, can take it a step further use map of maps if you have multiple languages.

3 Comments

Indeed, this sounds like a much more easier approach in terms of pain. In short words just give me it in english and I'll take care of the month naming in other languages.
if you are a psycho as I am, you can even use Map<String, List<String>. Use the first entry as en: [fr, hn, ru], similar to index of an array, and use rest of the entries for translation. Not sure if it's any better or even if it's better than previous solutions, but it gives you a nice way to iterate through same month name for different languages, if you need to use in any table or similar areas. Using first one as index can also double as actual index if you wanna use month numbers instead of their english names, just put 0 at start and begin with 1 at jan
Yes, this is what i went with, for me much more easier and customizable in the long term fun Month.toCurrentLocale(language: String): String { return mapMonths[language]?.get(ordinal) ?: mapMonths["en"]!![ordinal] }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.