当前位置: 首页 > 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");
  }
}

运行截图

中文

英文

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

相关文章:

  • Volumetric Lights 2 HDRP
  • 蓝桥杯 java基础
  • 火狐,要完了!
  • 在AWS Lambda上部署EC2编译的FFmpeg工具——自定义层的方案
  • 情深不必纠缠
  • AIGC之Image2Video(一)| Animate Anyone:从静态图像生成动态视频,可将任意图像角色动画化
  • 区块链的可拓展性研究【04】分片
  • blender 粒子系统 roughness 属性
  • npm ERR! notarget No matching version found for @eslint/eslintrc@^2.1.4.
  • 截取字符串
  • 【Python】Faker库详解:创建测试数据轻而易举
  • Spring boot -- 学习HttpMessageConverter
  • 如何选择合适的运筹优化求解器?
  • Python 精讲 | 奇葩的 is
  • 遥感卫星综述(下载和预处理)(持续更新)
  • Nmap脚本未来的发展趋势
  • 要求CHATGPT高质量回答的艺术:提示工程技术的完整指南—第 17 章:对话提示
  • urllib爬虫 应用实例(三)
  • 【数据挖掘】国科大苏桂平老师数据库新技术课程作业 —— 第三次作业
  • TP5上传图片压缩尺寸
  • 使用 Tailwind CSS 完成导航栏效果
  • docker容器配置MySQL与远程连接设置(纯步骤)
  • 什么是网站劫持
  • LeNet
  • JavaScript 简单理解原型和创建实例时 new 操作符的执行操作
  • 生成对抗网络——研讨会
  • Ubuntu 20.04 安装 mysql8 LTS
  • 蓝桥杯:货物摆放
  • ganache部署智能合约报错VM Exception while processing transaction: invalid opcode
  • 金融银行业更适合申请哪种SSL证书?