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

SpringBoot集成i18n(多语言)

配置文件

spring:

        messages:
                basename: il8n/messages # 配置国际化资源文件路径
                fallback-to-system-locale: true # 是否使用系统默认的语言环境作为备选项

国际化配置


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;

/**
* 国际化配置
*/
@Configuration
public class I18nlocaleConfig implements WebMvcConfigurer{
/**
* 默认解析器 其中locale表示默认语言
*/
@Bean
public LocaleResolver localeResolver() {
return new MyLocaleResolver();
}

@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {

LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("Accept-Language");
return localeChangeInterceptor;
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}

参数解析

import org.apache.commons.lang3.StringUtils;
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 request) {
// 从 request 域中读取传过来的参数
String l = request.getHeader("Accept-Language");

// 声明 Locale 为默认语言显示
Locale locale = Locale.getDefault();
// 判断传入参数是否为空
if (!StringUtils.isEmpty(language) && StringUtils.contains(language,"_")){
// 将传过来的参数,通过下划线分割,获取到地区(zh)即代码(CN)
String[] split = l.split("_");
// 进行赋值
locale = new Locale(split[0],split[1]);
}
// 返回
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}

ApplicationEvent

import com.zzdy.recharge.il8n.utils.MessageUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.MessageSource;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class ApplicationEvent implements ApplicationListener<ContextRefreshedEvent> {
@Resource
protected MessageSource messageSource;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
MessageUtils.setMessageSource(messageSource);
}
}

MessageUtils

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ResourceBundleMessageSource;


public class MessageUtils extends ResourceBundleMessageSource {
private static MessageSource messageSource;

public static void setMessageSource(MessageSource source){
messageSource=source;
}
public MessageUtils() {
super();
}
/**
* 获取单个国际化翻译值
*/
public static String get(String pvsKey) {
try {
return messageSource.getMessage(pvsKey, null, LocaleContextHolder.getLocale());
} catch (Exception e) {
return pvsKey;
}
}
/**
* 获取单个国际化翻译值
*/
public static String get(String pvsKey,Object ... pvParams) {
try {
return messageSource.getMessage(pvsKey, pvParams, LocaleContextHolder.getLocale());
} catch (Exception e) {
return pvsKey;
}
}
}

运行

import com.zzdy.recharge.il8n.utils.MessageUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("recharge/i18n")
public class GreetingController {

@GetMapping("/greeting")
  public String greeting() {
        return MessageUtils.get("not.null");
  }
}

运行截图

中文

英文

相关文章:

  • 精密制造ERP系统包含哪些模块?精密制造ERP软件是做什么的
  • java使用xstream框架生成xml文件
  • 5.【自动驾驶与机器人中的SLAM技术】2D点云的scan matching算法 和 检测退化场景的思路
  • Unity中Shader编译目标渲染器
  • Java转Go学习之旅 | Go入门(2)
  • Java零基础-if条件语句
  • 雷军:我的程序人生路
  • JS的监听事件
  • 企业微信协议开发,API接口调用
  • 操作系统——进程同步
  • Linux chfn命令教程:如何更改和管理用户的finger信息(附案例详解和注意事项)
  • 【微软技术栈】与其他.NET语言的互操作性 (C++/CLI)
  • 移动端APP自动化测试框架-UiAutomator2基础
  • hive sqlspark 优化
  • GPT3年终总结
  • [NodeJS] 关于Buffer
  • 《Java8实战》-第四章读书笔记(引入流Stream)
  • 《Java编程思想》读书笔记-对象导论
  • angular学习第一篇-----环境搭建
  • Apache Zeppelin在Apache Trafodion上的可视化
  • C++类中的特殊成员函数
  • GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(上)...
  • Javascript基础之Array数组API
  • SQL 难点解决:记录的引用
  • 初识 webpack
  • 如何设计一个比特币钱包服务
  • 一个完整Java Web项目背后的密码
  • 走向全栈之MongoDB的使用
  • NLPIR智能语义技术让大数据挖掘更简单
  • scrapy中间件源码分析及常用中间件大全
  • 小白应该如何快速入门阿里云服务器,新手使用ECS的方法 ...
  • ​一些不规范的GTID使用场景
  • (delphi11最新学习资料) Object Pascal 学习笔记---第7章第3节(封装和窗体)
  • (LeetCode 49)Anagrams
  • (LNMP) How To Install Linux, nginx, MySQL, PHP
  • (vue)页面文件上传获取:action地址
  • (黑马出品_高级篇_01)SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式
  • (数据结构)顺序表的定义
  • (一)插入排序
  • (原創) 未来三学期想要修的课 (日記)
  • *++p:p先自+,然后*p,最终为3 ++*p:先*p,即arr[0]=1,然后再++,最终为2 *p++:值为arr[0],即1,该语句执行完毕后,p指向arr[1]
  • *ST京蓝入股力合节能 着力绿色智慧城市服务
  • .cfg\.dat\.mak(持续补充)
  • .NET 使用配置文件
  • .Net 中Partitioner static与dynamic的性能对比
  • .net遍历html中全部的中文,ASP.NET中遍历页面的所有button控件
  • .NET开发不可不知、不可不用的辅助类(三)(报表导出---终结版)
  • .NET面试题(二)
  • .NET轻量级ORM组件Dapper葵花宝典
  • /*在DataTable中更新、删除数据*/
  • /usr/bin/env: node: No such file or directory
  • []sim300 GPRS数据收发程序
  • [20171113]修改表结构删除列相关问题4.txt
  • [BPU部署教程] 教你搞定YOLOV5部署 (版本: 6.2)
  • [BZOJ1089][SCOI2003]严格n元树(递推+高精度)