Talk about guys! Recently I asked here if anyone h...
# ktor
j
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)
    }
}