Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs related to invalid locale used for time formatting #246

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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