Java避坑指南|SpringBoot国际化踩坑日记

1,445 阅读3分钟

这是我参与更文挑战的第19天,活动详情查看: 更文挑战

前言:

国际化信息也称为本地化信息 。 Java 通过 java.util.Locale 类来表示本地化对象,它通过 “语言类型” 和 “国家/地区” 来创建一个确定的本地化对象 。举个例子吧,比如在发送一个具体的请求的时候,在header中设置一个键值对:"Accept-Language":"zh",通过Accept-Language对应值,服务器就可以决定使用哪一个区域的语言,找到相应的资源文件,格式化处理,然后返回给客户端。

最近在做国际化的时候遇到一个bug是无法加载到根目录下的自定义的properties文件如下图:

图片.png 原因是没有加载到根目录下的文件 ,我们来看一下我的idea文件目录:

图片.png

代码如下:

    /**
     * Spring 方式
     * @param args
     */
    private static ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();

    static {
        //指定国家化资源文件路径
        messageSource.setBasename("il8n/messages");
        //指定将用来加载对应资源文件时使用的编码,默认为空,表示将使用默认的编码进行获取。
        messageSource.setDefaultEncoding("UTF-8");
    }

    public static String getChineseValueByKey(String key){

        return messageSource.getMessage(key, null, Locale.CHINA);
    }

    public static String getDeafultValueByKey(String key){

        return messageSource.getMessage(key, null, null);
    }

    public static String getEnglishValueByKey(String key){

        return messageSource.getMessage(key, null, Locale.US);
    }

    public static String getValueAndPlaceholder(String key){

        return messageSource.getMessage(key, new Object[]{"安全"}, null);
    }

仔细检查了几遍代码和配置没有错误,最后查看编译后的target文件下的classes目录下il8n目录并没有编译到更目录下,然后开始了漫长的排查过程


最终发现问题尽然在这里!!!

图片.png pom文件打包配置了只编译resources下的application.properties文件!!!

解决方法:

注释掉<resourses>下所有配置项

图片.png

至此,SpringBoot国际化完成!!!

文件内容替换自己的就可以

图片.png

代码实现:

package com.wechat.wechatservice.utils;

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContext;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
/**
 * @BelongsProject: wechat_java
 * @BelongsPackage: com.wechat.wechatservice.utils
 * @Author: admin
 * @CreateTime: 2021-01-05 11:14
 * @Description: 获取国际化配置文件
 */
@Component
public class ResourceUtils {

/*    public static String getEnglishValueByKey(String key){

        Locale locale = new Locale("en", "US");
        //使用指定的英文Locale
        ResourceBundle mySource = ResourceBundle.getBundle("is8n/message", locale);
        return mySource.getString(key);
    }

    public static String getChineseValueByKey(String key){

        Locale locale = Locale.getDefault();
        //使用指定的中文Locale
        ResourceBundle mySource = ResourceBundle.getBundle("is8n/messages", locale);
        return mySource.getString(key);
    }

    public static String getDeafultValueByKey(String key){

        //使用默认的Locale
        ResourceBundle mySource = ResourceBundle.getBundle("is8n/message");
        return mySource.getString(key);
    }

    public static String getValueAndPlaceholder(String key){

        //使用默认的Locale
        ResourceBundle mySource = ResourceBundle.getBundle("is8n/message");

        String beforeValue = mySource.getString(key);

        //填充国家化文件中的占位符
        String afterValue = MessageFormat.format(beforeValue, "安全");
        return afterValue;
    }*/

    /**
     * Spring 方式
     * @param args
     */
    private static ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();

    static {
        //指定国家化资源文件路径
        messageSource.setBasename("il8n/messages");
        //指定将用来加载对应资源文件时使用的编码,默认为空,表示将使用默认的编码进行获取。
        messageSource.setDefaultEncoding("UTF-8");
    }

    public static String getChineseValueByKey(String key){

        return messageSource.getMessage(key, null, Locale.CHINA);
    }

    public static String getDeafultValueByKey(String key){

        return messageSource.getMessage(key, null, null);
    }

    public static String getEnglishValueByKey(String key){

        return messageSource.getMessage(key, null, Locale.US);
    }

    public static String getValueAndPlaceholder(String key){

        return messageSource.getMessage(key, new Object[]{"安全"}, null);
    }



    public static void main(String[] args) {
       //System.out.println(get("com.website.operation"));
        System.out.println(ResourceUtils.getChineseValueByKey("com.website.operation"));

        System.out.println(ResourceUtils.getDeafultValueByKey("com.website.operation"));

        System.out.println(ResourceUtils.getEnglishValueByKey("com.website.operation"));

        System.out.println(ResourceUtils.getValueAndPlaceholder("com.website.writeLog"));
    }

}

ok!SpringBoot国际化完成,大家也可以根据自己的需求去做优化,希望可以对大家有帮助,有不对的地方希望大家可以提出来的,共同成长;

整洁成就卓越代码,细节之中只有天地