Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

MessageProvider



The MessageProvider class provides bundles of messages. It manages several bundles of messages, which are simply property files. These property files associate keys with String messages.

Usage

The way to use this class is:
  • Create one or several bundlesusing properties files URLs. For example:
       MessageProvider provider = MessageProvider.getInstance();
       provider.createBundle("aKey", "org/myresources/", "messages.properties");
    
    or
       MessageProvider provider = MessageProvider.getInstance();
       provider.createBundle("anotherKey", url);
    
  • Reference one bundle and get some messages anywhere in the code:
       MessageBundle bundle = MessageProvider.getBundle("aKey");
       String message = bundle.getMessage("propertyName");
    

MessageBundle

The MessageConstructor class allows to construct a message using a key in the bundle and one or several variables.

Creation of the bundle

Suppose for example that we have the following properties file:
   hello=Hello World!
   my2cents=my $1 cents
   dogfriend=Doggo $1 is a friend of $2
We can create the bundle by:
   MessageProvider provider = MessageProvider.getInstance();
   MessageBundle bundle = provider.createBundle("theBundle", "messages.properties");

General usage

Now we have a message bundle and we can use it without variables by using the MessageBundle.getMessage(String) method.

For example:
   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.

For example:
   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"

Using named variables

It is also possible to use named variables in the bundle for a message, by using the MessageBundle.getNamedMessage(String, Object...) method. The array of variables is a list of (variable name, variable value) pairs.

For example, if we have the following properties file:
  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"

MessageConstructor

The MessageConstructor class allows to construct a message from a template and several variables. This class works exactly as the MessageBundle, except that is uses a template rather than a message retrieved by its key.

General usage

We can use the constructor with unnamed variables by using the MessageConstructor.getText(String, Object...) method.

For example:
  • If template = "my $1 cents"
  • The result of MessageConstructor.getText(template, 2) is "my 2 cents"
The &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);

Using named variables

We can also use the constructor with named variables by using the MessageConstructor.getNamedText(String, Object...) method. The array of variables is a list of (variable name, variable value) pairs.

For example:
  • If template = "my ${money} cents"
  • The result of MessageConstructor.getNamedText(template, "money", 2) is "my 2 cents"
More complex examples:
   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"

Categories: lang | packages

Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences