https://kotlinlang.org logo
#ktor
Title
# ktor
j

Júlio Santos

05/17/2021, 1:09 AM
Talk about guys! Recently I asked here if anyone had already implemented a solution to internationalize the messages and I had no answer, but I found a solution and I want to share it with you.
Copy code
fun getMessages(locale: Locale = Locale.getDefault(), key: String): String {
    val message = ResourceBundle.getBundle("messages", locale)
    return message.getString(key)
}
Copy code
fun StatusPages.Configuration.handle() {
    exception<Exception> {
        val response = mapOf(
            "status" to 400,
            "Code" to getMessages(key = "hello"),
            "Message" to getMessages(key = "world")
        )

        call.respond(status = HttpStatusCode.BadRequest, message = response)
    }

    exception<EntityNotFoundException> {
        val language = call.request.acceptLanguage()
        val response = mapOf(
            "status" to 400,
            "Code" to getMessages(Locale("pt", "BR"), "hello"),
            "Message" to getMessages(Locale("pt", "BR"), "world")
        )
        call.respond(HttpStatusCode.NotFound, message = response)
    }
}
3 Views