14.国际化信息
程序员文章站
2022-02-08 07:26:03
...
文章目录
14.国际化信息
1. 数字格式化
public static void formatNum () {
Locale locale = new Locale("en", "US");
NumberFormat numberFormat = NumberFormat
.getCurrencyInstance(locale);
double amt = 1234.89;
System.out.println(numberFormat.format(amt));
}
$1,234.89
2. 日期格式化
public static void formatDate () {
Locale locale = new Locale("zh", "CN");
DateFormat format = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
System.out.println(format.format(new Date()));
}
11:26:06
3. 消息格式化
public static void formatMsg() {
String pattern1 = "hello {0}, time:{1}, money:{2}";
String pattern2 = "at {1,time,short} on {1,date,long},{0} paid{2,number,currency}";
Object[] params = {"John", new GregorianCalendar().getTime(), 1.0E3};
String msg1 = MessageFormat.format(pattern1, params);
MessageFormat messageFormat = new MessageFormat(pattern2, Locale.US);
String msg2 = messageFormat.format(params);
System.out.println(msg1);
System.out.println(msg2);
}
hello John, time:18-10-28 上午11:26, money:1,000
at 11:26 AM on October 28, 2018,John paid$1,000.00
4. 资源化文件
public static void resourceBoundle() {
ResourceBundle resourceBundle = ResourceBundle.getBundle("locale/resource", Locale.US);
System.out.println(resourceBundle.getString("hello"));
resourceBundle = ResourceBundle.getBundle("locale/resource", Locale.CHINA);
System.out.println(resourceBundle.getString("hello"));
resourceBundle = ResourceBundle.getBundle("locale/resource", Locale.CHINESE);
System.out.println(resourceBundle.getString("hello"));
resourceBundle = ResourceBundle.getBundle("locale/resource");
System.out.println(resourceBundle.getString("hello"));
}
resource_en_US.properties
hello=hello
resource.properties
hello=你好-default
hello
你好-default
你好-default
你好-default
5. 动态资源文件
fmt_resource_en_US.properties
hello=hello,{0},time:{1}
fmt_resource_zh_CN.properties
hello=你好,{0},时间:{1}
public static void resourceBoundleDynamic() {
ResourceBundle resourceBundle = ResourceBundle.getBundle("locale/fmt_resource", Locale.US);
String msgUs = resourceBundle.getString("hello");
resourceBundle = ResourceBundle.getBundle("locale/fmt_resource", Locale.CHINA);
String msgCn = resourceBundle.getString("hello");
MessageFormat mfUs = new MessageFormat(msgUs, Locale.US);
MessageFormat mfCn = new MessageFormat(msgCn, Locale.CHINA);
Object[] params = {"Presley", new GregorianCalendar().getTime()};
System.out.println(mfUs.format(params));
System.out.println(mfCn.format(params));
}
Output:
hello,Presley,time:10/28/18 11:26 AM
你好,Presley,时间:18-10-28 上午11:26
4. Spring MessageSource-without Application
-
资源文件
-
resource_en_US.properties
hello=hello,{0},time:{1}
-
resource_zh_CN.properties
hello=你好,{0},时间:{1}
-
-
Xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd " default-autowire="default"> <bean id="myResource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>i18n.resource</value> </list> </property> </bean> </beans>
-
Test
public static void testWithoutApplication() { Resource resource = new ClassPathResource("i18n/applicationContext.xml"); XmlBeanFactory xmlBeanFactory = new XmlBeanFactory(resource); MessageSource messageSource = xmlBeanFactory.getBean(MessageSource.class); Object[] params = {"Presley", new GregorianCalendar().getTime()}; String msgCn = messageSource.getMessage("greetingew", params, "default", Locale.CHINA); String msgZh = messageSource.getMessage("greeting", params, Locale.US); System.out.println(msgCn); System.out.println(msgZh); }
-
Output:
default 你好, Presley, 现在时间: 10/28/18 11:36 AM
5. Spring MessageSource-with Application
-
资源文件:同上
-
Xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd " default-autowire="default"> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>i18n.resource</value> </list> </property> </bean> </beans>
-
Test
public static void testWitApplication() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("i18n/application.xml"); Object[] params = {"Presley", new GregorianCalendar().getTime()}; String msgCn = applicationContext.getMessage("greeting", params, "default", Locale.CHINA); String msgZh = applicationContext.getMessage("greeting", params, Locale.US); System.out.println(msgCn); System.out.println(msgZh); }
-
Output:
hello, Presley, current time: 18-10-28 上午11:38 你好, Presley, 现在时间: 10/28/18 11:38 AM
-
注意:
使用ApplicationContext
, bean的名字必须为messageSource
,否则报异常:Exception in thread "main" org.springframework.context.NoSuchMessageException: No message found under code 'greeting' for locale 'en_US'.