alaz / scala-i18n   1.1.0

GitHub

Play-like internationalized messages for any Scala

Scala versions: 3.x 2.13 2.12 2.11

Maven Central Build Status

Introduction

Small library to support i18n messages in Scala "just like in Play Framework".

  • properties files
  • in UTF-8 (vs. Java properties files in ISO 8859-1)
  • formatted with MessageFormat

It provides a trivial library to "localize" application entities as well.

Messages

Mostly just like in Play Framework (the differences are outlined below). Messages are in messages_XXX.txt (mind the file extension, Play does not use any) files in UTF-8 encoding in the application's resources (Play's messages reside in /conf ). There is a default file messages.txt where the key is looked up when it cannot be found in the language-specific file, e.g. messages_en.txt or messages_ru.txt.

The messages are formatted using Java's java.util.MessageFormat. Mind the apostrophes (see the details in Javadoc)!

Messages call depend on the implicit parameter of type Lang which represents the language. It's being used in Localized as well, see below.

You would use Messages so:

implicit val userLang = Lang("en")

val msg = Messages("greet")

Localized

You may want to localize for some application's entity, e.g. a user or a session. In this case, you may implicitly determine this entity's preferred language:

case class User(id: Int, lang: Lang)

object User {
  implicit object userLocale extends Localized {
    override def locale(user: User) = user.lang
  }
}

and format the whole page or email for any user later:

val email =
  Localized(user) { implicit lang =>
    val greet = Messages("email.greet", user.fullName)
    val text = Messages("email.text")
    s"$greet $text"
  }

Using

In SBT:

val i18n = "com.osinka.i18n" %% "scala-i18n" % "1.1.0"

Credits

License

Apache 2.0