spring boot后端 i18n国际化

本文参考:http://www.itsoku.com/article/286

i18n国际化是指将不同国家的语言存入到不同的配置表中,然后用的时候读取此配置表即可。在spring boot项目中按照下面第:2,3,4,5四个步骤做即可实现i18n

1.新建一个spring boot项目。应为spring boot中application已经实现MessageSource了国际化接口,所以他实现了让我们访问的方法。

 2.先在resources目录下创建资源:注意:名称不是我乱起的,一定要符合规范,例如message_zh_CN.proterties中的:“message”是路径,“_”分隔符 ,“zh”表示中文, “CN”表示中国。

  

 properties里面的内容:

 内容中{0}{1}{2}....是占位符,在调用传参时自动匹配

3.在application.properties或application.yml中配置

spring.messages.basename=static.i18n.message   //表示在static文件下的i18n文件下message开头的配置文件.   

spring.messages.cache-duration=3600                   //设置默认使用缓存大小

spring.messages.encoding=UTF-8                         //指定编码格式

4.LocaleConfig配置类

package com.cjhd.fruit.hall.config;

import java.util.Locale;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

@Configuration
public class LocaleConfig {
    /** 
     *    默认解析器 其中locale表示默认语言,当请求中未包含语种信息,则设置默认语种
     *    当前默认为CHINA,zh_CN
     */
    @Bean
    public SessionLocaleResolver localeResolver() {
        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        localeResolver.setDefaultLocale(Locale.CHINA);
        return localeResolver;
    }

    /** 
       *  默认拦截器 其中lang表示切换语言的参数名 
       *  拦截请求,获取请求参数lang种包含的语种信息并重新注册语种信息
     */
    @Bean
    public WebMvcConfigurer localeInterceptor() {
        return new WebMvcConfigurer() {
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
                localeInterceptor.setParamName("lang");
                registry.addInterceptor(localeInterceptor);
            }
        };
    }
}

5.最后测试

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class HallApplicationTests {
    
    @Autowired
    private MessageSource messageSource;
    
    @Test
    public void testI18n() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String str = messageSource.getMessage("hello.world", new String[] {"啊啊啊啊啊","哦哦哦","嗯嗯嗯"}, Locale.CHINA);
        String str1 = messageSource.getMessage("hello.world", new String[] {"aaaa","ooo","eee"}, Locale.US);
        log.info("i18n测试:{}, {}", str,str1);
    }
}

输出结果:

2021-03-13 22:28:33.555 [main] INFO  com.cjhd.fruit.hall.HallApplicationTests - i18n测试:大家好啊啊啊啊啊哦哦哦嗯嗯嗯,我是中文, hello,myaaaaooo is Englisheee!!!

原文地址:https://www.cnblogs.com/li-yan-long/p/14530824.html