SpringBoot如何集成i18n(多语言)

 更新时间:2024年04月03日 10:29:41   作者:@幻影忍者  
这篇文章主要介绍了SpringBoot如何集成i18n(多语言)问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

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");
  }
}

运行截图

  • 中文

  • 英文

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持程序员之家。

相关文章

  • JVM类加载机制详解

    JVM类加载机制详解

    本文主要介绍了JVM类加载机制的相关知识,具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02
  • IDEA生成patch和使用patch的方法实现

    IDEA生成patch和使用patch的方法实现

    比如你本地修复的 bug,需要把增量文件发给客户,很多场景下大家都需要手工整理修改的文件,并整理好目录,这个很麻烦,那有没有简单的技巧呢?本文主要介绍了IDEA生成patch和使用patch的方法实现,感兴趣的可以了解一下
    2023-08-08
  • 浅析Java中的set集合类型及其接口的用法

    浅析Java中的set集合类型及其接口的用法

    Java本身对set集合提供了一个接口,一般的实现类是HastSet和 TreeSet,这里我们先来简要浅析Java中的set集合类型及其接口的用法:
    2016-05-05
  • SpringBoot实现mysql与clickhouse多数据源的项目实践

    SpringBoot实现mysql与clickhouse多数据源的项目实践

    本文主要介绍了SpringBoot实现mysql与clickhouse多数据源的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-11-11
  • 关于Spring MVC同名参数绑定问题的解决方法

    关于Spring MVC同名参数绑定问题的解决方法

    Spring MVC中的参数绑定还是蛮重要的,最近在使用中遇到了同名参数绑定的问题,想着总结分享出来,下面这篇文章主要给大家介绍了关于Spring MVC同名参数绑定问题的解决方法,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-08-08
  • SpringBoot中使用com.alibaba.druid.filter.config.ConfigTools对数据库密码加密的方法

    SpringBoot中使用com.alibaba.druid.filter.config.ConfigTools对数据库

    这篇文章主要介绍了SpringBoot中使用com.alibaba.druid.filter.config.ConfigTools对数据库密码加密的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01
  • 使用java模拟简单的tomcat的方法详解

    使用java模拟简单的tomcat的方法详解

    这篇文章主要为大家详细介绍了java模拟简单的tomcat的方法,使用数据库,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • java实现斗地主发牌功能

    java实现斗地主发牌功能

    这篇文章主要为大家详细介绍了java实现斗地主发牌功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-11-11
  • Redis如何实现分布式锁详解

    Redis如何实现分布式锁详解

    分布式锁一般有三种实现方式:1. 数据库乐观锁;2. 基于Redis的分布式锁;3. 基于ZooKeeper的分布式锁.本篇文章将介绍第二种方式,基于Redis实现分布式锁,文中有非常详细的介绍,需要的朋友可以参考下
    2021-06-06
  • java随机抽取指定范围不重复的数字

    java随机抽取指定范围不重复的数字

    这篇文章主要介绍了java随机抽取指定范围不重复的数字的相关资料,需要的朋友可以参考下
    2016-06-06

最新评论

?


http://www.vxiaotou.com