发布时间:2023-04-22 10:30
a、在IDEA的resources文件夹下创建il8n,在il8n下创建login文件夹;
b、在login文件下创建login.properties、login_zh_CN.properties、login_en_US.properties;
login.properties:代表默认显示的语种,login_zh_CN.properties:切换为页面显示中文,login_en_US.properties:切换为页面显示英文。
如果要显示更多的语言的,则需要自己定义更多的properties文件
c、创建文件后选择 login_zh_CN.properties、login_en_US.properties任意一个文件,点击下方的Resource Bundle
在application.properties中添加
spring.messages.basename=i18n.login
#{}中的内容表示国际化处理
Signin Template for Bootstrap
package lhz.lx.demo.component;
import org.springframework.boot.autoconfigure.web.WebMvcProperties;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
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 l = request.getParameter("l");
Locale locale = Locale.getDefault();
if(!StringUtils.isEmpty(l)){
String[] split = l.split("_");
locale = new Locale(split[0],split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
package lhz.lx.demo.config;
import lhz.lx.demo.component.MyLocaleResolver;
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.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
源码下载:SpringBoot+Thymeleaf实现国际化源码_thymeleaf国际化-Java代码类资源-CSDN下载