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!!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM