1、src\main\resources\i18n
创建:lan.properties、lan_zh_CN.properties、lan_en_US.properties,默认、中文、英文。
password=密码 和 password=password
核心配置文件application.yml
1 2 3 4 5 6 7 8 9 10 11 12 | server: port: 8091 spring: thymeleaf: enabled: true #开启thymeleaf视图解析 encoding: utf-8 #编码 prefix: classpath:/templates/ #前缀 cache: false #是否使用缓存 mode: HTML #严格的HTML语法模式 suffix: .html #后缀名 messages: basename: i18n.login |
basename要设置成i18n.login,指定这个配置文件。
2、自定义区域解析器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package com.jay.SpringBootStudy8.config; 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 httpServletRequest) { String l = httpServletRequest.getParameter( "l" ); Locale locale = Locale.getDefault(); if (!( "" . equals (l) || l == null )) { String[] split = l.split( "_" ); locale = new Locale(split[0], split[1]); } return locale; } @Override public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) { } } |
3、注册区域自定义解析器 到 自定义视图解析器中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package com.jay.SpringBootStudy8.config; 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.WebMvcConfigurer; @Configuration public class MyMvcConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController( "/" ).setViewName( "index" ); registry.addViewController( "/index.html" ).setViewName( "index" ); } @Bean public LocaleResolver localeResolver(){ return new MyLocaleResolver(); } } |
4、注册的区域解析器方法签名是要和WebMvcAutoConfiguration类中的一样,必须是public LocaleResolver localeResolver...
5、index.html网页
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <!DOCTYPE html> <html lang= "en_US" xmlns:th= "http://www.thymeleaf.org" > <head> <meta charset= "UTF-8" /> <meta name= "viewport" content= "width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" > <meta http-equiv= "X-UA-Compatible" content= "ie=edge" > <title>index</title> </head> <body> <div th:text= "#{login.password}" ></div> <a th:href= "@{/index(l='zh_CN')}" >中文</a> <a th:href= "@{/index(l='en_US')}" >English</a> </body> </html> |
6、Controller
1 2 3 4 5 6 7 | @Controller public class IndexController { @RequestMapping( "/index" ) public String index(Model model){ return "index" ; } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
2017-08-10 mysql只保留一条有效数据,删除其他重复的数据