当前位置: 首页 > news >正文

Springboot3国际化

国际化实现步骤

Spring Boot 3 提供了强大的国际化支持,使得应用程序可以根据用户的语言和区域偏好适配不同的语言和地区需求。

  1. 添加国际化资源文件: 国际化资源文件通常放在 src/main/resources 目录下,并按照不同的语言和地区命名,例如:

    • messages.properties:默认语言(如英文)
    • messages_zh_CN.properties:中文简体
    • messages_fr.properties:法语
  2. 配置 MessageSource Bean: 可以通过在 application.propertiesapplication.yml 中进行简单配置来加载国际化资源文件:

    spring:messages:basename: messagesencoding: UTF-8

    或者在配置类中定义 MessageSource Bean:

    @Configuration
    public class MessageConfig {@Beanpublic MessageSource messageSource() {ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();messageSource.setBasename("classpath:messages");messageSource.setDefaultEncoding("UTF-8");return messageSource;}
    }
  3. 使用国际化资源: 在代码中可以通过 MessageSource 来获取国际化消息。例如,在控制器中根据请求参数确定语言环境并获取对应的消息。

  4. 模板中的国际化: 如果使用 Thymeleaf 作为模板引擎,可以在模板中直接使用国际化消息。需要确保在 application.properties 中启用了国际化支持,并且在模板中使用 #{} 表达式引用消息键。

  5. 自动检测客户端语言: Spring Boot 提供了 LocaleResolver 来自动检测和设置客户端的语言环境。可以使用 AcceptHeaderLocaleResolver 或自定义的 LocaleResolver

  6. 缓存本地语言设置: 若要将本地语言设置缓存,可以在自己的配置类中增加 LocaleChangeInterceptor 拦截器和实现 LocaleResolver 方法。比如使用 CookieLocaleResolver 将语言设置存储在 Cookie 中。

  7. 与 Spring Security 结合: 在使用 Spring Security 时,可以通过在资源文件中添加相应的消息并在 Spring Security 配置中使用这些消息来实现登录页面和错误信息的多语言支持。

示例

配置国际化yaml

spring:messages:encoding: UTF-8basename: i18n/messagesprofiles:active: zh_CN
#-Dspring.profiles.active=en_US

英文

server:port: 8000
spring:jackson:date-format: MM-dd-yyyy

中文

spring:jackson:date-format: yyyy-MM-dd
server:port: 8000

国际化配置

package com.cokerlk.language;import com.cokerlk.language.service.EnUSProductService;
import com.cokerlk.language.service.IProductService;
import com.cokerlk.language.service.ZhCNProductService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;@Configuration
@Data
public class I18NConfiguration {@Value("${spring.profiles.active}")private String locale;@Profile("zh_CN")@Beanpublic IProductService zhCNBussService(){return new ZhCNProductService();}@Profile("en_US")@Beanpublic IProductService enUSBussService(){return new EnUSProductService();}
}

产品接口

package com.cokerlk.language.service;import java.util.Map;public interface IProductService {Map<String,String> getProduct();
}

中文产品

package com.cokerlk.language.service;import com.cokerlk.language.I18NConfiguration;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;@Slf4j
public class ZhCNProductService implements IProductService {@ResourceI18NConfiguration i18NConfiguration;@ResourceMessageSource messageSource;@Overridepublic Map getProduct() {log.info("中文");Map result = new HashMap();result.put("create-date", new Date());result.put("text", messageSource.getMessage("product_name", null, Locale.of(i18NConfiguration.getLocale())));return result;}
}

英文产品

package com.cokerlk.language.service;import com.cokerlk.language.I18NConfiguration;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;@Slf4j
public class EnUSProductService implements IProductService {@ResourceI18NConfiguration i18NConfiguration;@ResourceMessageSource messageSource;@Overridepublic Map<String,String> getProduct() {log.info("英文");Map result = new HashMap();result.put("create-date", new Date());result.put("text", messageSource.getMessage("product_name", null, Locale.of(i18NConfiguration.getLocale())));return result;}
}

message配置

#messages.properties
product_name=huawei mate 70
#messages_en_US.properties
product_name=Hua wei mate 70
#messages_zh_CN.properties
product_name=华为mate70

测试结果

http://www.lryc.cn/news/510123.html

相关文章:

  • 阿尔萨斯(JVisualVM)JVM监控工具
  • 框架专题:反射
  • 【Go】context标准库
  • LLMs之o3:《Deliberative Alignment: Reasoning Enables Safer Language Models》翻译与解读
  • git设置项目远程仓库指向github的一个仓库
  • 实战演练JDK的模块化机制
  • jdk17+springboot3项目加密部署
  • rm -rf 删除/下bin lib lib64 sbin软链接系统恢复
  • 并发与竞争
  • Java后端开发 ”Bug“ 分享——订单与优惠卷
  • Linux系统之tee命令的基本使用
  • idea 8年使用整理
  • 多个微服务 Mybatis 过程中出现了Invalid bound statement (not found)的特殊问题
  • k8s,service如何找到容器
  • 观察者模式和发布-订阅模式有什么异同?它们在哪些情况下会被使用?
  • docker compose deploy fate cluster
  • 字节跳动Java开发面试题及参考答案(数据结构算法-手撕面试题)
  • 网工日记:FTP工作模式
  • unity使用代码在动画片段中添加event
  • 嵌入式轻量级开源操作系统:HeliOS的使用
  • 解决VMware的ubuntu22虚拟机没有网络
  • 金属衬底介质片对平面波的反射-问题的解析求解和FEM求解
  • 2023 年 9 月青少年软编等考 C 语言四级真题解析
  • C++的内存四区
  • Java爬虫技术:按关键字搜索VIP商品详情
  • C++ —— 模板类与函数
  • 【软考高级】系统架构设计师复习笔记-精华版
  • 免费 IP 归属地接口
  • AIA - IMSIC之二(附IMSIC处理流程图)
  • 数据处理之数据规约