Springboot 国际化

# 定义国际化资源

  • resources下新建i18n文件夹
  • 新建xx.properties文件
  • 中文:新建xx_zh_CN.properties文件存放对应的中文
  • 英文:新建xx_en_US.properties文件存放对应的英文
    效果是这样的:
    image.png
  • 定义需要国际化的内容

image.png

  • 在application.yml中配置
spring:
  messages:
    # 定义国际化文件的文件地址,读取的原则是顺序读取如果存在同名的则读取第一个
    basename: i18n/login,i18n/errorMessage
  • 定义国际化解析器与拦截器
package com.futao.springmvcdemo.foundation.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

/**
 * 通用bean注册管理中心
 *
 * @author futao
 * Created on 2019-01-15.
 */
@org.springframework.context.annotation.Configuration
public class Configuration {

    /**
     * 国际化,设置默认的语言为中文
     * 将用户的区域信息存在session
     * 也可以存在cookie,由客户端保存   {@link org.springframework.web.servlet.i18n.CookieLocaleResolver}
     *
     * @return
     */
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
        return slr;
    }

    /**
     * 国际化,设置url识别参数
     *
     * @return
     */
    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        lci.setParamName("lang");
        return lci;
    }
}
  • 国际化配置文件会被spring加载,所以可以通过spring容器获取到国际化文件里面的内容
    • 获取spring容器
package com.futao.springmvcdemo.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

/**
 * Spring容器
 *
 * @author futao
 * Created on 2019-01-15.
 */
@Service
public class SpringUtils implements ApplicationContextAware {

    private static ApplicationContext context = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (context == null) {
            context = applicationContext;
        }
    }

    /**
     * 获取容器
     *
     * @return 容器
     */
    public static ApplicationContext getContext() {
        return context;
    }
}
  • 在程序中使用
//通过spring容器读取
SpringUtils.getContext().getMessage(code, null, LocaleContextHolder.getLocale())

//或者
    @Resource
     private MessageSource messageSource;
     return messageSource.getMessage(code, null, LocaleContextHolder.getLocale());
  • 写个工具类

package com.futao.springmvcdemo.service.notbusiness;

import com.futao.springmvcdemo.foundation.LogicException;
import com.futao.springmvcdemo.model.system.ErrorMessage;
import com.futao.springmvcdemo.utils.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.i18n.LocaleContextHolder;

/**
 * 国际化
 *
 * @author futao
 * Created on 2019-01-15.
 */
public class I18nService {

    private static final Logger LOGGER = LoggerFactory.getLogger(I18nService.class);


    /**
     * 获取消息
     * 也可以使用下面的方式:
     * Resource private MessageSource messageSource;
     * return messageSource.getMessage(code, null, LocaleContextHolder.getLocale());
     *
     * @param code k
     * @return v
     */
    public static String getMessage(String code) {
        try {
            return SpringUtils.getContext().getMessage(code, null, LocaleContextHolder.getLocale());
        } catch (NoSuchMessageException e) {
            LOGGER.error("获取国际化资源{}失败" + e.getMessage(), code, e);
            throw LogicException.le(ErrorMessage.I18N_RESOURCE_NOT_FOUND, new String[]{code});
        }
    }
}
  • 使用
I18nService.getMessage("welcome")
  • 前端在接口后面加上local信息?lang=zh_CN或者?lang=en_US即可

源代码: https://github.com/FutaoSmile/springbootFramework
或者: https://gitee.com/FutaoSmile/springboot_framework
欢迎star~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值