diff --git a/composeApp/src/commonMain/kotlin/com/clipevery/utils/DateUtils.kt b/composeApp/src/commonMain/kotlin/com/clipevery/utils/DateUtils.kt index cddbe10b7..fe8cea205 100644 --- a/composeApp/src/commonMain/kotlin/com/clipevery/utils/DateUtils.kt +++ b/composeApp/src/commonMain/kotlin/com/clipevery/utils/DateUtils.kt @@ -1,7 +1,6 @@ package com.clipevery.utils import io.realm.kotlin.types.RealmInstant -import java.text.SimpleDateFormat import java.time.Instant import java.time.LocalDateTime import java.time.ZoneId @@ -49,24 +48,10 @@ object DateUtils { return dateFormatter.format(LocalDateTime.now()) } - fun getDateFormat(date: LocalDateTime, language: String): String { - val locale = when (language) { - "zh" -> Locale.SIMPLIFIED_CHINESE - "en" -> Locale.US - "jp" -> Locale.JAPAN - "es" -> Locale("es", "ES") - else -> Locale.getDefault() - } - - val pattern = when (language) { - "en" -> "MM/dd/yyyy" - "es" -> "dd/MM/yyyy" - "jp" -> "yyyy/MM/dd" - "zh" -> "yyyy年MM月dd日" - else -> "MM/dd/yyyy" - } - - val formatter = SimpleDateFormat(pattern, locale) + fun getDateText(date: LocalDateTime, pattern: String, locale: Locale): String { + val formatter: DateTimeFormatter = Memoize.memoize(pattern, locale) { + DateTimeFormatter.ofPattern(pattern, locale) + }() return formatter.format(date) } diff --git a/composeApp/src/commonMain/kotlin/com/clipevery/utils/ToolUtils.kt b/composeApp/src/commonMain/kotlin/com/clipevery/utils/ToolUtils.kt index 76a23b50b..c13a6c1f7 100644 --- a/composeApp/src/commonMain/kotlin/com/clipevery/utils/ToolUtils.kt +++ b/composeApp/src/commonMain/kotlin/com/clipevery/utils/ToolUtils.kt @@ -11,4 +11,15 @@ class OnceFunction(private val function: () -> T) { } return result!! } -} \ No newline at end of file +} + +object Memoize { + fun memoize(vararg inputs: T, function: () -> R): () -> R { + val cache = mutableMapOf, R>() + return { + val key = inputs.toList() + cache.getOrPut(key) { function() } + } + } + +} diff --git a/composeApp/src/desktopMain/kotlin/com/clipevery/i18n/I18n.kt b/composeApp/src/desktopMain/kotlin/com/clipevery/i18n/I18n.kt index d569fa511..fdd1d7b34 100644 --- a/composeApp/src/desktopMain/kotlin/com/clipevery/i18n/I18n.kt +++ b/composeApp/src/desktopMain/kotlin/com/clipevery/i18n/I18n.kt @@ -10,6 +10,7 @@ import java.io.FileNotFoundException import java.io.InputStreamReader import java.nio.charset.StandardCharsets import java.time.LocalDateTime +import java.util.Locale import java.util.Properties import java.util.concurrent.ConcurrentHashMap @@ -102,7 +103,22 @@ class CopywriterImpl(private val language: String) : Copywriter { } override fun getDate(date: LocalDateTime): String { - return DateUtils.getDateFormat(date, language) + val locale = when (language) { + "zh" -> Locale.SIMPLIFIED_CHINESE + "en" -> Locale.US + "jp" -> Locale.JAPAN + "es" -> Locale("es", "ES") + else -> Locale.getDefault() + } + + val pattern = when (language) { + "en" -> "MM/dd/yyyy" + "es" -> "dd/MM/yyyy" + "jp" -> "yyyy/MM/dd" + "zh" -> "yyyy年MM月dd日" + else -> "MM/dd/yyyy" + } + return DateUtils.getDateText(date, pattern, locale) } override fun getAbridge(): String {