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

若依 springBoot 配置国际化

代码如下,配置文件在资源中。Language 取值en-US zh-CN 和前端商量好就行

<dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId></dependency>package com.ruoyi.common.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ModelAndView;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;/*** Created with IntelliJ IDEA.** @Author: yxy* @Date: 2025/11/11/16:42* @Description:*/public class MyI18nInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){final String key = "Language";String language = request.getHeader(key);if (language != null && language.matches("[a-zA-Z]{2}[-][a-zA-Z]{2}")){//如果请求头中包含符合格式的语言信息,例如"en-US"
//            log.info("当前语言={}",language);//根据语言信息创建Locale对象,例如,"en-US"分割为语言"en"和国家/地区"US"Locale locale = new Locale(language.split("-")[0], language.split("-")[1]);
//            log.info("loc" + locale);LocaleContextHolder.setLocale(locale);}else {//如果请求头中没有有效的语言信息,则设置默认语言Locale defaultLocale = Locale.US;
//            log.info("未指定语言,使用默认语言={}", defaultLocale);LocaleContextHolder.setLocale(defaultLocale);}return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView){}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handle, Exception ex){}
}package com.ruoyi.framework.config;import com.ruoyi.common.config.MyI18nInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
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 com.ruoyi.common.constant.Constants;/*** 资源文件配置加载* * @author ruoyi*/
@Configuration
public class I18nConfig implements WebMvcConfigurer
{
//    @Bean
//    public LocaleResolver localeResolver()
//    {
//        SessionLocaleResolver slr = new SessionLocaleResolver();
//        // 默认语言
//        slr.setDefaultLocale(Constants.DEFAULT_LOCALE);
//        return slr;
//    }
//
//    @Bean
//    public LocaleChangeInterceptor localeChangeInterceptor()
//    {
//        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
//        // 参数名
//        lci.setParamName("lang");
//        return lci;
//    }
//
//    @Override
//    public void addInterceptors(InterceptorRegistry registry)
//    {
//        registry.addInterceptor(localeChangeInterceptor());
//    }@Beanpublic ResourceBundleMessageSource messageSource(){ResourceBundleMessageSource source = new ResourceBundleMessageSource();//这里设置自己的文件名source.setBasenames("i18n.message","i18n.exception","i18n.business", "i18n.export");source.setUseCodeAsDefaultMessage(true);source.setDefaultEncoding("UTF-8");return source;}@Overridepublic void addInterceptors(InterceptorRegistry registry){//注册拦截器MyI18nInterceptor myI18nInterceptor = new MyI18nInterceptor();InterceptorRegistration loginRegistry = registry.addInterceptor(myI18nInterceptor);//拦截路径loginRegistry.addPathPatterns("/**");}
}package com.ruoyi.common.utils;import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;//@Component
public class MessageUtils {/*** 根据消息键和参数 获取消息 委托给spring messageSource** @param code 消息键* @param args 参数* @return 获取国际化翻译值*/public static String message(String code, Object... args){MessageSource messageSource = SpringUtils.getBean(MessageSource.class);return messageSource.getMessage(code, args, LocaleContextHolder.getLocale());}}package com.ruoyi.web.controller;import com.ruoyi.common.utils.MessageUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class TestController66 {@GetMapping("/test22")public String test() {// 调用国际化消息String successMsg = MessageUtils.message("sys.common.success");String loginTitle = MessageUtils.message("user.login.title");String welcomeMsg = MessageUtils.message("user.login.welcome", "张三"); // 带参数return successMsg + " | " + loginTitle + " | " + welcomeMsg;}
}
http://www.dtcms.com/a/598611.html

相关文章:

  • 十年经验网站开发公司安阳市商祺网络有限责任公司
  • 网络初识~
  • 织梦怎么做中英文网站做网站只做前端可以用吗
  • C 语言17:位操作符 | ^:从二进制编码到大小端
  • 计算机网站php设计代做平台公司和项目公司的区别
  • flowable04网关和变量
  • 设计旅游网站的主色调sem工具是什么
  • 如何降低交互复杂度,减少用户学习成本
  • 交易类网站做支付宝功能淘宝网站代理怎么做
  • 中国建设银行网站如何注册logo设计公司 南京
  • 麒麟桌面操作系统切换到root用户方法
  • 找企业名录的网站网站的横幅怎么做
  • 工具学习_Vulrule
  • 上海外贸soho网站建设静态网站登陆怎么做
  • 哈希表中桶的概念
  • 网站建设系统设计重庆百度搜索排名优化
  • 中山快速做网站服务盐城网站建设流程
  • LangFlow 中,节点间的数据流动核心是 **“输入-输出端口映射”+“执行链路驱动”**
  • 百度上怎么制作自己的网站中文网页设计案例欣赏
  • 深度学习:python动物识别分类检测系统 Django框架 CNN算法 深度学习 卷积神经网络 TensorFlow (建议收藏)✅
  • 【C++基础与提高】第四章:运算符与表达式——构建程序逻辑的工具
  • 专业提供网站制作双语言网站源码
  • 太原论坛网站开发公司芜湖网站公司
  • 深度学习模型CNN识别恶意软件
  • 模型-详解 Vision Transformer (ViT)
  • 广州专业做网站公司热门网页设计制作代码
  • 温岭 网站制作企业网站自己可以做
  • 虚拟机做网站有用吗关键词seo排名优化推荐
  • Springboot-WebService 服务端发布与客户端调用
  • CentOS7 + VMware 搭建 K3s 集群遇到的网络问题全记录与解决方案