Conditional Formatting – Localization
By Gigi Rosen / January 24, 2024 / No Comments / Oracle Certifications, The ZonedDateTime Class
Conditional Formatting
Consider contiguous half-open intervals on the number line, as shown below. The (lower and upper) limits of these intervals can be any numbers, which will always be in ascending order as they are on the number line. Below we see two half-open intervals with limits 0.0, 1.0, and 2.0.
If a value v falls in the half-open interval [vi, vi+1), then we select the (lower) limit vi. So, if v is any number in the half-open interval [0.0, 1.0), the limit 0.0 is selected. Similarly, if v is any number in the half-open interval [1.0, 2.0), the limit 1.0 will be selected, and so on.
From Table 18.16, we see that the format element having the format type choice requires a subformat pattern. This subformat pattern specifies a choice format. The general syntax of the choice format is shown below, where limit vi and its associated subformat pattern fi are separated by the hash sign (#). Each vi#fi constitutes a choice, and choices are separated by the vertical bar (|). Two consecutive limits constitute a half-open interval: [vi, vi+1). Note that the last choice uses <, meaning any value greater than or equal to vn. Also, the limits v1, v2, …, vn must be in ascending order for the choice format to work correctly.
v
1
#
f
1
|
v
2
#
f
2
| … |
v
n
<
f
n
The choice format thus defines the limits and the corresponding choices. When a value is supplied for formatting, the choice format determines the limit, as explained above, and the formatter uses the choice associated with this limit— hence the term conditional formatting.
Below is a pattern that uses a choice format. The pattern’s only format element has argument index 0 and format type choice, and its subformat pattern specifies a choice format (underlined below, but see also Table 18.16). The argument index 0 in the pattern designates the value that is used to determine the limit, and thereby, select the corresponding choice in the choice format.
“There {0,choice,
0#are no bananas|1#is only one banana|
\
2<are {1,number,integer} bananas
}.”
The choice format above has three choices, separated by the vertical bar (|). Its limits are 0, 1, and 2. Each limit in a choice is associated with a subformat pattern. For limits 0 and 1, the subformat pattern is just text. The subformat pattern for limit 2 specifies a format element to format a number as an integer, as it expects an argument to be supplied for this format element. Note that the argument index in this embedded format element is 1.
Example 18.9 uses a pattern that includes the choice format above to illustrate handling of plurals in messages. Resource bundles for different locales contain the pattern with the choice format. An appropriate resource bundle for a locale is fetched at (1). The choice pattern is read from the resource bundle and a MessageFormat is created at (2), based on the choice pattern and the requested locale. For each locale, the formatter is tested for three cases, exemplified by the rows of two-dimensional array messageArguments at (3). Each row of arguments is passed to the format() method at (5) for formatting. The program output shows the arguments passed and the corresponding result.
In the argument matrix, the first row, {0.5}, specifies the argument 0.5, which falls in the half-interval [0, 1). Limit 0 is chosen, whose choice is selected. This results in the following pattern to be applied:
“There
are no bananas
.”
Similarly for the second row, {1.5}, the limit is determined to be 1, resulting in the following pattern to be applied:
“There is
only one banana
.”
For the third row, {2.5, 2}, the argument 2.5 determines the limit 2, resulting in the pattern below to be applied. The second element (value 2) in this row is the argument that is formatted according to the format element in this pattern.
“There
are {1,number,integer} bananas
.”
We see from the program output that the correct singular or plural form is selected depending on the value of the argument passed to the formatter. It is instructive to experiment with passing different values to the formatter.
Example 18.9 Using Choice Pattern
# File: ChoiceBundle.properties
choice.pattern = There {0,choice,0#are no bananas|1#is only one banana|\
2<are {1,number,integer} bananas}.
…
import java.text.*;
import java.util.*;
public class ChoicePatternUsage {
static void displayMessages(Locale requestedLocale) {
System.out.println(“Requested Locale: ” + requestedLocale);
// Fetch the resource bundle: (1)
ResourceBundle bundle =
ResourceBundle.getBundle(“resources.ChoiceBundle”, requestedLocale);
// choice.pattern = There {0,choice,0#are no bananas|1#is only one banana
// |2#are {1,number,integer} bananas}.
// Create formatter for specified choice pattern and locale: (2)
MessageFormat mf =
new MessageFormat(bundle.getString(“choice.pattern”), requestedLocale);
// Create the message argument arrays: (3)
Object[][] messageArguments = { {0.5}, {1.5}, {2.5, 2} };
// Test the formatter with arguments: (4)
for (int choiceNumber = 0;
choiceNumber < messageArguments.length; choiceNumber++) {
String result = mf.format(messageArguments[choiceNumber]); // (5)
System.out.printf(“Arguments:%-10sResult: %s%n”,
Arrays.toString(messageArguments[choiceNumber]),result);
}
}
public static void main(String[] args) {
displayMessages(new Locale(“en”, “US”));
System.out.println();
displayMessages(new Locale(“es”, “ES”));
}
}