Skip to content
Francis Galiegue edited this page May 29, 2013 · 8 revisions

Preliminary note

The API may change. This is only version 0.1.

Features

No external library dependency

This library relies only on the JDK (note: 6+).

Message source extensibility

The library has a simple interface for one message source:

public interface MessageSource
{
    String getMessage(final String key);
}

You can implement this interface with whatever you like. The implementation has two builtin message sources: one using Maps, another using property files.

And as to property files...

Property files are read in UTF-8

This is unlike the JDK's ResourceBundle, which reads property files in ISO-8859-1.

Message sources can be stacked

You can use as many message sources as you want. Using the builder class for a MessageBundle, you can append a message source or prepend one:

MessageSource source1, source2, source3;

MessageBundle.Builder builder = new MessageBundle.Builder();

builder = builder.appendSource(source1).appendSource(source2); // Order is now: source1 -> source2
builder = builder.prependSource(source3); // Order is now: source3 -> source1 -> source2

// Build the final bundle
final MessageBundle bundle = builder.build();

You can also reuse an existing bundle and extend it:

MessageSource source4;

// New bundle with order source3 -> source1 -> source2 -> source4
final MessageBundle newBundle = bundle.copy().appendSource(source4).build();

Unknown keys are returned as is

This is unlike a ResourceBundle, which, in this case, throws an (unchecked!!) exception. In the event when a key does not exist, the key itself is returned. This allows for more graceful failures, and an easy spotting of missing keys.

Example:

final Map<String, String> map = new HashMap<String, String>();
map.put("foo", "bar");

final MessageSource source = new MapMessageSource(map);
final MessageBundle bundle = new MessageBundle.Builder().appendSource(source).build();

bundle.getKey("foo"); // returns "bar"
bundle.getKey("baz"); // returns "baz"

Clone this wiki locally