Application
Internationalization
Answer in the reader's language: translations, plurals and language detection.
Internationalization (i18n) means your application can speak more than one language. You keep the texts in files per language, ask for a text by a key, and Kest picks the right language. i18n is nestjs-i18n.
1. Put the texts in files
i18n/
en.json { "user": { "greeting": "Hello {name}", "items": "{count, plural, =0 {No items} one {# item} other {# items}}" } }
fr.yaml user: { greeting: "Bonjour {name}" }
de.properties user.greeting=Hallo {name}
es/common.json read as keys "common.title" (a directory per language, a file per namespace)JSON, YAML and .properties all work, even mixed. Keys are dotted (user.greeting). {name} is a placeholder.
2. Ask for a text
Kest.run<AppModule> { import(I18nModule.forRoot { path = "i18n"; fallbackLanguage = "en"; messages("it", "user.greeting" to "Ciao {name}") }) }
@Injectable class Greeter(private val i18n: I18nService) {
fun hello(lang: String) = i18n.t("user.greeting", lang, "name" to "Ann") // "Hello Ann" / "Bonjour Ann" ...
}- A key missing in the requested language falls back to the fallback language, then to the key itself (so a missing translation shows
user.greeting, which is visible and easy to find, never a crash). fr-CHfalls back tofr.- A placeholder without a value stays visible (
{name}), so you notice. messages("it", "key" to "text")adds texts in code (useful in tests).
Plurals
"1 item" but "2 items": languages differ in how many forms they have. Use the ICU style:
{count, plural, =0 {No items} one {# item} other {# items}}i18n.t("user.items", "en", "count" to 0) // "No items"
i18n.t("user.items", "en", "count" to 1) // "1 item"
i18n.t("user.items", "en", "count" to 5) // "5 items"# is replaced by the number, =0 matches exactly zero. Kest knows the plural rules of English, French, Portuguese, Russian, Ukrainian, Polish, Arabic and the languages with no plural forms; so Russian's one/few/many/other works.
3. Per request: pick the reader's language
val options = I18nOptions().apply { path = "i18n" }
val service = I18nService(options)
startHttp(NettyHttpAdapter(8080)) { useMiddleware(I18nMiddleware(service, options)) }
@Get fun hello(request: HttpRequest) = i18n.t("user.greeting", I18nMiddleware.languageOf(request), "name" to "Ann")The middleware decides the language from, in order: the ?lang= query parameter, the x-lang header, then Accept-Language (what the browser sends). Set options.resolver to choose differently (a user setting from the database). The response carries Content-Language. To pick from a header yourself: i18n.negotiate("fr-CH, fr;q=0.9, en;q=0.8") returns the best language you have.
i18n.reload() reads the files again.
Good practice
- Keys, not sentences.
checkout.pay_button, not the English text as the key; translators change wording, keys stay. - Never build sentences by joining fragments (
"You have " + n + " items"): word order differs between languages. Use one message with placeholders and plurals. - Keep texts out of code, in the files, so translators do not need a developer.
- Also localise dates, numbers and currency with
java.time/NumberFormatand the reader'sLocale. - Validation messages can be translated the same way (look up the key in your error handler).