玩转spring boot——国际化
2017-05-08 13:56
369 查看
[b]前言[/b]
在项目开发中,可能遇到国际化的问题,而支持国际化却是一件很头疼的事。但spring boot给出了一个非常理想和方便的方案。
[b]一、准备工作[/b]
pom.xml:
App.java
创建国际化配置文件:LocaleConfig.java
MainController.java:
在resources目录增加两个properties文件,分别为:
messages_en_US.properties:
messages_zh_CN.properties:
[b]二、前端调用[/b]
在thymeleaf模板引擎中使用#{}的标签就能调用messages中的内容
index.html:
项目结构如下:
运行效果:
[b]三、后端渲染[/b]
修改MainController类:
其中,MessageSource类可以获取messages的内容。
index.html:
一个使用了#{}标签直接调用messages的内容,另一个使用了${}标签来获取后台的值
运行效果:
[b] 总结[/b]
国际化就已经实现了,然而更完美的做法是当第一次请求时,在后台通过Request获取到一个初始的语言。当获取到的语言和用户所需要的语言不一直时,才需要在前端UI再去设置哪个语言是用户所需要的。
代码下载:https://github.com/carter659/spring-boot-14.git
如果你觉得我的博客对你有帮助,可以给我点儿打赏,左侧微信,右侧支付宝。
有可能就是你的一点打赏会让我的博客写的更好:)
返回玩转spring boot系列目录
在项目开发中,可能遇到国际化的问题,而支持国际化却是一件很头疼的事。但spring boot给出了一个非常理想和方便的方案。
[b]一、准备工作[/b]
pom.xml:
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
App.java
创建国际化配置文件:LocaleConfig.java
package com.example; import java.util.Locale; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.i18n.SessionLocaleResolver; @Configuration @EnableAutoConfiguration @ComponentScan public class LocaleConfig extends WebMvcConfigurerAdapter { @Bean public LocaleResolver localeResolver() { SessionLocaleResolver slr = new SessionLocaleResolver(); // 默认语言 slr.setDefaultLocale(Locale.US); return slr; } @Bean public LocaleChangeInterceptor localeChangeInterceptor() { LocaleChangeInterceptor lci = new LocaleChangeInterceptor(); // 参数名 lci.setParamName("lang"); return lci; } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(localeChangeInterceptor()); } }
MainController.java:
package com.example; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; /** * 控制器 博客出处:http://www.cnblogs.com/GoodHelper/ * */ @Controller public class MainController { @GetMapping("/") public String index() { return "index"; } }
在resources目录增加两个properties文件,分别为:
messages_en_US.properties:
hello=hello
messages_zh_CN.properties:
hello=\u4F60\u597D
[b]二、前端调用[/b]
在thymeleaf模板引擎中使用#{}的标签就能调用messages中的内容
index.html:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>玩转spring boot——国际化</title> </head> <body> <h1>玩转spring boot——国际化</h1> <h4> <a href="http://www.cnblogs.com/GoodHelper/">from 刘冬的博客</a> </h4> <br /> <br /> <a href="/?lang=en_US">English(US)</a> <a href="/?lang=zh_CN">简体中文</a> <br /> <h3 th:text="#{hello}"></h3> <br /> <br /> <a href="http://www.cnblogs.com/GoodHelper/">点击访问原版博客(www.cnblogs.com/GoodHelper)</a> </body> </html>
项目结构如下:
运行效果:
[b]三、后端渲染[/b]
修改MainController类:
package com.example; import java.util.Locale; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; /** * 控制器 博客出处:http://www.cnblogs.com/GoodHelper/ * */ @Controller public class MainController { @Autowired private MessageSource messageSource; @GetMapping("/") public String index(Model model) { Locale locale = LocaleContextHolder.getLocale(); model.addAttribute("world", messageSource.getMessage("world", null, locale)); return "index"; } }
其中,MessageSource类可以获取messages的内容。
index.html:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>玩转spring boot——国际化</title> </head> <body> <h1>玩转spring boot——国际化</h1> <h4> <a href="http://www.cnblogs.com/GoodHelper/">from 刘冬的博客</a> </h4> <br /> <br /> <a href="/?lang=en_US">English(US)</a> <a href="/?lang=zh_CN">简体中文</a> <br /> <h3 th:text="#{hello} + ' , ' + ${world}"></h3> <br /> <br /> <a href="http://www.cnblogs.com/GoodHelper/">点击访问原版博客(www.cnblogs.com/GoodHelper)</a> </body> </html>
一个使用了#{}标签直接调用messages的内容,另一个使用了${}标签来获取后台的值
运行效果:
[b] 总结[/b]
国际化就已经实现了,然而更完美的做法是当第一次请求时,在后台通过Request获取到一个初始的语言。当获取到的语言和用户所需要的语言不一直时,才需要在前端UI再去设置哪个语言是用户所需要的。
代码下载:https://github.com/carter659/spring-boot-14.git
如果你觉得我的博客对你有帮助,可以给我点儿打赏,左侧微信,右侧支付宝。
有可能就是你的一点打赏会让我的博客写的更好:)
返回玩转spring boot系列目录
相关文章推荐
- 玩转spring boot——websocket
- 玩转spring boot——结合docker
- 玩转Spring Boot 使用Spring security
- 自己动手在Spring-Boot上加强国际化功能的示例
- 1.玩转Spring Boot 前言
- 3.玩转Spring Boot 不使用spring-boot-starter-parent构建
- 10.玩转Spring Boot 集成Mybatis
- Spring boot 国际化自动加载资源文件去除默认国际化文件
- Springboot国际化i18n
- 玩转spring boot——简单登录认证
- 玩转spring boot——负载均衡与session共享
- 14.玩转Spring Boot 多数据源
- 16.玩转Spring Boot 使用Spring security 集成CAS
- 玩转spring boot——结合AngularJs和JDBC
- 17.玩转Spring Boot 集成Dubbo
- 玩转spring boot——结合阿里云持续交付
- 玩转spring boot——war部署
- 玩转Spring Boot 集成Dubbo
- SpringBoot学习之验证信息国际化
- 玩转Spring Boot 集成Dubbo