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

LocaleContextResolver实现多语言切换-笔记

1. LocaleContextResolver功能简介

org.springframework.web.servlet.LocaleContextResolver是 Spring MVC 中用于解析和管理用户 Locale(语言环境) 的核心接口。

//LocaleContextResolver 接口定义
public interface LocaleContextResolver extends LocaleResolver {LocaleContext resolveLocaleContext(HttpServletRequest var1);void setLocaleContext(HttpServletRequest var1, @Nullable HttpServletResponse var2, @Nullable LocaleContext var3);
}//LocaleResolver 定义
public interface LocaleResolver {Locale resolveLocale(HttpServletRequest var1);void setLocale(HttpServletRequest var1, @Nullable HttpServletResponse var2, @Nullable Locale var3);
}

它的主要作用是:

  1. 解析请求中的 Locale 信息
    从 HTTP 请求中提取用户指定的语言/地区信息(如 URL 参数、请求头、Cookie 等)。

  2. 维护 Locale 上下文
    将解析后的 Locale 信息存储在当前线程的上下文中(LocaleContext),确保整个请求链(如 Controller、视图渲染)使用一致的 Locale。

  3. 支持多级存储策略
    提供多种实现类,支持基于请求头、会话(Session)、Cookie 或固定值的 Locale 解析。

常见 LocaleContextResolver 实现类:

实现类特点
AcceptHeaderLocaleResolver从 HTTP 请求头 Accept-Language 解析 Locale。
SessionLocaleResolver

从Session中解析Locale;将 Locale 存储在用户 Session 中,适合需要持久化语言偏好的场景。

CookieLocaleResolver从Cookie中解析Locale;将 Locale 存储在 Cookie 中,适合无状态或分布式系统。
FixedLocaleResolver固定使用一个 Locale(如开发环境调试)。

典型使用场景

场景描述
国际化支持根据用户选择的语言显示不同的界面内容(如中英文切换)。
动态语言切换用户通过 URL 参数(如 ?lang=zh-CN)或按钮切换语言。
自动语言检测通过浏览器的 Accept-Language 请求头自动识别用户语言偏好。
持久化语言偏好使用 Cookie 或 Session 保存用户的语言选择,避免重复传递参数。

2. 用法演示

下面用一个例子演示使用 LocaleContextResolver 实现多语言切换功能,根据当前语言环境返回国际化消息,动态适配用户的语言偏好。

2.1 完整代码

step1. 配置 LocaleContextResolver 和拦截器

import java.util.Locale;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
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;@Configuration
public class WebLocalConfig implements WebMvcConfigurer {// 1. 配置 MessageSource(用于加载国际化资源文件)@Beanpublic MessageSource messageSource() {ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();messageSource.setBasename("classpath:CustomMessages"); // 指定国际化资源文件的前缀messageSource.setDefaultEncoding("UTF-8"); // 设置编码return messageSource;}// 2. 配置 LocaleResolver(用于解析语言环境)// 这里使用 LocaleContextResolver 的实现类 SessionLocaleResolver@Beanpublic LocaleResolver localeResolver() {// 使用 Session 存储 Locale,避免每次请求传递参数SessionLocaleResolver resolver = new SessionLocaleResolver();//设置默认语言为中文(zh_CN)resolver.setDefaultLocale(Locale.CHINA);return resolver;}// 3. 配置 LocaleChangeInterceptor(用于通过 URL 参数切换语言)@Beanpublic LocaleChangeInterceptor localeChangeInterceptor() {// 通过 URL 参数(如 xxx?lang=zh-CN)切换语言LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();interceptor.setParamName("lang");// 设置参数名return interceptor;}//4. 将 LocaleChangeInterceptor 添加到拦截器中,使其生效@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(localeChangeInterceptor());}
}

step2.根据Locale解析对应的国际化语言

Service 层使用 LocaleContextHolder 获取上下文中的 Locale,并根据Locale解析对应的国际化语言

