MessageProvider provider = MessageProvider.getInstance(); provider.createBundle("aKey", "org/myresources/", "messages.properties");or
MessageProvider provider = MessageProvider.getInstance(); provider.createBundle("anotherKey", url);
MessageBundle bundle = MessageProvider.getBundle("aKey"); String message = bundle.getMessage("propertyName");
hello=Hello World! my2cents=my $1 cents dogfriend=Doggo $1 is a friend of $2We can create the bundle by:
MessageProvider provider = MessageProvider.getInstance(); MessageBundle bundle = provider.createBundle("theBundle", "messages.properties");
String text1 = bundle.getMessage("hello"); // return "Hello World!"We can also use our bundle with unnamed variables, specified by their index, by using the MessageBundle.getMessage(String, Object...) method.
String text2 = bundle.getMessage("my2cents", 2); // return "my 2 cents" String text3 = bundle.getMessage("dogfriend", "Buddy", "Mike"); // return "Doggo Buddy is a friend of Mike"Note that if we provide an empty text for a variable, the resulting empty value will be enclosed by quotes. For example:
String text2 = bundle.getMessage("my2cents", ""); // return "my '' cents"
hello=Hello World! my2cents=my ${money} cents dogfriend=Doggo ${dog} is a friend of ${human}We can then use the method:
String text2 = bundle.getNamedMessage("my2cents", "money", 2); // return "my 2 cents" String text3 = bundle.getNamedMessage("dogfriend", "dog", "Buddy", "human", "Mike"); // return "Doggo Buddy is a friend of Mike"
template = "my $1 cents"
MessageConstructor.getText(template, 2)
is "my 2 cents"&index
pattern will be interpreted as the n-index variable in the vararg part for the constructor of the class. If there is no variable for this index, the pattern will return an empty string. For example:String text1 = MessageConstructor.getText("my $1 and $2 cents", 2, 3); // return "my 2 and 3 cents" String text2 = MessageConstructor.getText("my $4 cents", 2); // return "my cents" String text3 = MessageConstructor.getText("my $1 and $2 cents", new Object[]{2, 3}); // return "my 2 and 3 cents"Note that to avoid to throw an exception, the
$0
pattern will be interpreted as $1
. So for example the two constructs will both return the text "my 2 cents":String text1 = MessageConstructor.getText("my $1 cents", 2); String text2 = MessageConstructor.getText("my $0 cents", 2);
template = "my ${money} cents"
MessageConstructor.getNamedText(template, "money", 2)
is "my 2 cents"String text1 = MessageConstructor.getNamedText("my ${money1} and ${money2} cents", "money1", 2, "money2", 3); // return "my 2 and 3 cents" String text3 = MessageConstructor.getNamedText("my ${money1} and ${money2} cents", new Object[]{"money1", 2, "money2", 3}); // return "my 2 and 3 cents"
Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences