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.
resolvers += "bintray" at "http://jcenter.bintray.com"
libraryDependencies += "com.netaporter" %% "scala-i18n" % "0.1"
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
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!"
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"
scala-i18n
is open source software released under the Apache 2 License.