05点睛Spring4.1-国际化
5.1 ReloadableResourceBundleMessageSource
- 使用ReloadableResourceBundleMessageSource可获得不同语言的配置
- 此处是全局配置,适合用@Bean声明
5.2 示例
5.2.1 新建英文messagesmessages_en_US.properties
wisely.name = wyf
wisely.age = 32
5.2.2 新建中文messagesmessages_zh_CN.properties
wisely.name = \u6C6A\u4E91\u98DE
wisely.age = 3-10-2
5.2.3 配置ReloadableResourceBundleMessageSource
package com.wisely.i18n; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource; @Configuration
public class I18NConfig {
@Bean
public static ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource= new ReloadableResourceBundleMessageSource();
String[] resources = {"classpath:com/wisely/i18n/messages"};
messageSource.setBasenames(resources);
messageSource.setCacheSeconds(1);
return messageSource;
} }
5.2.4 测试
package com.wisely.i18n; import java.util.Locale; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class Main {
@Autowired
MessageSource messageSource; public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.i18n");
String nameEn = context.getMessage("wisely.name",null, Locale.US);
String nameCn = context.getMessage("wisely.name",null, Locale.CHINA);
System.out.println("nameEN="+nameEn);
System.out.println("nameCN="+nameCn); Main main = context.getBean(Main.class);
main.showAgeInfo(context); context.close();
} public void showAgeInfo(AnnotationConfigApplicationContext context){
String ageEn = context.getMessage("wisely.age",null, Locale.US);
String ageCn = context.getMessage("wisely.age",null, Locale.CHINA);
System.out.println("nameEN="+ageEn);
System.out.println("nameCN="+ageCn);
}
}
输出结果
nameEN=wyf
nameCN=汪云飞
nameEN=32
nameCN=3-10-2
05点睛Spring4.1-国际化的更多相关文章
- 18点睛Spring4.1-Meta Annotation
18.1 Meta Annotation 元注解:顾名思义,就是注解的注解 当我们某几个注解要在多个地方重复使用的时候,写起来比较麻烦,定义一个元注解可以包含多个注解的含义,从而简化代码 下面我们用& ...
- 04点睛Spring4.1-资源调用
转发:https://www.iteye.com/blog/wiselyman-2210666 4.1 Resource spring用来调用外部资源数据的方式 支持调用文件或者是网址 在系统中调用p ...
- 14点睛Spring4.1-脚本编程
转发:https://www.iteye.com/blog/wiselyman-2212678 14.1 Scripting脚本编程 脚本语言和java这类静态的语言的主要区别是:脚本语言无需编译,源 ...
- 00点睛Spring4.1-环境搭建
转载:https://www.iteye.com/blog/wiselyman-2210250 0.1 前置条件 Spring 4.1提倡基于Java Config和注解的配置,所以本教程通篇不会采用 ...
- 05点睛Spring MVC 4.1-服务器端推送
转发:https://www.iteye.com/blog/wiselyman-2214626 5.1 服务器端推送 SSE(server send event)是一种服务器端向浏览器推送消息的技术, ...
- 17点睛Spring4.1-@Conditional
17.1 @Conditional @Conditional为按照条件配置spring的bean提供了支持,即满足某种条件下,怎么配置对应的bean; 应用场景 当某一个jar包在classpath中 ...
- 16点睛Spring4.1-TaskScheduler
转发:https://www.iteye.com/blog/wiselyman-2213049 16.1 TaskScheduler 提供对计划任务提供支持; 使用@EnableScheduling开 ...
- 15点睛Spring4.1-TaskExecutor
转发:https://www.iteye.com/blog/wiselyman-2212679 15.1 TaskExecutor spring的TaskExecutor为在spring环境下进行并发 ...
- 13点睛Spring4.1-Spring EL
13.1 Spring EL Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似jsp的EL表达式语言; 本教程关注于在注解中使用Spring EL; Spring EL ...
随机推荐
- RateLimiter令牌桶算法
限流,是服务或者应用对自身保护的一种手段,通过限制或者拒绝调用方的流量,来保证自身的负载. 常用的限流算法有两种:漏桶算法和令牌桶算法 漏桶算法 思路很简单,水(请求)先进入到漏桶里,漏桶以一定的速度 ...
- RookeyFrame 模块 线上创建的模块 迁移到 线下来
1. 把线上创建的model,写在项目的model层里面. 把文件 Rookey.Frame.Web\Config\TempModel\Order_File.code 复制到model层, 用文本编辑 ...
- Pytest权威教程13-Fixture方法及测试用例的参数化
目录 Fixture方法及测试用例的参数化 @pytest.mark.parametrize:参数化测试函数 基本的pytest_generate_tests例子 更多示例 返回: Pytest权威教 ...
- Linux 修改时区的办法
Linux修改时区的正确方法 CentOS和Ubuntu的时区文件是/etc/localtime,但是在CentOS7以后localtime以及变成了一个链接文件 [root@centos7 ~]# ...
- hbase 在线修复集群命令
前提:HDFS fsck确保hbase根目录下文件没有损坏丢失,如果有,则先进行corrupt block移除. 切记:一定要在所有Region都上线之后再修复,否则修复之后可能出现重复Region. ...
- Failed opening libc!的解决方法
方法来源: http://www.cfd-online.com/Forums/fluent/135879-setting-process-affinity-failed-opening-libc.ht ...
- web开发学习的网站
网易云课堂>imooc>coursera 网易云课堂 imooc.com 关于web的视频会多一些 最近要学一个付费的课程 http://www.v2ex.com/t/154242 ...
- 2018-2019-2 网络对抗技术 20165322 Exp7 网络欺诈防范
2018-2019-2 网络对抗技术 20165322 Exp7 网络欺诈防范 目录 实验原理 实验内容与步骤 简单应用SET工具建立冒名网站 ettercap DNS spoof 结合应用两种技术, ...
- ubuntu之路——day8.4 Adam自适应矩估计算法
基本上讲,Adam就是将day8.2提到的momentum动量梯度下降法和day8.3提到的RMSprop算法相结合的优化算法 首先初始化 SdW = 0 Sdb = 0 VdW = 0 Vdb = ...
- Software Project Management_JUnit && Maven
Task1: Develop the project “HelloWorld” -A .java program: Just print out “Hello” + your name; -A tes ...