美文网首页
SpringBoot项目国际化

SpringBoot项目国际化

作者: viankoo | 来源:发表于2019-10-21 15:45 被阅读0次

1.背景

最近,客户提了个需求要求网站国际化,提供中英文两种版本,可切换。so,就琢磨起国际化了,还早就知道有国际化了,但这还是我第一次在正式项目中实战...。本来就想直接用预先写好的资源文件,奈何干不过level比你高的人,还要求对资源文件的动态配置。
下面进入正题吧...

2.利用Properties类实现对properties文件增删改查

别忘了,全局设置文件编码UTF—8。
以IDEA为例,File->settings->File Encodings


2.1读取properties文件

Properties pp = new Properties();
        InputStream stream = this.getClass().getResourceAsStream(path); //path:相对路径  不知道的(Ctrl+Alt+Shirt+C)
        BufferedReader bf = new BufferedReader(new InputStreamReader(stream)); //转换成字节流,防止中文乱码
        pp.load(bf); //加载Properties文件
        Enumeration<?> en = pp.propertyNames();
        while (en.hasMoreElements()) {
            String key = (String) en.nextElement();
            String value = pp.getProperty(key);
        }

2.2更新properties文件

Properties pp = new Properties();
        InputStream stream = this.getClass().getResourceAsStream(path);
        pp.load(new InputStreamReader(stream,"utf-8")); //指定编码

        pp.setProperty(key,val);  //设置key,value   可用于添加,更新

        String resource = getClass().getResource(path).getPath();
        String decode = URLDecoder.decode(resource, "utf-8");  //decode 防止文件路径中文乱码,导致找不到文件
        FileOutputStream outputStream = new FileOutputStream(decode);
        OutputStreamWriter writer = new OutputStreamWriter(outputStream, "utf-8");
        pp.store(writer,"Update,key="+key+";value="+val); //comments: properties文件的注释

2.3删除properties文件中的k-v

Properties pp = new Properties();
        InputStream stream = this.getClass().getResourceAsStream(path);
        pp.load(new InputStreamReader(stream,"utf-8"));
        pp.remove(key);
        String resource = getClass().getResource(path).getPath();
        String decode = URLDecoder.decode(resource, "utf-8");
        FileOutputStream outputStream = new FileOutputStream(decode);
        OutputStreamWriter writer = new OutputStreamWriter(outputStream, "utf-8");
        pp.store(writer,"Delete"+key);

3.SpringBoot利用国际化

3.1资源文件的创建

在resources下创建i18n文件夹,创建Resource Bundle类型文件



命名messages,加入中英文类型文件。
完成之后会生成下面三个文件:


3.2自定义i18n国际化配置

后台的国际化,主要就是返回给前台的提示。
前台国际化,由js加载资源文件获取。

3.2.1方法一:在application.yml配置文件中
spring: 
  messages:
    basename: i18n/messages
    encoding: UTF-8

反正我没找到basename属性,也没研究,用下面的方法妥妥的。

3.2.2方法二:Bean的形式配置
@Configuration
public class MessageSourceConfig {

    @Bean(name = "messageSource")
    public ResourceBundleMessageSource getMessageSource() throws Exception {
        ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
        resourceBundleMessageSource.setDefaultEncoding("UTF-8");
        resourceBundleMessageSource.setBasenames("i18n/messages");
        return resourceBundleMessageSource;
    }

}

注意:指定Bean name。
至此,国际化配置已经完成啦!

3.3切换语言

public class MessageUtils {

    /**
     * 根据消息键和参数 获取消息 委托给spring messageSource
     *
     * @param code 消息键
     * @param args 参数
     * @return
     */
    public static String message(String code, Object... args) {
        MessageSource messageSource = SpringUtils.getBean(MessageSource.class);
        return messageSource.getMessage(code, args, Locale.CHINESE);
    }

}

切换语言,就是切换地区。指定Locale的地区就行了。Locale常量提供了很多地区环境可供选择。

相关文章

网友评论

      本文标题:SpringBoot项目国际化

      本文链接:https://www.haomeiwen.com/subject/plidyctx.html