Blog Archives

Managing i18n using a Java enum

Hi there !

Here it is, I said previously that I’ll also use this blog for my development posts. I also decided to try to publish them in english as much as possible. For the first one, I just wanted to share with you a short snippet related to i18n (for those who don’t know what i18n means, it’s a frequently used contraction for internationalization).

This snippet allows to manage i18n using native Java API, by getting the different messages from a properties file using a Java enum. I commonly use this in the Eclipse plug-ins that I develop, but it could be used in any kind of context. Just keep in mind that in some cases, following the framework you’re using, this one probably provides a way to manage i18n, and it should be interesting to use the way provided by the framework. Nevertheless, here is an easy piece of java code that manages the properties file containing the messages:

import java.util.Locale;

/**
* Enumeration that defines all the GUI & User-dedicated messaging system
*
* @author Antoine Neveux
* @version 1.0
*
*/
public enum MyPluginMessages {

// Keys for the messages
        MESSAGE_KEY_1, MESSAGE_KEY_2

;

/**
* Internal Resource Bundle instance
*/
private static ResourceBundle resourceBundle;

static {
MyPluginMessages.resourceBundle = ResourceBundle.getBundle(
"my_messages", Locale.getDefault());
}

/**
* Returns the message that underlies the current Message Key
*
* @return String
*/
public String message() {
if (MyPluginMessages.resourceBundle == null)
return this.toString();
String key = this.toString();
String message = MyPluginMessages.resourceBundle
.getString(key);
return message != null ? message : key;
}

/**
* Returns the message that underlies the current Message Key, formatted
* with input parameters
*
* @param varargs
* Input Parameter
* @return String
*/
public String message(Object... varargs) {
if (MyPluginMessages.resourceBundle == null)
return this.toString();
String key = this.toString();
String message = MyPluginMessages.resourceBundle
.getString(key);
try {
return message != null ? String.format(message, varargs) : key;
} catch (Exception e) {
                        // TODO Report the error using something appropriate, for example, Eclipse error log
/* Activator.sendErrorToErrorLog(
"An exception occurred while trying to format Message with key: "
+ key, e); */
}
return key;
}

}

Then, you just need to put a properties file in your project’s classpath (here, its name is my_messages). This file should contain the different messages defined like this:

MESSAGE_KEY_1=Hello World !

Finally, when you need to get a message in your source code, you just have to call it like this:

MyPluginMessages.MESSAGE_KEY_1.message();

Note also that the provided snippet uses a StringFormatter, so you can pass arguments to the message method so as to use them as arguments of the StringFormatter, to define dynamic messages.

I hope this post could help somebody, or at least will be interesting for someone :)

See you for a next development post !