No description, website, or topics provided.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
project Bintray release working Oct 1, 2013
src Remove unused import Oct 1, 2013
.gitignore Initial commit Oct 1, 2013
.travis.yml Rename travis file Oct 1, 2013
LICENSE Initial commit Oct 1, 2013
README.md
build.sbt Bintray release working Oct 1, 2013

README.md

scala-i18n

Build Status

scala-i18n is a very thin Scala wrapper around Java's java.util.ResourceBundle to make it a bit nicer to use from Scala code.

SBT

resolvers += "bintray" at "http://jcenter.bintray.com"

libraryDependencies += "com.netaporter" %% "scala-i18n" % "0.1"

Getting Started

Provided you have you resource bundle files like so:

src/main/resources/messages/
├── messages.properties
├── messages_fr.properties
└── messages_fr_CA.properties

You can create a ResourceBundle reference like so:

val messages = ResourceBundle("messages/messages")

Then you can call methods like so:

Returns the value for the message key my.greeting.message from the messages_fr_CA.properties.

val message = messages.get("my.greeting.message", Locale.CANADA_FRENCH)

If the message key does not exist in the messages_fr_CA.properties file, it will fallback and try the messages_fr.properties. If the message still doesn't exist, it will fallback and try the root messages.properties file.

If the message key does not exist in any files, then get will throw a MissingResourceException. Alternatively you can use the find method that will either return a Some(message) when a message with the specified key exists or a None when NO message with the specified key exists.

messages.find("my.greeting.message", Locale.ENGLISH) == Some("Hello!")
messages.find("my.non.existent.message", Locale.ENGLISH) == None

Defaults

getOrElse lets you specify a default in the instance that no message exists for the specified key:

messages.getOrElse("my.greeting.message", Locale.ENGLISH, "Default!") == "Hello!"
messages.getOrElse("my.non.existent.message", Locale.ENGLISH, "Default!") == "Default!"

Parameters

You can specify parameters in a resource bundle file like so:

my.greeting.message=Hello {0}, you look {1}

The you may use getWithParams or findWithParams like so:

val message = messages.getWithParams("my.greeting.message", Locale.ENGLISH, "Joe Bloggs", "awful")
message == "Hello Joe Bloggs, you look awful"

License

scala-i18n is open source software released under the Apache 2 License.