Skip to content

Commit

Permalink
Fix bugs related to invalid locale used for time formatting (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
mars885 authored Oct 23, 2024
1 parent 0a582d4 commit d8a6be7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.paulrybitskyi.gamedge.common.testing

import com.paulrybitskyi.gamedge.core.providers.LocaleProvider
import java.util.Locale

class FakeLocaleProvider(
private val locale: Locale = Locale.ENGLISH,
) : LocaleProvider {

override fun getLocale(): Locale {
return locale
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.paulrybitskyi.gamedge.core.formatters

import com.paulrybitskyi.gamedge.core.providers.LocaleProvider
import com.paulrybitskyi.gamedge.core.providers.TimeFormat
import com.paulrybitskyi.gamedge.core.providers.TimeFormatProvider
import com.paulrybitskyi.gamedge.core.providers.TimeProvider
Expand All @@ -36,6 +37,7 @@ internal class ArticlePublicationDateFormatterImpl @Inject constructor(
private val relativeDateFormatter: RelativeDateFormatter,
private val timeProvider: TimeProvider,
private val timeFormatProvider: TimeFormatProvider,
private val localeProvider: LocaleProvider,
) : ArticlePublicationDateFormatter {

private companion object {
Expand Down Expand Up @@ -68,7 +70,9 @@ internal class ArticlePublicationDateFormatterImpl @Inject constructor(

private fun formatAsAbsoluteDate(dateTime: LocalDateTime): String {
val pattern = getAbsoluteDatePattern(dateTime)
val formattedDate = DateTimeFormatter.ofPattern(pattern).format(dateTime)
val formattedDate = DateTimeFormatter
.ofPattern(pattern, localeProvider.getLocale())
.format(dateTime)

return formattedDate
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.paulrybitskyi.gamedge.common.domain.games.entities.Game
import com.paulrybitskyi.gamedge.common.domain.games.entities.ReleaseDate
import com.paulrybitskyi.gamedge.common.domain.games.entities.ReleaseDateCategory
import com.paulrybitskyi.gamedge.core.R
import com.paulrybitskyi.gamedge.core.providers.LocaleProvider
import com.paulrybitskyi.gamedge.core.providers.StringProvider
import com.paulrybitskyi.hiltbinder.BindType
import java.time.Instant
Expand All @@ -36,6 +37,7 @@ interface GameReleaseDateFormatter {
internal class GameReleaseDateFormatterImpl @Inject constructor(
private val stringProvider: StringProvider,
private val relativeDateFormatter: RelativeDateFormatter,
private val localeProvider: LocaleProvider,
) : GameReleaseDateFormatter {

private companion object {
Expand Down Expand Up @@ -76,7 +78,7 @@ internal class GameReleaseDateFormatterImpl @Inject constructor(
private fun ReleaseDate.formatCompleteDate(): String {
val releaseLocalDateTime = toLocalDateTime()
val formattedReleaseDate = DateTimeFormatter
.ofPattern(COMPLETE_DATE_FORMATTING_PATTERN)
.ofPattern(COMPLETE_DATE_FORMATTING_PATTERN, localeProvider.getLocale())
.format(releaseLocalDateTime)

return buildString {
Expand All @@ -89,7 +91,7 @@ internal class GameReleaseDateFormatterImpl @Inject constructor(

private fun ReleaseDate.formatDaylessDate(): String {
return DateTimeFormatter
.ofPattern(DAYLESS_DATE_FORMATTING_PATTERN)
.ofPattern(DAYLESS_DATE_FORMATTING_PATTERN, localeProvider.getLocale())
.format(toLocalDateTime())
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.paulrybitskyi.gamedge.core.providers

import com.paulrybitskyi.hiltbinder.BindType
import java.util.Locale
import javax.inject.Inject

interface LocaleProvider {
fun getLocale(): Locale
}

@BindType
internal class LocaleProviderImpl @Inject constructor() : LocaleProvider {

override fun getLocale(): Locale {
// App only supports the English language
return Locale.ENGLISH
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.paulrybitskyi.gamedge.core

import com.google.common.truth.Truth.assertThat
import com.paulrybitskyi.gamedge.common.testing.FakeLocaleProvider
import com.paulrybitskyi.gamedge.core.formatters.ArticlePublicationDateFormatterImpl
import com.paulrybitskyi.gamedge.core.formatters.RelativeDateFormatter
import com.paulrybitskyi.gamedge.core.providers.TimeFormat
Expand Down Expand Up @@ -50,6 +51,7 @@ internal class ArticlePublicationDateFormatterImplTest {
relativeDateFormatter = relativeDateFormatter,
timeProvider = timeProvider,
timeFormatProvider = timeFormatProvider,
localeProvider = FakeLocaleProvider(),
)

every { relativeDateFormatter.formatRelativeDate(any()) } returns RELATIVE_DATE
Expand Down

0 comments on commit d8a6be7

Please sign in to comment.