SpringBoot配置国际化

发布时间:2024-12-15 06:07

学习国际美食配方,丰富餐桌文化 #生活乐趣# #美食烹饪乐趣# #国际美食探索#

最新推荐文章于 2022-01-29 09:22:21 发布

迪士尼在逃公主ddd 于 2020-08-14 00:14:14 发布

第一步

在resources下面建一个i18n的包。再新建需要的properties,如下:
在这里插入图片描述
并且写好每个语音对应的语句:
在这里插入图片描述

第二步

用 #{} 取出所配置的语句:
在这里插入图片描述
写好对应的链接:

<a th:href="@{/login.html(l='zh_CN')}">中文</a> <a th:href="@{/login.html(l='en_US')}">英文</a> 12

第三步

写一个继承LocaleResolver的类:

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[] s = language.split("_"); locale = new Locale(s[0],s[1]); } return locale; } @Override public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { } }

123456789101112131415161718

在继承WebMvcConfigurer的类上加入刚才写好的类

@Configuration public class MyMvcConfig implements WebMvcConfigurer { //自定义国际化 @Bean public LocaleResolver localeResolver(){ return new MyLocaleResolver(); } } 12345678

可能遇到的乱码问题:
一:
在这里插入图片描述
二:在application.properties加入

#国际化配置 spring.messages.encoding=UTF-8 spring.messages.basename=i18n.login #或者 spring.messages.basename=i18n/login 路径一定要对应好 1234

网址:SpringBoot配置国际化 https://www.yuejiaxmz.com/news/view/478883

相关内容

Springboot配置 logback日志框架,且自动清理生效
【开题报告】基于SpringBoot的闲置物品置换平台的设计与实现
Springboot基于SpringBoot的宠物门诊系统6f8jy
springboot基于安卓android的个性化服装搭配系统APP
Springboot基于springboot的校园生活工具租赁系统cftei
springboot旧物置换网站 (附源码)
基于Springboot框架黑龙江哈尔滨某大学二手物品交易系统设计与实现 研究背景和意义、国内外现状
springboot中unexpected error (type=Not Found, status=404)/WEB
【开题报告】基于SpringBoot的旧衣物上门回收系统的设计与实现
二手家电交易系统(SpringBoot,SSM,MySQL)

随便看看