├── src
└── main
│ ├── resources
│ ├── i18n
│ │ ├── messages.properties
│ │ ├── messages_zh_CN.properties
│ │ └── messages_en_US.properties
│ ├── templates
│ │ └── hello.html
│ └── application.properties
│ ├── webapp
│ ├── index.jsp
│ └── WEB-INF
│ │ └── web.xml
│ └── java
│ └── com
│ └── zheng
│ ├── Application.java
│ ├── web
│ ├── HelloController.java
│ └── LanguageController.java
│ ├── utils
│ └── LocaleMessageSourceUtil.java
│ └── config
│ └── I18nConfig.java
├── .gitignore
├── README.md
└── pom.xml
/src/main/resources/i18n/messages.properties:
--------------------------------------------------------------------------------
1 | welcome=你好
--------------------------------------------------------------------------------
/src/main/resources/i18n/messages_zh_CN.properties:
--------------------------------------------------------------------------------
1 | welcome=你好
--------------------------------------------------------------------------------
/src/main/resources/i18n/messages_en_US.properties:
--------------------------------------------------------------------------------
1 | welcome=hello welcome
--------------------------------------------------------------------------------
/src/main/webapp/index.jsp:
--------------------------------------------------------------------------------
1 |
2 |
3 | Hello World!
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /target/
2 | /.idea/
3 | /.settings/
4 | /.classpath
5 | /.project
6 | /spring-boot-i18n.iml
7 |
--------------------------------------------------------------------------------
/src/main/webapp/WEB-INF/web.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 | Archetype Created Web Application
7 |
8 |
--------------------------------------------------------------------------------
/src/main/java/com/zheng/Application.java:
--------------------------------------------------------------------------------
1 | package com.zheng;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | * Created by zhenglian on 2016/10/9.
8 | */
9 | @SpringBootApplication
10 | public class Application {
11 | public static void main(String[] args) {
12 | SpringApplication.run(Application.class, args);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/main/resources/templates/hello.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Hello World
6 |
7 |
8 |
9 |
13 |
14 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/src/main/java/com/zheng/web/HelloController.java:
--------------------------------------------------------------------------------
1 | package com.zheng.web;
2 |
3 | import com.zheng.utils.LocaleMessageSourceUtil;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.stereotype.Controller;
6 | import org.springframework.web.bind.annotation.RequestMapping;
7 |
8 | /**
9 | * Created by zhenglian on 2016/10/9.
10 | */
11 | @Controller
12 | public class HelloController {
13 | @Autowired
14 | private LocaleMessageSourceUtil messageSourceUtil;
15 |
16 | @RequestMapping("/hello")
17 | public String hello() {
18 | String welcome = messageSourceUtil.getMessage("welcome");
19 | System.out.println(welcome);
20 | return "hello";
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | ########################################################
2 | ###THYMELEAF (ThymeleafAutoConfiguration)
3 | ########################################################
4 | spring.thymeleaf.prefix=classpath:/templates/
5 | spring.thymeleaf.suffix=.html
6 | #spring.thymeleaf.mode=HTML5
7 | spring.thymeleaf.encoding=UTF-8
8 | # ;charset= is added
9 | spring.thymeleaf.content-type=text/html
10 | # set to false for hot refresh
11 | spring.thymeleaf.cache=false
12 |
13 |
14 | ########################################################
15 | ###i18n (MessageSourceAutoConfiguration)
16 | ########################################################
17 |
18 | #设置国际化配置文件存放在classpath:/i18n目录下
19 | spring.messages.basename=i18n/messages
20 | #设置加载资源的缓存失效时间,-1表示永久有效,默认为-1
21 | spring.messages.cache-seconds=3600
22 | #设定message bundles编码方式,默认为UTF-8
23 | #spring.messages.encoding=UTF-8
24 |
25 |
--------------------------------------------------------------------------------
/src/main/java/com/zheng/web/LanguageController.java:
--------------------------------------------------------------------------------
1 | package com.zheng.web;
2 |
3 | import org.springframework.stereotype.Controller;
4 | import org.springframework.web.bind.annotation.RequestMapping;
5 | import org.springframework.web.servlet.LocaleResolver;
6 | import org.springframework.web.servlet.support.RequestContextUtils;
7 |
8 | import javax.servlet.http.HttpServletRequest;
9 | import javax.servlet.http.HttpServletResponse;
10 | import java.util.Locale;
11 |
12 | /**
13 | * Created by zhenglian on 2016/10/12.
14 | */
15 | @Controller
16 | public class LanguageController {
17 |
18 | @RequestMapping("/changeSessionLanauage")
19 | public String changeSessionLanauage(HttpServletRequest request, HttpServletResponse response,
20 | String lang){
21 | System.out.println(lang);
22 | LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
23 | if("zh".equals(lang)){
24 | localeResolver.setLocale(request, response, new Locale("zh", "CN"));
25 | }else if("en".equals(lang)){
26 | localeResolver.setLocale(request, response, new Locale("en", "US"));
27 | }
28 | return "redirect:/hello";
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/com/zheng/utils/LocaleMessageSourceUtil.java:
--------------------------------------------------------------------------------
1 | package com.zheng.utils;
2 |
3 | import org.springframework.context.MessageSource;
4 | import org.springframework.context.i18n.LocaleContextHolder;
5 | import org.springframework.stereotype.Component;
6 |
7 | import javax.annotation.Resource;
8 | import java.util.Locale;
9 |
10 | /**
11 | * Created by zhenglian on 2016/10/9.
12 | */
13 | @Component
14 | public class LocaleMessageSourceUtil {
15 | @Resource
16 | private MessageSource messageSource;
17 |
18 | public String getMessage(String code) {
19 | return getMessage(code, null);
20 | }
21 |
22 | /**
23 | *
24 | * @param code :对应messages配置的key.
25 | * @param args : 数组参数.
26 | * @return
27 | */
28 | public String getMessage(String code, Object[] args){
29 | return getMessage(code, args, "");
30 | }
31 |
32 | /**
33 | *
34 | * @param code :对应messages配置的key.
35 | * @param args : 数组参数.
36 | * @param defaultMessage : 没有设置key的时候的默认值.
37 | * @return
38 | */
39 | public String getMessage(String code,Object[] args,String defaultMessage){
40 | //这里使用比较方便的方法,不依赖request.
41 | Locale locale = LocaleContextHolder.getLocale();
42 | return messageSource.getMessage(code, args, defaultMessage, locale);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ### 国际化配置
2 | #### 在页面上使用
3 | springboot默认就支持国际化配置,只需要添加国际化配置文件即可
4 | 格式为messages.properties(默认),
5 | messages_zh_CN.properties(中文)
6 | messages_en_US.properties(英文)
7 | 将其放到classpath:/resources下即可
8 | 需要注意的是在页面上引用时,需要通过
9 | 的方式引用
10 | springboot内部是通过MessageSourceAutoConfiguration来配置的
11 | 我们可以通过在application.properties中配置属性改变国际化配置的一些信息
12 | >
13 | > #设置国际化配置文件存放在classpath:/i18n目录下
14 | > spring.messages.basename=i18n/messages
15 | > #设置加载资源的缓存失效时间,-1表示永久有效,默认为-1
16 | > spring.messages.cache-seconds=3600
17 | > #设定message bundles编码方式,默认为UTF-8
18 | > #spring.messages.encoding=UTF-8
19 | ####在代码中使用
20 | 在代码中直接通过@Autowired MessageSource 并通过它来获取需要的国际化字段
21 | 当然需要使用系统当前的locale对象,可以通过:LocaleContextHolder.getLocale();
22 | 或者RequestContextUtils.getLocale(request);获取
23 |
24 | ### 国际化LocaleResolver
25 | springboot默认采用AcceptHeaderLocaleResolver来解析国际化信息,但是这个Resolver是根据浏览器所在的
26 | 操作系统的内核区域设置来决定语言的,一般使用的少
27 | 采用会话Resolver,SessionLocaleResolver,在用户请求的当前会话过程中有效,用户下一次请求时恢复到原来的
28 | 语言设置
29 | 还包含其他Resolver
30 | CookieLocaleResolver
31 | FixedLocaleResolver 这个Resolver不允许用户修改语言
32 |
33 | 除了使用LocaleResolver.setLocale()来设置语言区域之外,
34 | 还可以使用拦截器修改语言区域:LocaleChangeInterceptor
35 | 但是注意这种方式可以和session cookie Resolver一起使用,但是不能和FixedLocaleResolver一起使用,抛异常,我的哥!
--------------------------------------------------------------------------------
/src/main/java/com/zheng/config/I18nConfig.java:
--------------------------------------------------------------------------------
1 | package com.zheng.config;
2 |
3 | import org.springframework.context.annotation.Bean;
4 | import org.springframework.context.annotation.Configuration;
5 | import org.springframework.web.servlet.LocaleResolver;
6 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
7 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
8 | import org.springframework.web.servlet.i18n.CookieLocaleResolver;
9 | import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
10 |
11 | import java.util.Locale;
12 |
13 | /**
14 | * Created by zhenglian on 2016/10/12.
15 | */
16 | @Configuration
17 | public class I18nConfig extends WebMvcConfigurationSupport {
18 |
19 | /**
20 | * session区域解析器
21 | * @return
22 | */
23 | // @Bean
24 | // public LocaleResolver localeResolver() {
25 | // SessionLocaleResolver resolver = new SessionLocaleResolver();
26 | // //这里通过设置China.US可以进行中英文转化
27 | // resolver.setDefaultLocale(Locale.US);
28 | //
29 | // return resolver;
30 | // }
31 |
32 |
33 | /**
34 | * cookie区域解析器
35 | * @return
36 | */
37 | @Bean
38 | public LocaleResolver localeResolver() {
39 | CookieLocaleResolver slr = new CookieLocaleResolver();
40 | //设置默认区域,
41 | slr.setDefaultLocale(Locale.CHINA);
42 | slr.setCookieMaxAge(3600);//设置cookie有效期.
43 | return slr;
44 | }
45 |
46 | // @Bean
47 | // public LocaleResolver localeResolver() {
48 | // FixedLocaleResolver slr = new FixedLocaleResolver ();
49 | // //设置默认区域,
50 | // slr.setDefaultLocale(Locale.US);
51 | // return slr;
52 | // }
53 |
54 |
55 | @Bean
56 | public LocaleChangeInterceptor localeChangeInterceptor() {
57 | LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
58 | // 设置请求地址的参数,默认为:locale
59 | // lci.setParamName(LocaleChangeInterceptor.DEFAULT_PARAM_NAME);
60 | return lci;
61 | }
62 |
63 | @Override
64 | public void addInterceptors(InterceptorRegistry registry) {
65 | registry.addInterceptor(localeChangeInterceptor());
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | com.zheng
5 | spring-boot-i18n
6 | war
7 | 1.0-SNAPSHOT
8 | spring-boot-i18n Maven Webapp
9 | http://maven.apache.org
10 |
11 | org.springframework.boot
12 | spring-boot-starter-parent
13 | 1.3.3.RELEASE
14 |
15 |
16 |
17 |
18 | org.springframework.boot
19 | spring-boot-starter-web
20 |
21 |
22 | org.springframework.boot
23 | spring-boot-starter-test
24 |
25 |
26 | junit
27 | junit
28 |
29 |
30 |
31 | org.springframework.boot
32 | spring-boot-starter-thymeleaf
33 |
34 |
35 |
36 |
37 | spring-boot-i18n
38 |
39 |
40 |
41 | spring-releases
42 | Spring Releases
43 | https://repo.spring.io/libs-release
44 |
45 |
46 | org.jboss.repository.releases
47 | JBoss Maven Release Repository
48 | https://repository.jboss.org/nexus/content/repositories/releases
49 |
50 |
51 |
52 | central-repos3
53 | Central Repository3
54 | http://maven.jahia.org/maven2/
55 |
56 | true
57 |
58 |
59 | false
60 |
61 |
62 |
63 |
64 |
65 |
66 | spring-releases
67 | Spring Releases
68 | https://repo.spring.io/libs-release
69 |
70 |
71 |
72 |
73 |
--------------------------------------------------------------------------------