Skip to content

Commit

Permalink
allow silent missing resource exception
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanfallet committed Feb 22, 2025
1 parent 61e2ad4 commit 0cd44be
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.util.*
class TranslateFromPropertiesUseCase(
private val baseName: String = DEFAULT_RESOURCE_BUNDLE,
private val control: ResourceBundle.Control = UTF8Control(),
private val silentMissingResourceException: Boolean = false,
) : ITranslateUseCase {

companion object {
Expand All @@ -19,7 +20,12 @@ class TranslateFromPropertiesUseCase(

override fun invoke(input1: Locale, input2: String, input3: List<String>): String {
val bundle = ResourceBundle.getBundle(baseName, input1, control)
val string = bundle.getString(input2)
val string = try {
bundle.getString(input2)
} catch (e: MissingResourceException) {
if (silentMissingResourceException) return input2
else throw e
}
return input3.takeUnless { it.isEmpty() }?.let {
cache.computeIfAbsent(Pair(string, bundle.locale)) {
MessageFormat(string, bundle.locale)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package dev.kaccelero.commons.localization

import java.util.*
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

class TranslateFromPropertiesUseCaseTest {

Expand All @@ -23,4 +25,19 @@ class TranslateFromPropertiesUseCaseTest {
assertEquals("Hello Nathan!", useCase(Locale.ENGLISH, "hello_arg", listOf("Nathan")))
}

@Test
fun testInvokeMissing() {
val useCase = TranslateFromPropertiesUseCase()
val exception = assertFailsWith<MissingResourceException> {
useCase(Locale.ENGLISH, "missing")
}
assertEquals("missing", exception.key)
}

@Test
fun testInvokeMissingSilent() {
val useCase = TranslateFromPropertiesUseCase(silentMissingResourceException = true)
assertEquals("missing", useCase(Locale.ENGLISH, "missing"))
}

}
4 changes: 2 additions & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencyResolutionManagement {
library("tests-h2", "com.h2database:h2:2.3.232")

// Exposed
version("exposed", "0.54.0")
version("exposed", "0.59.0")
library("exposed-core", "org.jetbrains.exposed", "exposed-core").versionRef("exposed")
library("exposed-jdbc", "org.jetbrains.exposed", "exposed-jdbc").versionRef("exposed")
library("exposed-dao", "org.jetbrains.exposed", "exposed-dao").versionRef("exposed")
Expand All @@ -55,7 +55,7 @@ dependencyResolutionManagement {
)

// Ktor
version("ktor", "2.3.12")
version("ktor", "2.3.13")
library("ktor-serialization-kotlinx-json", "io.ktor", "ktor-serialization-kotlinx-json").versionRef("ktor")
library("ktor-server-core", "io.ktor", "ktor-server-core").versionRef("ktor")
library("ktor-server-test-host", "io.ktor", "ktor-server-test-host").versionRef("ktor")
Expand Down

0 comments on commit 0cd44be

Please sign in to comment.