Java 杂谈Springboot整合SpringBoot极简教程 · Spring Boot

SpringBoot国际化

2019-01-23  本文已影响3人  垃圾简书_吃枣药丸

# 定义国际化资源

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

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

上一篇 下一篇

猜你喜欢

热点阅读