Giter VIP home page Giter VIP logo

scala-i18n's Introduction

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.

scala-i18n's People

Contributors

theon avatar

Watchers

Eric Peters avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.