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-国际化的更多相关文章

  1. 18点睛Spring4.1-Meta Annotation

    18.1 Meta Annotation 元注解:顾名思义,就是注解的注解 当我们某几个注解要在多个地方重复使用的时候,写起来比较麻烦,定义一个元注解可以包含多个注解的含义,从而简化代码 下面我们用& ...

  2. 04点睛Spring4.1-资源调用

    转发:https://www.iteye.com/blog/wiselyman-2210666 4.1 Resource spring用来调用外部资源数据的方式 支持调用文件或者是网址 在系统中调用p ...

  3. 14点睛Spring4.1-脚本编程

    转发:https://www.iteye.com/blog/wiselyman-2212678 14.1 Scripting脚本编程 脚本语言和java这类静态的语言的主要区别是:脚本语言无需编译,源 ...

  4. 00点睛Spring4.1-环境搭建

    转载:https://www.iteye.com/blog/wiselyman-2210250 0.1 前置条件 Spring 4.1提倡基于Java Config和注解的配置,所以本教程通篇不会采用 ...

  5. 05点睛Spring MVC 4.1-服务器端推送

    转发:https://www.iteye.com/blog/wiselyman-2214626 5.1 服务器端推送 SSE(server send event)是一种服务器端向浏览器推送消息的技术, ...

  6. 17点睛Spring4.1-@Conditional

    17.1 @Conditional @Conditional为按照条件配置spring的bean提供了支持,即满足某种条件下,怎么配置对应的bean; 应用场景 当某一个jar包在classpath中 ...

  7. 16点睛Spring4.1-TaskScheduler

    转发:https://www.iteye.com/blog/wiselyman-2213049 16.1 TaskScheduler 提供对计划任务提供支持; 使用@EnableScheduling开 ...

  8. 15点睛Spring4.1-TaskExecutor

    转发:https://www.iteye.com/blog/wiselyman-2212679 15.1 TaskExecutor spring的TaskExecutor为在spring环境下进行并发 ...

  9. 13点睛Spring4.1-Spring EL

    13.1 Spring EL Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似jsp的EL表达式语言; 本教程关注于在注解中使用Spring EL; Spring EL ...

随机推荐

  1. sqoop job 实现自动增量导入

    一.测试环境 1.MySQL表结构 mysql> show create table autoextend\GCREATE TABLE `autoextend` (  `id` bigint(2 ...

  2. RegularExpression正则表达式

    1.必須為大寫字母,小寫字母,數字和符號的至少其中3種組合 The new password should contain any three of different syntax which is ...

  3. BZOJ 2159: Crash 的文明世界 第二类斯特林数+树形dp

    这个题非常巧妙啊~ #include <bits/stdc++.h> #define M 170 #define N 50003 #define mod 10007 #define LL ...

  4. leetcode 63 简单题

    题目很水... 直接放代码了 int uniquePathsWithObstacles(int** obstacleGrid, int obstacleGridRowSize, int obstacl ...

  5. YII框架的事件机制

    一.什么是事件机制 解释:发生了一件事情,然后某些东西对这件事作出反应. 例子:假设发生了A同学结婚事件,然后B同学给份子钱反应,那么,B是怎么知道(监听)A事件的发生了呢,有两种办法. 扫描式:B不 ...

  6. Struts2.5入门之环境设置

    1. 下载Struts2.5,下载地址 2. 新建项目struts2 3. 解压后将struts\lib下的所有jar包复制到WEB-INF/lib文件夹下 4. 修改web.xml配置文件,添加如下 ...

  7. 重写mybatis的字符串类型处理器

    1.简介 无论是 MyBatis 在预处理语句(PreparedStatement)中设置一个参数时,还是从结果集中取出一个值时, 都会用类型处理器将获取的值以合适的方式转换成 Java 类型. St ...

  8. js jquery 实现 排班,轮班,日历,日程。使用fullcalendar 插件

    如果想用fullcalendar实现排班功能,或者日历.日程功能.那么只需要简单的几步: 这里先挂官网链接: fullcalendar fullcalendar官网下载链接 一.下载及简单配置 1.这 ...

  9. LDA算法 (主题模型算法) 学习笔记

    转载请注明出处: http://www.cnblogs.com/gufeiyang 随着互联网的发展,文本分析越来越受到重视.由于文本格式的复杂性,人们往往很难直接利用文本进行分析.因此一些将文本数值 ...

  10. [spring-boot] 多环境配置

    application-{profile}.properties 按照格式创建两个配置文件,一个DEV环境,一个测试环境 修改其端口: server.port=8888 DEV server.port ...