
org.javalite.activeweb.Messages Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of activeweb Show documentation
Show all versions of activeweb Show documentation
Core functionality of the framework
package org.javalite.activeweb;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
/**
* This class is used to pull messages from a resource bundle called activeweb_messages
.
* It is primarily used by the validation framework, but client code can use it as well for other things.
*
* @author Igor Polevoy
*/
public class Messages {
private static final String BUNDLE = "activeweb_messages";
private Messages() {}
/**
* Looks for a localized property/message in activeweb_messages
bundle.
*
* @param key key of the property.
* @param locale locale of a bundle.
* @param params list of parameters for a message. The order of parameters in this list will correspond to the
* numeric order in the parameters listed in the message and has nothing to do with a physical order. This means
* that the 0th parameter in the list will correspond to {0}
, 1st to {1}
and so on.
* @return localized message merged with parameters (if provided), or key if message not found.
*/
public static String message(String key, Locale locale, Object... params) {
return getMessage(key, locale, params);
}
/**
* Looks for a property/message in activeweb_messages
bundle.
* Uses Locale supplied by request.
*
* @param key key of the property.
* @param params list of substitution parameters for a message.
* @return message merged with parameters (if provided), or key if message not found.
*/
public static String message(String key, Object... params) {
return getMessage(key, RequestContext.getHttpRequest().getLocale(), params);
}
private static String getMessage(String key, Locale locale, Object... params){
MessageFormat mf = new MessageFormat("");
try{
if(locale == null){
mf.applyPattern(ResourceBundle.getBundle(BUNDLE).getString(key));
}else{
mf.applyPattern(ResourceBundle.getBundle(BUNDLE, locale).getString(key));
}
}catch(Exception e){
mf.applyPattern(key);
}
return mf.format(params);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy