当前位置:
首页 >
前端技术
> javascript
>内容正文
javascript
Spring Boot中通过Accept-Language头信息设置国际化内容
生活随笔
收集整理的这篇文章主要介绍了
Spring Boot中通过Accept-Language头信息设置国际化内容
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
目录
概念
演示
操作步骤
概念
使用Spring MVC编写国际化配置文件:
1. 编写国际化配置文件;
2. 使用ResourceBundleMessageSource管理国际化资源文件;
3. 在页面使用fmt:message取出国际化内容;
使用Spring Boot编写国际化;
1. 编写国际化配置文件;
2. 使用模板引擎,把数据送到界面上去
这里要注意:默认情况下,只有在templates里面的文件,才能获取模版的支持:
演示
程序结构如下:
程序截图如下!
英文界面:
中文界面:
源码如下:
HelloController.java
package internationdemo.demo.controller;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;@Controller public class HelloController {@RequestMapping({"/", "index.html"})public String index(){return "index";} }index.properties
index.btn=登录# index.HAY=怎么是你# index.HOAY=怎么老是你# index.password=#密码 index.remember=记住我# index.title=标题#index_en_US.properties
index.btn=Sign in index.HAY=How Are You index.HOAY=How Old Are You index.password=PassWord index.remember=remember index.title=titleindex_zh_CN.properties
index.btn=登录 index.HAY=怎么是你 index.HOAY=怎么老是你 index.password=密码 index.remember=记住我 index.title=标题index.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head><meta charset="UTF-8"><title th:text="#{index.title}">Title</title> </head> <body><h1 th:text="#{index.HAY}">How Are You</h1><label th:text="#{index.HOAY}">How Old Are You</label><input placeholder="PassWord" th:placeholder="#{index.password}"><br><div><label><input type="checkbox">[[#{index.remember}]]</label></div><button type="submit" th:text="#{index.btn}">Sign in</button> </body> </html>application.properties
spring.messages.basename=i18n.index
操作步骤
添加国际化:
添加好两个properties后,Resource Bundle 'index'就会自动出来;
随后就能这样进行添加:
选中任意一个:
添加即可:
总结
以上是生活随笔为你收集整理的Spring Boot中通过Accept-Language头信息设置国际化内容的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: C++笔记-构造内存泄漏检测类的基本思路
- 下一篇: Spring Boot中@Configu