MessageFormat examples
22.3.3. Further examples using MessageFormat

Addresses

One common requirement is to display addresses with unknown number of address lines. To avoid empty lines, there’s a custom MessageFormat type called “addLineBreak” which adds a line break after the value if (and only if) the value was actually nonempty.

Example:

${ADDRESS_1,addLineBreak}${ADDRESS_2,addLineBreak}${ADDRESS_3,addLineBreak}

For more flexibility, there's also another type addString to append any generic String if the given value is not empty. Due to limitations of MessageFormat which automatically trims any additional arguments, any strings that start or end with a space must be quoted with double quotes, for Example: ", " or " and ".

Example 1, adding a single space after the value (if it is defined):

${VARIABLE,addString," "}  ->  "value "

Example 2, adding a comma plus a space after the value (if it is defined):

${VARIABLE,addString,", "} -> "value, "

Example 3, adding an arbitrary String, surrounded by spaces:

${VARIABLE,addString," String "} -> "value String "

Example 4, adding an arbitrary String without surrounding spaces; quoting is optional here:

${VARIABLE,addString,String} -> "valueString"

Integers

Integer value displayed with two forced decimal places:

${anInteger,number,0.00}

For integer parameters, a ChoiceFormat can be used to display texts depending on the actual value.

Example:

${anInteger,choice,0#No tokens|1#One token|1<{0} tokens}

This results in “No tokens” for the integer zero, “One token” for the integer one and “n tokens” for any integer greater than one.