import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;@Service
public class MessageService {@Autowiredprivate MessageSource messageSource;public String getLocalizedMessage(String code) {// 从 LocaleContext 中获取当前线程的 LocaleLocale currentLocale = LocaleContextHolder.getLocale();// 打印当前语言环境System.out.println("Current Locale: " + currentLocale);System.out.println("Trying to get message for code: " + code); // 打印 key//根据 Locale 从对应的资源文件中加载code的值return messageSource.getMessage(code, null, currentLocale);}
}

step3. 新建测试Controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class GreetingController {@Autowiredprivate MessageService messageService;@GetMapping("GreetingController/greet")public String greet() {// 获取国际化消息return messageService.getLocalizedMessage("greeting.message");}}

 step4. 配置国际化资源文件

  • CustomMessages_zh_CN.properties(中文)
    • greeting.message=你好,世界

  • CustomMessages_en_US.properties (英文)
    • greeting.message=hello,world

资源位置:

  • src/main/resources/
    ├── CustomMessages_en_US.properties        // 英文
    ├── CustomMessages_zh_CN.properties        // 中文
    

step5. 测试

case1. 不设定 lang参数,默认为中文。

  • 浏览器访问:http://127.0.0.1:8080/GreetingController/greet,输入如下:

case2. lang参数设为 zh_CN,输出结果为中文

  • 浏览器访问:http://127.0.0.1:8080/GreetingController/greet?lang=zh_CN,输入如下:

case3. lang参数设为 en_US,输出结果为英文

  • 浏览器访问:http://127.0.0.1:8080/GreetingController/greet?lang=en_US,输入如下:

2.2 代码流程简介

上述流程的核心原理简述:

  • LocaleContextResolver 负责从请求中解析出 Locale(如 URL 参数、请求头、Session、Cookie 等)。这里用的实现类为SessionLocaleResolver, 即WebLocalConfig#localeResolver方法中配置。
  • 解析出的 Locale 会通过 LocaleContextHolder.setLocale(...) 设置到当前线程的上下文中。
  • 后续的组件(这里对应MessageService,也能是Controller或视图)可以直接从 LocaleContextHolder.getLocale() 获取当前的 Locale,实现上下文一致性。

通过 LocaleContextResolver,Spring MVC 提供了灵活的国际化支持机制。开发者可以根据业务需求选择不同的 Resolver 实现,结合 LocaleChangeInterceptor 实现语言切换,使用LocaleContextHolder维护整个请求生命周期中 Locale 上下文的一致性,从而实现灵活的国际化支持。

http://www.xdnf.cn/news/6740.html

相关文章:

  • c++ 类的语法3
  • 八股文--JUC(2)
  • 物联网技术在银行安全用电系统中的应用与实践研究
  • 【C++】15.并发支持库
  • C语言水仙花数
  • 通过python安装小智语音服务器端
  • 从实模式到保护模式
  • 基于LTE帧结构参数的F-OFDM仿真
  • 刻录光盘--和炸铁路,tarjan
  • MGX:多智能体管理开发流程
  • 动态范围调整(SEF算法实现)
  • 在多个SpringBoot程序中./相对路径下隐患、文件覆盖问题
  • CSS- 2.1 实战之图文混排、表格、表单、学校官网一级导航栏
  • 基于51单片机和8X8点阵屏、矩阵按键的记忆类小游戏
  • C语言文件操作
  • 第八章 模板项目生成
  • Nextjs首屏加载速度性能从80分优化到98分
  • Qt控件:交互控件
  • PT2020 20触控I2C输出IC
  • 时频分析的应用—外部信号的显影和定点清除
  • 第三部分:内容安全(第十六章:网络型攻击防范技术、第十七章:反病毒、第十八章:入侵检测/防御系统(IDS/IPS))
  • 第J1周:ResNet-50算法实战与解析
  • C语言图案代码大全:从基础到高级
  • 院校机试刷题第四天:1911反转公约数、1702十六进制不进位加法
  • PR-2021
  • ADC深入——SNR、SFDR、ENOB等概念
  • 深入浅出拆分学习,图神经网络拆分学习,混合联邦学习
  • 网络攻防模拟:城市安全 “数字预演”
  • 14 C 语言浮点类型详解:类型精度、表示形式、字面量后缀、格式化输出、容差判断、存储机制
  • AlphaEvolve:基于Gemini的算法发现与优化综合报告