1.编写properties文件
默认properties
login.btn=\u767B\u5F55
login.password=\u5BC6\u7801
login.remember=\u8BB0\u4F4F\u6211
login.tip=\u8BF7\u767B\u5F55
login.username=\u7528\u6237\u540D
中文properties
login.btn=\u767B\u5F55
login.password=\u5BC6\u7801
login.remember=\u8BB0\u4F4F\u6211
login.tip=\u8BF7\u767B\u5F55
login.username=\u7528\u6237\u540D
英文properties
login.btn=Sign in
login.password=Password
login.remember=Remember me
login.tip=Please sign in
login.username=Username
2.在application.properties中配置
#配置绑定 i18n
spring.messages.basename=i18n.login
3.编写config配置类
特别要注意到是不要导错包
package com.ckm.config;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
public class MyLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
String language = request.getParameter("l");
System.out.println("参数:"+language);
Locale locale = Locale.getDefault();
if (!StringUtils.isEmpty(language)){
String[] split = language.split("_");
locale = new Locale(split[0], split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
注册Bean,让Spring托管
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
4.注意路径以及参数命名
5.查看效果
中文状态
英文状态
|