springboot支持国际化

news/2024/9/29 23:36:43

Spring Boot支持本地化的消息,这样你的应用程序就可以满足不同语言偏好的用户。 默认情况下,Spring Boot会在classpath的根部寻找 messages 资源包(resource bundle)的存在。

自动配置只有在资源包中存在默认的properties文件(默认为 messages.properties )时才生效。如果资源包中只有特定语言的properties,那你需要添加默认的。 没有没有找到任何base name和语言相匹配的文件,MessageSource 不会自动配置。

在application.properties加上如下配置

spring.messages.basename=messages,config.i18n.messages
spring.messages.fallback-to-system-locale=false
// Steps:// --Understand Locale(know language codes)// --Configure Locale resolver and interceptor in spring// -- Create languge files(messages_XX.properties)  messages.properties:default// -- Update Thymeleaf views for i18n  Print text:[[#{key}]]  Attribute:th:value="#{key}"// -- Switch between languages(Englich,Chinese..)   URL paramter:?lang=xx

1.获取语言对应的code

 @GetMapping("/")public String hello( HttpServletRequest request) {// 修改语言是在浏览器上修改,比如谷歌 设置  语言Locale currentLocal = request.getLocale();String countryCode = currentLocal.getCountry();String countryName = currentLocal.getDisplayCountry();String langCode = currentLocal.getLanguage();String langName = currentLocal.getDisplayLanguage();System.out.println(countryCode + ":" + countryName);System.out.println(langCode + ":" + langName);System.out.println("==========================");String[] languages = Locale.getISOLanguages();for (String language : languages) {Locale locale = new Locale(language);// de:德语 en:英语 zh:中文//      System.out.println(language+":"+locale.getDisplayLanguage());}return "hello";}

2.配置如下代码

package com.integration.scaffold.javai18n;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.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;import java.util.Locale;
import java.util.TimeZone;@Configuration
public class MultiLanguageConfig implements WebMvcConfigurer {@Beanpublic LocaleResolver localeResolver() {SessionLocaleResolver slr = new SessionLocaleResolver();slr.setDefaultLocale(Locale.US);// timezone to change datetime outputslr.setDefaultTimeZone(TimeZone.getTimeZone("UTC"));return slr;}@Beanpublic LocaleChangeInterceptor localeChangeInterceptor() {LocaleChangeInterceptor lci = new LocaleChangeInterceptor();lci.setParamName("lang");return lci;}@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(localeChangeInterceptor());}
}

3.创建messages_xx.properties文件 如下示例代码

hello.title=New day, new weather
hello=Hello!
welcome=Welcome to my app
switch-en=Switch to English
switch-zh=Switch to Chinese

 

hello.title=新的一天新气象
hello=你好
welcome=欢迎来到APP
switch-en=切换到英语
switch-zh=切换到中文

 

4.写一个Thymeleaf,格式为:

text:[[#{key}]]  Attribute:th:value="#{key}"
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title th:text="#{welcome}"></title>
</head>
<body>
<div><h1>[[#{hello.title}]]</h1>
</div><span th:text="#{hello}"></span><br>
<span th:text="#{welcome}"></span><br>
<button type="button" th:text="#{switch-en}"onclick="window.location.href='http://localhost:8080/Scaffold/?lang=en'"></button>
<button type="button" th:text="#{switch-zh}"onclick="window.location.href='http://localhost:8080/Scaffold/?lang=zh'"></button></body>
</html>

在浏览器输入地址,既可以切换语言了

点击切换到中文

再点击切换到英文就可以切换到英文了

好了,国际化就做好了!

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/29959.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

【Azure App Service】列举为App Service集成虚拟网络(VNET)操作时所需要的最小权限

问题描述 作为Azure资源管理人员,对每一种资源操作时,都需要考虑权限设置。否则,会遇见类似如下错误:The client *************** with object id ********-****-****-****-************ does not have authorization to perform action Microsoft.Network/virtualNetworks…

Makdown语法合集

目录页 1. Markdown使用平台1.1. VS Code 平台1.1.1. 安装链接 1.1.2. Markdown使用的插件安装1.1.2.1. Markdown All in On 1.1.2.2. Markdown Preview Enhanced2. 普通文本编写规则2.1. 标题使用样式 2.2. 标题目录的生成 2.3. 自动添加章节号码 2.4. 常规格式 2…

【Java】获取近六个月的年月

以当前月份为标准,向前获取近6个月的年月(year_month)形成列表数据库里面存储的字段类型就是varchar,数据格式就是类似2024-12这样的年月格式。 目标: 以当前月份为标准,向前获取近6个月的年月(year_month)形成列表// 获取近6个月的年月列表List<String> recentM…

全局异常处理器

项目中的全局异常是如何处理的? 全局异常处理逻辑 一般项目开发有两种异常:预期异常(程序员手动抛出)运行时异常在目前的项目中已经提供了全局异常处理器BaseException 基础异常,如果业务中需要手动抛出异常,则需要抛出该异常package com.zzyl.exception;​​import com.…

T2,3,4,5,9动态背包问题

本文主要介绍常见的四种背包问题前言 本文主要介绍常见的四种背包问题,思维导图如下:一、01背包💡 现有 N 件物品和一个最多能承重 M 的背包,第 i 件物品的重量是 wi​,价值是 vi​。在背包能承受的范围内,试问将哪些物品装入背包后可使总价值最大,求这个最大价值。因为…

luogu P4342[IOI1998]Polygon

题目大意 给定一个多边形,对应节点上标记有一个数字,每条边上标记有加(t)或乘(x)表示相邻两个节点可进行的操作,操作后两个节点将合并为一个节点,首先删去一条边(不进行操作),之后在若干次操作后使得该多边形只剩一个节点,且要求所剩节点标记的数最大化,询问最大的…

ES底层原理

1、倒排索引 Elasticsearch 使用一种称为倒排索引的结构,它适用于快速的全文搜索。 有倒排索引,肯定会对应有正向索引:正向索引(forward index) 反向索引(inverted index,实际就是倒排索引)所谓的正向索引,就是搜索引擎会将待搜索的文件都对应一个文件ID,搜索时将这个…