MessageSourceUtil读取资源文件

        在处理返回值提示的时候,需要根据local返回中文或者英文,因此要使用到国际化内容

操作

1,新建资源文件

在src/main/resources源文件夹下创建一个i18n的子目录, 然后创建中文和英文对应properties文件,然后输入自己的提示信息

        输入内容:

result.success=执行成功
result.failure=执行失败
flow.toDo=有“{0}”待办“{1}”正等待您的审批!

flow.toDo=有“{0}”待办“{1}”正等待您的审批! 如果有其它参数用{0} 这种进行标记。

如果中文里面显示的值是乱码,打开setting 搜索 encoding

选择右下角的 Transparent native-to-ascii conversion 

然后点击OK

2,MessageSourceUtils

import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.i18n.LocaleContextHolder;

import java.util.Locale;


public class MessageSourceUtils {

    public static MessageSource getMessageSource() {
        try {
            return (MessageSource) SpringUtils.getBean("messageSource");
        } catch (Exception e) {
            return SpringUtil.getBean(MessageSource.class);
        }
    }

    public static String getMessage(String code) {
        Locale locale = LocaleContextHolder.getLocale();
        return getMessageSource().getMessage(code, null, locale);
    }

    public static String getMessage(String code, Object[] args) {
        Locale locale = LocaleContextHolder.getLocale();
        return getMessageSource().getMessage(code, args, locale);
    }

    public static String getMessage(String code, String module) {
        Locale locale = LocaleContextHolder.getLocale();
        Locale myLocale = new Locale(module + "_" + locale.getLanguage(), locale.getCountry());
        return getMessageSource().getMessage(code, null, myLocale);
    }

    public static String getMessage(String code, Object[] args, String module) {
        Locale locale = LocaleContextHolder.getLocale();
        Locale myLocale = new Locale(module + "_" + locale.getLanguage(), locale.getCountry());
        return getMessageSource().getMessage(code, args, myLocale);
    }

    public static String getMessage(String code, Object[] args, Locale locale, String module) {
        Locale myLocale = new Locale(module + "_" + locale.getLanguage(), locale.getCountry());
        return getMessageSource().getMessage(code, args, myLocale);
    }

    public static String getMessage(String code, Locale locale, String module) {
        Locale myLocale = new Locale(module + "_" + locale.getLanguage(), locale.getCountry());
        return getMessageSource().getMessage(code, null, myLocale);
    }

    public String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
        return getMessageSource().getMessage(code, args, defaultMessage, locale);
    }

    public static String getMessage(String code, Object[] args, Locale locale) {
        return getMessageSource().getMessage(code, args, locale);
    }

    public static String getMessage(MessageSourceResolvable resolvable, Locale locale) {
        return getMessageSource().getMessage(resolvable, locale);
    }

}

3,本地化读取

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;

import java.util.Locale;

@Configuration
public class LocaleConfiguration {

    @Bean(name = "messageSource")
    public MessageSource initMessageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:i18n/messages");
        return messageSource;
    }

    @Bean
    public LocaleResolver localeResolver() {
        CookieLocaleResolver localeResolver = new CookieLocaleResolver();
        localeResolver.setCookieName("LOCALE");
        localeResolver.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
        localeResolver.setLanguageTagCompliant(true);
        localeResolver.setCookieMaxAge(360 * 24 * 60 * 60);
        return localeResolver;
    }
}

4,判断中英文

public class LocaleUtils {

    public static String getLocale() {
        Locale locale = LocaleContextHolder.getLocale();
        String lang = locale.getLanguage();
        if (!StringUtil.isEmpty(locale.getCountry())) {
            lang = locale.getLanguage() + "_" + locale.getCountry();
        }
        return lang;
    }

    public static String getLanguage() {
        Locale locale = LocaleContextHolder.getLocale();
        return locale.getLanguage();
    }

    public static String getCountry() {
        Locale locale = LocaleContextHolder.getLocale();
        return locale.getCountry();
    }

    public static boolean isChinese() {
        String lang = getLanguage();
        return lang.toLowerCase().contains("cn") || lang.toLowerCase().contains("zh");
    }

    public static boolean isEnglish() {
        String lang = getLanguage();
        return lang.toLowerCase().contains("en") || lang.toLowerCase().contains("us");
    }

}

5,使用:

批量插入的时候,返回插入成功数和失败数:

Integer total = (Integer) result.get("total");
List<Long> fails = (List<Long>) result.get("fails");
String message = fails.isEmpty()
    ? MessageSourceUtil.getMessage("RESULT.BATCH_APPROVE_SUCCESS", new Object[]{total})
    : MessageSourceUtil.getMessage("RESULT.BATCH_APPROVE_ERROR",
    new Object[]{total - fails.size(), fails.size()});

6,注意:

        因为本地化读取使用了 @Configuration (相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文))这个,因此在打包的时候,要注意其是否唯一。本地化包或者引用包,打包的时候,要进行移除:

    task archivesJar(type: Jar) {
        archiveName = "${project.name}-${project.version}.jar"
        from('build/classes/java/main', 'build/resources/main')
        exclude('**/**Application.class', '**/application**','**/bootstrap**',
                '**/SwaggerConfig**.class', '**/WebMvcConfiguration**.class',
                '**/LocaleConfiguration**.class')
    }

总结

        使用MessageSourceUtil读取资源文件就比较方便,判断是中英文,返回对应的内容。