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读取资源文件就比较方便,判断是中英文,返回对应的内容。

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

天狼1222

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

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

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

打赏作者

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

抵扣说明:

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

余额充值