springboot页面国际化
页面交给thymeleaf
先在IDEA中统一设置properties的编码问题
在resources资源文件下新建一个i18n目录
建立一个login.properties文件,还有一个login_zh_CN.properties;
en_US

这里idea 可能需要安装插件Resource bundles editor

配置yaml文件
spring:
messages:
basename: i18n.login
html中 前端页面字体中英通过 thymeleaf获取的 th:text="#{在login.properties配置的中英键}
前端中英切换按钮 这个还需要去自定义 处理的组件类
<a class="btn btn-sm" th:href="@{/index.html(l/'zh_CN')}">中文</a><a class="btn btn-sm" th:href="@{/index.html(l/'en_US')}">English</a>
public class MyLocaleResolver implements LocaleResolver { //解析请求 @Override public Locale resolveLocale(HttpServletRequest request) { String language = request.getParameter("l"); 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 httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) { } }
在MvcConofig下添加bean
@Configuration public class MyMvcConfig implements WebMvcConfigurer {//自定义配置 @Bean public LocaleResolver localeResolver(){ return new MyLocaleResolver(); } }
可以切换
搬运狂神..
 
 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号