Interface MessageFormatter
-
- All Known Implementing Classes:
MessageFormatterImpl
public interface MessageFormatterThe MessageFormatter loads and formats local messages. This should be used to localize the system.Message Loading
All aspects of the message loading is descript in the package description
de.uni_trier.wi2.procake.utils.multilanguage. Since version 0.4 it is possible to specify the classloader that should load the resource bundle. If no classloader is specified, the class loader of the message formatter implementation is used. But for agents that are loaded with an own classloader this does not work because the agent resources are placed in the agent jar package. Therefore, the agent can specify the class loader that should be used to load the resources, eg., the classloader of the agent.Message Formatting
This explanation is taken from
MessageFormat.MessageFormatterprovides a means to produce concatenated messages in language-neutral way. Use this to construct messages displayed for end users.MessageFormattertakes a set of objects, formats them, then inserts the formatted strings into the pattern at the appropriate places.If the component or key can not be found in the resource bundle, the key is used as pattern.
Patterns and Their Interpretation
MessageFormatteruses patterns of the following form:<i>MessageFormatPattern:</i> <i>String</i> <i>MessageFormatPattern</i> <i>FormatElement</i> <i>String</i> <i>FormatElement:</i> { <i>ArgumentIndex</i> } { <i>ArgumentIndex</i> , <i>FormatType</i> } { <i>ArgumentIndex</i> , <i>FormatType</i> , <i>FormatStyle</i> } <i>FormatType: one of </i> number date time <i>FormatStyle:</i> short medium long full integer currency percent <i>SubformatPattern</i> <i>String:</i> <i>StringPart<sub>opt</sub></i> <i>String</i> <i>StringPart</i> <i>StringPart:</i> '' ' <i>QuotedString</i> ' <i>UnquotedString</i> <i>SubformatPattern:</i> <i>SubformatPatternPart<sub>opt</sub></i> <i>SubformatPattern</i> <i>SubformatPatternPart</i> <i>SubFormatPatternPart:</i> ' <i>QuotedPattern</i> ' <i>UnquotedPattern</i>Within a String ,
"''"represents a single quote. A QuotedString can contain arbitrary characters except single quotes; the surrounding single quotes are removed. An UnquotedString can contain arbitrary characters except single quotes and left curly brackets. Thus, a string that should result in the formatted message "'{0}'" can be written as"'''{'0}''"or"'''{0}'''".Within a SubformatPattern , different rules apply. A QuotedPattern can contain arbitrary characters except single quotes; but the surrounding single quotes are not removed, so they may be interpreted by the subformat. For example,
"{1,number,$'#',##}"will produce a number format with the pound-sign quoted, with a result such as: "$#31,45". An UnquotedPattern can contain arbitrary characters except single quotes, but curly braces within it must be balanced. For example,"ab {0} de"and"ab '}' de"are valid subformat patterns, but"ab {0'}' de"and"ab } de"are not.
- Warning:
- The rules for using quotes within message format patterns unfortunately have shown to be somewhat confusing. In particular, it isn't always obvious to localizers whether single quotes need to be doubled or not. Make sure to inform localizers about the rules, and tell them (for example, by using comments in resource bundle source files) which strings will be processed by MessageFormat. Note that localizers may need to use single quotes in translated strings where the original version doesn't have them.
The ArgumentIndex value is a non-negative integer written using the digits '0' through '9', and represents an index into the
argumentsarray passed to theformatmethods or the result array returned by theparsemethods. The component and the key are added automatically to the arguments. Consequently, argument index '0' is always the component, '1' is the key, and the given parameters are starting at index '2'.The FormatType and FormatStyle values are used to create a
Formatinstance for the format element. The following table shows how the values map to Format instances. Combinations not shown in the table are illegal. A SubformatPattern must be a valid pattern string for the Format subclass used.//Shows how FormatType and FormatStyle values map to * Format instances"
Format Type Format Style Subformat Created (none) (none) nullnumber(none) NumberFormat.getInstance(getLocale())integerNumberFormat.getIntegerInstance(getLocale())currencyNumberFormat.getCurrencyInstance(getLocale())percentNumberFormat.getPercentInstance(getLocale())SubformatPattern new DecimalFormat(subformatPattern, new DecimalFormatSymbols(getLocale()))date(none) DateFormat.getDateInstance(DateFormat.DEFAULT, getLocale())shortDateFormat.getDateInstance(DateFormat.SHORT, getLocale())mediumDateFormat.getDateInstance(DateFormat.DEFAULT, getLocale())longDateFormat.getDateInstance(DateFormat.LONG, getLocale())fullDateFormat.getDateInstance(DateFormat.FULL, getLocale())SubformatPattern new SimpleDateFormat(subformatPattern, getLocale())time(none) DateFormat.getTimeInstance(DateFormat.DEFAULT, getLocale())shortDateFormat.getTimeInstance(DateFormat.SHORT, getLocale())mediumDateFormat.getTimeInstance(DateFormat.DEFAULT, getLocale())longDateFormat.getTimeInstance(DateFormat.LONG, getLocale())fullDateFormat.getTimeInstance(DateFormat.FULL, getLocale())SubformatPattern new SimpleDateFormat(subformatPattern, getLocale())FormatType and FormatStyle mapping to instances Usage Information
Here are some examples of usage:
Object[] arguments = { new Integer(7), new Date(System.currentTimeMillis()), "a disturbance in the Force" }; String result = MessageFormatter.format("component", "1232", arguments); example /resources/Messages/component.properties entry: 1232=At {3,time} on {3,date}, there was {4} on planet {2,number,integer} ({0}:{1}). output: At 12:30 PM on Jul 3, 2053, there was a disturbance in the Force on planet 7 (component:1232).Typically, the message format will come from resources, and the arguments will be dynamically set at runtime.
- Author:
- Rainer Maximini
- See Also:
Locale,Format,NumberFormat,DecimalFormat,ChoiceFormat
-
-
Field Summary
Fields Modifier and Type Field Description static StringCOMPONENT_MULTILANGUAGEstatic StringDEFAULT_RESOURCE_PREFIXThe default location of the resources, values is "resources/Messages/" .static StringERROR_WRONG_FORMAT
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description Stringformat(String component, String key)Stringformat(String component, String key, ClassLoader cl)Stringformat(String component, String key, Object parameter1)Stringformat(String component, String key, Object[] parameter)Stringformat(String component, String key, Object[] parameter, ClassLoader cl)Stringformat(String component, String key, Object parameter1, ClassLoader cl)Stringformat(String component, String key, Object parameter1, Object parameter2)Stringformat(String component, String key, Object parameter1, Object parameter2, ClassLoader cl)Stringformat(String component, String key, Object parameter1, Object parameter2, Object parameter3)Stringformat(String component, String key, Object parameter1, Object parameter2, Object parameter3, ClassLoader cl)Stringformat(String component, String key, Object parameter1, Object parameter2, Object parameter3, Object parameter4)Stringformat(String component, String key, Object parameter1, Object parameter2, Object parameter3, Object parameter4, ClassLoader cl)Stringformat(String component, String key, Object parameter1, Object parameter2, Object parameter3, Object parameter4, Object parameter5)Stringformat(String component, String key, Object parameter1, Object parameter2, Object parameter3, Object parameter4, Object parameter5, ClassLoader cl)LocalegetLocale()voidsetLocale(Locale local)
-
-
-
Field Detail
-
DEFAULT_RESOURCE_PREFIX
static final String DEFAULT_RESOURCE_PREFIX
The default location of the resources, values is "resources/Messages/" .- See Also:
- Constant Field Values
-
COMPONENT_MULTILANGUAGE
static final String COMPONENT_MULTILANGUAGE
- See Also:
- Constant Field Values
-
ERROR_WRONG_FORMAT
static final String ERROR_WRONG_FORMAT
- See Also:
- Constant Field Values
-
-
Method Detail
-
format
String format(String component, String key, ClassLoader cl)
-
format
String format(String component, String key, Object parameter1, ClassLoader cl)
-
format
String format(String component, String key, Object parameter1, Object parameter2, ClassLoader cl)
-
format
String format(String component, String key, Object parameter1, Object parameter2, Object parameter3)
-
format
String format(String component, String key, Object parameter1, Object parameter2, Object parameter3, ClassLoader cl)
-
format
String format(String component, String key, Object parameter1, Object parameter2, Object parameter3, Object parameter4)
-
format
String format(String component, String key, Object parameter1, Object parameter2, Object parameter3, Object parameter4, ClassLoader cl)
-
format
String format(String component, String key, Object parameter1, Object parameter2, Object parameter3, Object parameter4, Object parameter5)
-
format
String format(String component, String key, Object parameter1, Object parameter2, Object parameter3, Object parameter4, Object parameter5, ClassLoader cl)
-
format
String format(String component, String key, Object[] parameter, ClassLoader cl)
-
getLocale
Locale getLocale()
-
setLocale
void setLocale(Locale local)
-
-