SpringBoot如何集成i18n(多语言)

 更新时间:2024年04月03日 10:29:41   作者:@幻影忍者  
这篇文章主要介绍了SpringBoot如何集成i18n(多语言)问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

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

运行截图

  • 中文

  • 英文

总结

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

相关文章

  • Java多种方法实现合并多个list对象列表

    Java多种方法实现合并多个list对象列表

    Java编程中,合并多个列表对象可以通过Stream API或传统循环方式实现,使用Stream API合并时,利用flatMap方法将嵌套的List展平,再通过collect方法收集成一个新的列表,传统循环则通过创建一个空的ArrayList,并通过遍历每个列表将元素添加进去
    2024-09-09
  • 使用EasyPOI实现多sheet动态列导出

    使用EasyPOI实现多sheet动态列导出

    这篇文章主要为大家详细介绍了如何使用EasyPOI根据指定时间范围创建动态列,以及如何将数据组织成符合要求的格式并导出,感兴趣的可以了解下
    2025-03-03
  • Java字符串拼接效率测试过程解析

    Java字符串拼接效率测试过程解析

    这篇文章主要介绍了Java字符串拼接效率测试过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • springboot中使用自定义两级缓存的方法

    springboot中使用自定义两级缓存的方法

    这篇文章主要介绍了springboot中使用自定义两级缓存的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • 解决window.location.href之后session丢失的问题

    解决window.location.href之后session丢失的问题

    今天小编就为大家分享一篇关于解决window.location.href之后session丢失的问题,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • Java实现合并多个PDF的示例代码

    Java实现合并多个PDF的示例代码

    这篇文章主要介绍了通过Java实现合并多个PDF,并将合并后的新PDF存储到文件夹下,文中的示例代码简洁易懂,感兴趣的可以跟随小编一起试一试
    2022-01-01
  • springboot启动报错Failed to load class [javax.servlet.Filter]的解决

    springboot启动报错Failed to load class [java

    这篇文章主要介绍了springboot启动报错Failed to load class [javax.servlet.Filter]的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2026-03-03
  • 关于druid配置参数的使用详解

    关于druid配置参数的使用详解

    这篇文章主要介绍了关于druid配置参数的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-06-06
  • 彻底搞定堆排序:二叉堆

    彻底搞定堆排序:二叉堆

    二叉堆有两种:最大堆和最小堆。最大堆:父结点的键值总是大于或等于任何一个子节点的键值;最小堆:父结点的键值总是小于或等于任何一个子节点的键值
    2021-07-07
  • Java Reactor反应器模式使用方法详解

    Java Reactor反应器模式使用方法详解

    这篇文章主要介绍了Java Reactor反应器模式使用方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06

最新评论