SpringBoot 实现国际化

专栏文章

1.springboot前后端分离项目集成Security框架和JWT完成用户校验

2.SpringBoot 前后端分离 集成图形验证码

3.SpringBoot 跨域问题之CorsFilter

前言

国际化(Internationalization 简称 I18n,其中“I”和“n”分别为首末字符,18 则为中间的字符数)是指软件开发时应该具备支持多种语言和地区的功能。换句话说就是,开发的软件需要能同时应对不同国家和地区的用户访问,并根据用户地区和语言习惯,提供相应的、符合用具阅读习惯的页面和数据,例如,为中国用户提供汉语界面显示,为美国用户提供提供英语界面显示。

1. 编写国际化资源文件

1.1. 在resources目录下创建i18n目录

1.2. 在i18n下创建默认的资源文件messages.properties

1.3. 创建中文资源文件:messages_zh.properties

1.4. 创建英文资源文件:messages_en.properties

1.5. 编写配置的语句

一共写三版:默认显示、中文显示、英文显示,这里就默认显示中文。

2. 实现WebMvcConfigurer配置

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.LocaleResolver;
/**
 * @author 方磊
 */
public class WebMvcConfiguration implements WebMvcConfigurer {

    /**
     * 将自定义的区域信息解析器以组件的形式添加到容器中
     * @return
     */
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocalResolver();
    }
    
    /**
     * 系统国际化文件配置
     * @return MessageSource
     */
    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:i18n/messages");
        return messageSource;
    }
}

3. spring工具类 方便在非spring管理环境中获取bean

import com.fang.ordering.common.utils.StringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * spring工具类 方便在非spring管理环境中获取bean
 *
 * @author fanglei
 */
@Component
public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware
{
    /** Spring应用上下文环境 */
    private static ConfigurableListableBeanFactory beanFactory;

    private static ApplicationContext applicationContext;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
    {
        SpringUtils.beanFactory = beanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
    {
        SpringUtils.applicationContext = applicationContext;
    }

    /**
     * 获取对象
     *
     * @param name
     * @return Object 一个以所给名字注册的bean的实例
     * @throws BeansException
     *
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException
    {
        return (T) beanFactory.getBean(name);
    }

    /**
     * 获取类型为requiredType的对象
     *
     * @param clz
     * @return
     * @throws BeansException
     *
     */
    public static <T> T getBean(Class<T> clz) throws BeansException
    {
        T result = (T) beanFactory.getBean(clz);
        return result;
    }

    /**
     * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
     *
     * @param name
     * @return boolean
     */
    public static boolean containsBean(String name)
    {
        return beanFactory.containsBean(name);
    }

    /**
     * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
     *
     * @param name
     * @return boolean
     * @throws NoSuchBeanDefinitionException
     *
     */
    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException
    {
        return beanFactory.isSingleton(name);
    }

    /**
     * @param name
     * @return Class 注册对象的类型
     * @throws NoSuchBeanDefinitionException
     *
     */
    public static Class<?> getType(String name) throws NoSuchBeanDefinitionException
    {
        return beanFactory.getType(name);
    }

    /**
     * 如果给定的bean名字在bean定义中有别名,则返回这些别名
     *
     * @param name
     * @return
     * @throws NoSuchBeanDefinitionException
     *
     */
    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException
    {
        return beanFactory.getAliases(name);
    }

    /**
     * 获取aop代理对象
     *
     * @param invoker
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getAopProxy(T invoker)
    {
        return (T) AopContext.currentProxy();
    }

    /**
     * 获取当前的环境配置,无配置返回null
     *
     * @return 当前的环境配置
     */
    public static String[] getActiveProfiles()
    {
        return applicationContext.getEnvironment().getActiveProfiles();
    }

    /**
     * 获取当前的环境配置,当有多个环境配置时,只获取第一个
     *
     * @return 当前的环境配置
     */
    public static String getActiveProfile()
    {
        final String[] activeProfiles = getActiveProfiles();
        return StringUtils.isNotEmpty(activeProfiles) ? activeProfiles[0] : null;
    }

    /**
     * 获取配置文件中的值
     *
     * @param key 配置文件的key
     * @return 当前的配置文件的值
     *
     */
    public static String getRequiredProperty(String key)
    {
        return applicationContext.getEnvironment().getRequiredProperty(key);
    }
}

4. 配置自定义区域信息解析器

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * 自定义区域信息解析器
 * @author 方磊
 */
public class MyLocalResolver implements LocaleResolver {
    
    @Override
   public Locale resolveLocale(HttpServletRequest request) {
        // 获取请求中的语言参数来链接
        String language = request.getParameter("language");
        Locale locale = Locale.getDefault();
        if (!StringUtils.isEmpty(language)) {
            String[] strings = language.split("_");
            locale = new Locale(strings[0], strings[1]);
        }
        return locale;
    }
    
    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
    }
}

5. 配置MessageUtils工具类

import com.fang.ordering.common.utils.spring.SpringUtils;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

/**
 * 获取i18n资源文件
 *
 * @author fanglei
 */
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());
    }
}

6. 使用工具类输出

7. 总结

实现:LocalResolver 、WebMvcConfigurer

  • LocalResolver :默认的解析器,用于设置当前会话的默认国际化语言。
  • WebMvcConfigurer:拦截器,用来对请求的语言参数进行获取,采用默认的LocaleChangeInterceptor 作为拦截器来指定切换国际化语言的参数名。
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
# i18n-demo 基于i18n利用springboot实现后台国际化demo 引入外部依赖:commons-lang3、lombok ## 背景 项目做到尾声,临时需要添加国际化内容,开始只是前台展示的页面添加了国际化支持,后来发现一些后台提示很不友好,遂也选择进行国际化处理。 ## 实现 **Springboot国际化支持的很好,我们就用自带的i18n实现国际化就行。** 1. application.properties配置文件指定messages位置 ``` spring.messages.basename=i18n.message spring.messages.encoding=UTF-8 ``` 通过查看源码我们可以知道,如果不进行配置,默认读取resource下面messages文件,源码如下: ![springboot默认国际化配置读取位置](https://img-blog.csdnimg.cn/20200511183958366.png) 2. 在resource文件夹下面创建我们配置的basename,目录结构如下图: ![三个配置文件](https://img-blog.csdnimg.cn/20200512093846448.png) **其中 message.properties必须要有,其他的配置文件命名格式为:message_语言_国家.properties**,其中语言和国家格式可以查看 java.util.Locale 类中的说明。 3. 自定义重写 LocaleResolver 类的 resolveLocale 方法,代码如下: ``` public class MyLocaleResolverConfig implements LocaleResolver { private static final String PATH_PARAMETER = "lang"; private static final String PATH_PARAMETER_SPLIT = "_"; @Override public Locale resolveLocale(HttpServletRequest request) { String lang = request.getHeader(PATH_PARAMETER); Locale locale = request.getLocale(); if (!StringUtils.isEmpty(lang)) { String[] split = lang.split(PATH_PARAMETER_SPLIT); locale = new Locale(split[0], split[1]); } return locale; } @Override public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { } } ``` resolveLocale 方法作用就是我们的程序从哪获取指定的语言信息,我这里采用的是从header中获取指定语言,如果没有则采用浏览器 默认的。 还可以从 Session 或 Cookie 中获取。**这里因为我们采取header中获取,所以前端发的请求中 Request Headers 中要有 key 为 lang,value 为 en_US 或 zh_CN 的header。** 4. 将我们自定义的 MyLocaleResolverConfig 作为 Bean 注册进系统中: ``` @Bean public LocaleResolver localeResolver() { return new MyLocale ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搬砖小方

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值