Skip to content

Commit

Permalink
Fix i18n test on JVM 8
Browse files Browse the repository at this point in the history
  • Loading branch information
osipxd committed Nov 26, 2024
1 parent 7a6ef09 commit 3537e41
Showing 1 changed file with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import io.ktor.i18n.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.testing.*
import kotlin.test.*
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull

class TranslationTest {

Expand Down Expand Up @@ -93,7 +95,7 @@ class TranslationTest {

routing {
get("/") {
val value = i18n("some_key")
val value = i18n("some_key").fixJava8Encoding()
call.respond(HttpStatusCode.OK, value)
}
}
Expand All @@ -107,4 +109,17 @@ class TranslationTest {
val contentAsString = response.bodyAsText()
assertEquals("Русский Ключ", contentAsString)
}

/**
* On Java 8 PropertyResourceBundle requires properties to be encoded in ISO-8859-1,
* while starting from Java 9 the default encoding is UTF-8.
*/
private fun String.fixJava8Encoding(): String {
return if (System.getProperty("java.version").startsWith("1.8")) {
// Encode string in UTF-8 instead of ISO-8859-1
this.toByteArray(Charsets.ISO_8859_1).toString(Charsets.UTF_8)
} else {
this
}
}
}

0 comments on commit 3537e41

Please sign in to comment.