Here a small code example to manage messages into java application. This mono instance class allow you to load messages according language. You could also replace token into messages with dynamical argument.
You can find java class at the end of this post.
This class does not contain a lot of code. The next instruction allow to load messages according language :
msg = ResourceBundle.getBundle("message", new Locale(ApplicationPreference.getLanguage(),"") );
This allow to load message.properties from the classpath. If language is “fr” then the file which is going to be loaded is “message_fr.properties”. But if file doesn’t exist so application would try to load the properties according default language defined for Java (then.. it is the one by default). And if the fileis not find again the file message.properties is loaded (f not find again then an exception is raised).
Then deux function allow you to get message from key. To manage messages with token, the class use MessageFormat. Here the code :
return MessageFormat.format(getInstance().msg.getString(key),arguments);
The argument called “arguments” is an object list.
How does it work ? Then it is very simple. Imagine this messages into the properties file :
msg1=My name is {1} and not {2}If I called the function with a list with John and Tom, I will got this message :
My name is John and not Tom
Here is the example end, Have a nice day….
package com.cba.socialBridge.util.application; import java.text.MessageFormat; import java.util.Locale; import java.util.ResourceBundle; public class ApplicationMessage { static protected ApplicationMessage instance=null; static public String[] languageList = { "Français", "English" }; static public String[] languageListCode = { "fr", "en" }; protected ResourceBundle msg; private ApplicationMessage() { super(); msg = ResourceBundle.getBundle("message", new Locale(ApplicationPreference.getLanguage(),"") ); } public static int getIndexLanguageInLanguageList() { int index=-1; for (int i=0 ; index==-1 && i { if ( languageListCode[i].equals(ApplicationPreference.getLanguage()) ) { index = i; } } return index; } protected static ApplicationMessage getInstance() { if ( instance==null ) { instance = new ApplicationMessage(); } return instance; } public static String getMessage(String key) { String result = "?" + key + "?"; try { result = getInstance().msg.getString(key); } catch(Exception e) { ApplicationLogger.getLogger().error("There is a problem with messages management : " + e.getMessage() ); } if ( result == null ) result = "?" + key + "?"; return result; } public static String getMessage(String key,Object[] arguments) { String result = "?" + key + "?"; try { result = MessageFormat.format(getInstance().msg.getString(key), arguments); } catch(Exception e) { ApplicationLogger.getLogger().error("There is a problem with messages management : " + e.getMessage() ); } if ( result == null ) result = "?" + key + "?"; return result; } }







Just a webmaster comment to test if it is working well.
March 17th, 2008