Library

i18n

Languages, translatable messages, ICU MessageFormat 2.0 and locale-aware number and date formatting, for Scala 3 on the JVM and Scala.js.

3.0.0 coming to Maven Central

Install

Scala 3.9 or later. On the JVM:

// Mill
mvn"com.edoframework::edo-i18n:3.0.0"

// sbt
libraryDependencies += "com.edoframework" %% "edo-i18n" % "3.0.0"

On Scala.js, the same artifact with :: in Mill and %%% in sbt.

Quick start

A message carries its translations and its arguments, and is translated where it is shown:

import com.edoframework.i18n.*

val greeting = Message.withNamedArgs(
  Language.English -> "Hello {$name}, you have {$points} points",
  Language.Dutch   -> "Hallo {$name}, u hebt {$points} punten",
  Language.French  -> "Bonjour {$name}, vous avez {$points} points",
)("name" -> "Alice", "points" -> 150)

greeting.translate(Language.English)  // Hello Alice, you have 150 points
greeting.translate(Language.French)   // Bonjour Alice, vous avez 150 points

A Language is a BCP 47 tag. English, Dutch and French are built in; any other is Language("de") or Language("nl-BE"). A missing translation falls back to the base language (nl-BE to nl), then to English.

ICU MessageFormat 2.0

val template =
  """.match {$count :plural}
    |when one   {You have one item}
    |when other {You have {$count} items}""".stripMargin

MessageFormat2.format(template, Map("count" -> 1), "en")  // You have one item
MessageFormat2.format(template, Map("count" -> 5), "en")  // You have 5 items

Numbers, currencies and dates format in the language's locale: through java.text and java.time on the JVM, and through the browser's Intl API on Scala.js.

Coming from genius-i18n

3.0.0 has the API of fish.genius::genius-i18n:2.0.0 under its new name:

  • the dependency is com.edoframework::edo-i18n;
  • the package is com.edoframework.i18n, so replace genius.i18n in your imports;
  • it needs Scala 3.9 and brings zio-json 1.1;
  • the licence is Apache 2.0. Releases up to 2.0.0 stay MIT.

Source, issues and release notes on GitHub