Messageformat (dynamic text)
If a string contains multiple data related to internationalization, you can use the messageformat class to batch process the data.
For example:At pm on Jul 1000000, A hurricance destroyed 99 houses and caused $ of damage
The preceding strings contain time, number, currency, and other data related to internationalization. For such strings, you can use the messageformat class to batch process data related to internationalization.
Pattern string and placeholder
Mode string:
At {0} on {1}, a destroyed {2} houses and caused {3} of damage.
Placeholder
L messageformat class
• Messageformat (string pattern)
• Instantiate the messageformat object and load the corresponding mode string.
• Format (Object OBJ [])
• Format the output mode string and specify a placeholder replacement object in the parameter array.
• Format (new object [] {date, new INTEGER (99), new double (1e7 )})
L placeholders can be written in three ways:
• {Argumentindex}: number between 0 and 9, indicating the index number of the object data to be formatted in the parameter Array
• {Argumentindex, formattype}: format type of the Parameter
• {Argumentindex, formattype, formatstyle}: formatted style. Its value must be a valid pattern that matches the formatting type or a string that represents a valid pattern.
Java: 8
// Mode string -- contains placeholders
String Pattern = "at {0, time, full} on {0, date, long}, A hurricance destroyed {1, number} houses and caused {2, number, currency} of damage. ";
Messageformat mf =NewMessageformat (pattern, locale.Us);
// Construct an object array, 1e7 -- 1 multiplied by the 7th Power of 10
Object [] objs = {NewDate (),NewINTEGER (99 ),NewDouble (1e7 )};
String result = mf. Format (objs );
System.Out. Println (result );
// Obtain the resource object
Resourcebundle bundle = resourcebundle.Getbundle("Com. hbsi. Resource. myresource", locale.China);
Result = bundle. getstring ("title ");
// System. Out. println (result );
Messageformat mf1 =NewMessageformat (result, locale.China);
System.Out. Println (mf1.format (objs ));
JSP
<JSP: usebean id ="Date"Class ="Java. util. Date"> </Jsp: usebean>
<%
Request. setattribute ("number1 ",NewINTEGER (99 ));
Request. setattribute ("number2 ",NewDouble (1e7 ));
%>
<FMT: setbundle basename ="Com. hbsi. Resource. myresource"Var ="Bundle"Scope ="Request"/>
<FMT: Message key ="Username"Bundle ="$ {Bundle}"/> <Br>
<FMT: Message key ="Title"Bundle ="$ {Bundle}">
<FMT: Param value ="$ {Date}"> </FMT: param>
<FMT: Param value ="$ {Number1}"> </FMT: param>
<FMT: Param value ="$ {Number2}"> </FMT: param>
</FMT: Message> <br>
<FMT: Message bundle ="$ {Bundle}">
Title
<FMT: Param value ="$ {Date}"> </FMT: param>
<FMT: Param value ="$ {Number1}"> </FMT: param>
<FMT: Param value ="$ {Number2}"> </FMT: param>
</FMT: Message>