springboot集成i18n,國際語言標準實體返回

配置步驟:

新建標準的三個文件

1  三個文件從上到下分別寫入以下內容

test.zzq.us=\u963F\u53D1\u65AF\u8482\u82AC
test.order=4

  

test.zzq.china=laksjbflaksbfkasjlbfksabfk\u6848\u5217\u5F00\u53D1\u82AD\u601D\u853B\u64AD\u653E
test.order=2
test.zzq.china=laksjbflaksbfkasjlbfksabfk\u6848\u5217\u5F00\u53D1\u82AD\u601D\u853B\u64AD\u653E
test.order=3
result.interface.error=\u63A5\u53E3
result.interface.error.empty=\u4E0D\u5B58\u5728
result.interface.error.admin=\u9519\u8BEF\uFF0C\u8BF7\u8054\u7CFB\u7BA1\u7406\u5458!
result.interface.error.exception=\u63A5\u53E3 [%s] \u51FA\u73B0\u5F02\u5E38\uFF0C\u65B9\u6CD5\uFF1A%s.%s\uFF0C\u5F02\u5E38\u6458\u8981\uFF1A%s

2 在 application.yml 中配置

spring:
 
  messages:
  #表示放在classpath的i18n文件夾,文件前綴爲mess  
    basename: i18n.mess
    cache-duration: 3600
    encoding: UTF-8

3 配置LocaleMessage

package com.people.common.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.util.Locale;

@Component
public class LocaleMessage {

    @Autowired
    private MessageSource messageSource;

    /**
     * @param code:對應文本配置的key.
     * @return 對應地區的語言消息字符串
     */
    public String getMessage(String code){
        return this.getMessage(code,new Object[]{});
    }

    public String getMessage(String code,String defaultMessage){
        return this.getMessage(code,null,defaultMessage);
    }

    public String getMessage(String code,String defaultMessage,Locale locale){
        return this.getMessage(code,null,defaultMessage,locale);
    }

    public String getMessage(String code,Locale locale){
        return this.getMessage(code,null,"",locale);
    }

    public String getMessage(String code,Object[] args){
        return this.getMessage(code,args,"");
    }

    public String getMessage(String code,Object[] args,Locale locale){
        return this.getMessage(code,args,"",locale);
    }

    public String getMessage(String code,Object[] args,String defaultMessage){
        Locale locale = LocaleContextHolder.getLocale();
        return this.getMessage(code,args, defaultMessage,locale);
    }

    public String getMessage(String code,Object[]args,String defaultMessage,Locale locale){
        return messageSource.getMessage(code,args, defaultMessage,locale);
    }

}

4 封裝代碼

package com.people.common.utils;

import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.people.common.config.LocaleMessage;

/**
 * code:信息的鍵,properties中的key 默認找mess.properties,找不到去其他兩個當中找直到找到爲止
 * 注意:當三個文件當中都有同一個key對應不同的value時 執行順序:
 *  mess_zh_CN.properties > mess.properties > mess_en_US.properties 
 * 所以禁止寫入同一個key對應不同value
 *
 */
@Component
public class I18nUtils {
    @Autowired
	private  LocaleMessage localeMessage;
   
	/**
	 * 獲取key
	 * 
	 * @param key
	 * @return
	 */
	public  String getKey(String key) {
		String name = localeMessage.getMessage(key);
		return name;
	}

	/**
	 * 獲取指定哪個配置文件下的key
	 * 
	 * @param key
	 * @param local
	 * @return
	 */
	public  String getKey(String key, Locale local) {
		String name = localeMessage.getMessage(key, local);
		return name;
	}
}

5 測試

package com.people;
import java.util.Locale;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.people.common.utils.I18nUtils;
import com.people.common.utils.Slf4jLogUtil;
@RunWith(SpringRunner.class)
@SpringBootTest
public class I18nTest {
	/**
	 * code:信息的鍵,properties中的key 默認找mess.properties,找不到去其他兩個當中找直到找到爲止
	 * 注意:當三個文件當中都有同一個key對應不同的value時 執行順序:
	 *  mess_zh_CN.properties > mess.properties > mess_en_US.properties 
	 * 所以禁止寫入同一個key對應不同value
	 *
	 */
	@Autowired
	private I18nUtils i18nUtils;
	@Test
	public void i18nTest1(){
		String key = i18nUtils.getKey("test.order");
	     System.out.println(key);
	    
	}
	/**
	 *  code:信息的鍵,properties中的key
	 *  指定哪個.properties
     */
	@Test
	public void i18nTest2(){
		//在mess_zh_CN.properties找
		 Locale  locale=Locale.CANADA;
	     String name = i18nUtils.getKey("test.zzq.china",locale);
	     System.out.println(name);
	     Slf4jLogUtil.info(name);
	     System.out.println("---------------------------------------");
	     //在mess_en_US.properties找
	     Locale  locale2=Locale.US;
	     String name2 = i18nUtils.getKey("test.zzq.us",locale2);
	     System.out.println(name2);
	     Slf4jLogUtil.info(name2);
	}
	

}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章