A key is obtained by code specifying the message file's prefix.
|
search = Rechercher searching = Chercher search_menu = Rechercher... search_next = Rechercher \ suivant search_stopped = Chercher a arrêté pattern = There {0} on {1}.
|
The separator between key and value can be the equals sign (=) character or the colon (:) character. Keys are case sensitive. Lines starting with an exclamation (!) or a pound (#) character are ignored as comments. A backslash ( \ ) indicates line continuation of a value that span multiple lines.
To make keys easier to find keys in the file, arrange them in alphabetical order.
But name them so that different forms for the same word are together. Examples:
Numbers with curly braces, such as {0}, {1}, etc. in a key's translated value marks where the program dynamically applies other key-value pairs and program variables at run-time.
Below is an alternative way of explaining the example presented by
Sun Tutorial on
the coding used to return a value for key "pattern"
after replacing markers {0} and {1}.
Marker {0} within a sentence such as "There {0} on {1}" is replaced by defining a ChoiceFormat object that encapsulates the retrieval of additional key values. That processing is based on how many records the application processed, as stored in the numFiles variable. The static value in the fileLimits array is used to compare against the numFiles variable.
choiceForm object | ||
---|---|---|
numFiles & fileLimits | fileStrings [key] | en_US value |
0 | noFiles | are no files |
1 | oneFile | is one file |
2 or more | multipleFiles | are {2} files |
When no records are processed, numFiles would contain 0 (zero), which
triggers the retrieval of key noFiles.
When only one record is processed, numFiles would contain 1 (one), which
triggers the retrieval of key oneFile.
When two or more records are processed, numFiles would contain 2 or more, which
triggers the retrieval of key multipleFiles.
The value of a key replacing a marker can itself have a marker, such as {2}
in the value for the multipleFiles key.
Multi-stage replacements are allowed even though they may blow our mind.
Marker {2} is replaced with method NumberFormat.getInstance(). This yields a number formatted to the current locale.
Methods of the MessageFormat class are used to assemble (format) the substitution of markers in the pattern key. To instantiate a MessageFormat object for a locale:
Marker {1}, does not vary, so it is defined in the code to instantiate the messageForm array used to assemble the result:
The messageForm array is populated by methods of the MessageFormat class, which applies classes designated in the format array. object, string returned for the pattern key is first applied to a then formatted with an object specified for each marker position:
Format[] formats = {choiceForm, null, NumberFormat.getInstance()};
messageForm.setFormats(formats);
Format array position | 0 | 1 | 2 |
---|---|---|---|
Marker | {0} | {1} | {2} |
format Object | choiceForm | null | NumberFormat.getInstance() |
The value returned from choiceForm subsitutes for {0}. null means that a processing object is not applied to substitute a marker,
The value resulting from NumberFormat.getInstance() is substituted for {2}.
|
| ![]() |
Next: Internationalization ![]() |
![]()
| Your first name: Your family name: Your location (city, country): Your Email address: |
![]() Thank you! |