配置步骤:
新建标准的三个文件
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);
}
}