一.网页配置国际化语言
1. 网页直接获取配置文件中的内容
我们在项目中resource文件包下,设置一个: message.properties
这种文件的命名格式是国际默认的。美国:message_zh_CN.properties 中国:message_en_US.properties
我们分别在这三个文件中放入一些内容,让程序去调用,然后显示到网页中
message.properties:
welcome=Hello! Welcome to my website-usa.
message_en_US.properties:
welcome=Hello! Welcome to my website.
message_zh_CN.properties:
welcome=\u4F60\u597D\uFF01\u6B22\u8FCE\u6765\u5230\u6211\u4EEC\u7684\u4E16\u754C\u3002
我们可以把这三个文件放在resource文件包下,则不用去指定其他路径。
但我这里是放到了:resource/il18n/ 包下
dev.yml:
只需要配置:spring.messages.basename=i18n/message 即可,注意,这里不要后缀,只要国际化message就可以,能够识别 。 防止解析 乱码 encoding: UTF-8
spring:
main:
banner-mode: OFF
devtools:
restart:
enabled: true
additional-paths: src/main/java
thymeleaf:
cache: false
messages:
basename: i18n/message
encoding: UTF-8
server:
port: 8008
servlet:
context-path: /SpringBoot
logging:
config: classpath:springboot-log.xml
#logging:
# level:
# com.SpringBoot.demo: debug
# file:
# name: D:\logs\log_springboot.log
# max-history: 7
# max-size: 10MB
# total-size-cap: 1GB
# clean-history-on-start: FALSE
logs:
home:
path: D:\logs
我们需要一个控制层程序:
MessageController.java:
package com.SpringBoot.demo.message;
import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("m")
public class MessageController {
@GetMapping("show")
public String show() {
return "i18message";
}
}
展示的网页:
注意,在使用国际化语言,调用message中的信息,是可以直接使用 #{ } 。
注意:可以直接在html中就可以获取message配置文件中的内容
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>This is a Thymeleaf page</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
This is Thymeleaf page!
<p th:text="#{welcome}"></p>
</body>
</html>
启动测试下: 程序运行没有出错。并成功展示了 “Hello! Welcome to my website”
我的浏览器设置的是默认的中文,所以直接显示中文的message_zh_CN.properties资源
2. 控制层获取配置文件的内容
因为springboot中的是国际化语言,已经在spring中是配置好了的,所以我们可以直接注入就能够使用配置文件中的内容。
message.properties:
welcome=Hello! Welcome to my website-usa.
message_en_US.properties:
welcome=Hello! Welcome to my website.
message_zh_CN.properties:
welcome=\u4F60\u597D\uFF01\u6B22\u8FCE\u6765\u5230\u6211\u4EEC\u7684\u4E16\u754C\u3002
控制层直接注入即可: @Autowired
package com.SpringBoot.demo.message;
import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("m")
public class MessageController {
@Autowired
private MessageSource messageSource;
@GetMapping("show")
public String show(ModelMap model, Locale locale) {
String wel =messageSource.getMessage("welcome", null, locale);
model.addAttribute("wel",wel);
return "i18message";
}
}
从后台区数据,我们需要在网页上用$符号
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>This is a Thymeleaf page</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
This is Thymeleaf page!
<h1>==========网页直接获取============</h1>
<p th:text="#{welcome}"></p>
<h1>===========通过后台抓取===========</h1>
<p th:text="${wel}"></p>
</body>
</html>
便可以直接抓去后台数据
二. 使用拦截器实现语种切换
即在URL中可以设置语种,即每次进行url拦截
新增配置类,LocaleConfig.java:
localeResolver() 用于设置web的默认语种
localeInterceptor() 用于对mvc网页的拦截器进行处理,需要重写拦截器。
setParamName("lang") 通过设置语言,设置url的新路径中需要加上lang来改变语种.http://localhost:8008/SpringBoot/m/show?lang=
package com.SpringBoot.demo.message;
import java.util.Locale;
import org.springframework.context.annotation.Bean;
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.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
@Configuration
public class LocaleConfig {
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(Locale.US);
return localeResolver;
}
@Bean
public WebMvcConfigurer localeInterceptor() {
return new WebMvcConfigurer() {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// TODO Auto-generated method stub
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
registry.addInterceptor(localeChangeInterceptor);
}
};
}
}
运行测试:
测试中文:http://localhost:8008/SpringBoot/m/show?lang=zh_CN
测试英文:http://localhost:8008/SpringBoot/m/show?lang=en_US
三. 网页传参
message.properties:
welcome=Hello! Welcome to my website-usa.
wel.text=Hello! Welcome {0}({1}) to my website.
message_zh_CN.properties:
welcome=\u4F60\u597D\uFF01\u6B22\u8FCE\u6765\u5230\u6211\u4EEC\u7684\u4E16\u754C\u3002
wel.text=\u4F60\u597D\uFF0C\u6B22\u8FCE{0}({1})
message_en_US.properties:
welcome=Hello! Welcome to my website-usa.
wel.text=Hello! Welcome {0}({1}) to my website.
直接在网页上传参并显示: 利用国际化 #{ }
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>This is a Thymeleaf page</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
This is Thymeleaf page!
<h1>=========前端直接获取配置文件=============</h1>
<p th:text="#{welcome}"></p>
<h1>==========后台控制层获取============</h1>
<p th:text="${wel}"></p>
<h1>=====This is parameter from properties file=====前端直接获取============</h1>
<p th:text="#{wel.text('Cherry', 'China')}"></p>
</body>
</html>
运行测试:
四. 后台传参
因为可能遇到传参的个数不一样,所以我们可以自己定制一个工具类:
MessageUtil.java:
@Component : 把这个类声明为@Component类,直接配置到了spring容器中,注入后就可以使用
getMessage(String key) 解析:直接获取message中的信息
getMessage(String key, String[] params) 解析:有传参
public String getMessage(String key, String[] params, String defaultValue) : 解析: 有传参有默认值。或者没有参数会设置为默认值
package com.SpringBoot.demo.message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
@Component
public class MessageUtil {
@Autowired
private MessageSource messageSource;
public String getMessage(String key) {
return messageSource.getMessage(key, null, LocaleContextHolder.getLocale());
}
public String getMessage(String key, String[] params) {
return messageSource.getMessage(key, params, LocaleContextHolder.getLocale());
}
public String getMessage(String key, String[] params, String defaultValue) {
return messageSource.getMessage(key, params, defaultValue, LocaleContextHolder.getLocale());
}
}
控制层:使用上述工具类
MessageController.java:
package com.SpringBoot.demo.message;
import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("m")
public class MessageController {
@Autowired
private MessageUtil messageUtil;
@GetMapping("show")
public String show(ModelMap model, Locale locale) {
String wel = messageUtil.getMessage("welcome");//messageSource.getMessage("welcome", null, locale);
String[] params = {"zhm", "China"};
String welWithParams = messageUtil.getMessage("wel.text", params);
String defaultValue = messageUtil.getMessage("wel.slije", null, "This is a default value");
model.addAttribute("wel", wel);
model.addAttribute("welWithParams", welWithParams);
model.addAttribute("defaultValue", defaultValue);
return "i18message";
}
}
展示网页:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>This is a Thymeleaf page</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
This is Thymeleaf page!
<h1>=========前端直接获取配置文件=============</h1>
<p th:text="#{welcome}"></p>
<h1>==========后台控制层获取============</h1>
<p th:text="${wel}"></p>
<h1>=====This is parameter from properties file=====前端直接获取============</h1>
<p th:text="#{wel.text('Cherry', 'China')}"></p>
<h1>=====This is parameter show from controller=====后台直接获取============</h1>
<p th:text="${welWithParams}"></p>
<p th:text="${defaultValue}"></p>
</body>
</html>
测试结果: 后台直接得到参数