package com.vci.starter.web.util;
|
|
import org.springframework.context.MessageSource;
|
import org.springframework.context.i18n.LocaleContextHolder;
|
import org.springframework.stereotype.Component;
|
|
/**
|
* 国际化工具
|
* @author weidy
|
* @date 2019/11/7 2:41 PM
|
*/
|
@Component
|
public class MessageUtils {
|
|
/**
|
* 注入国际化资源镜像
|
*/
|
private static MessageSource messageSource;
|
|
/**
|
* 构造函数
|
* @param messageSource 国际化资源
|
*/
|
public MessageUtils(MessageSource messageSource) {
|
MessageUtils.messageSource = messageSource;
|
}
|
|
/**
|
* 获取单个国际化翻译值
|
* @param msgKey 资源代号
|
* @return 翻译后的值
|
*/
|
public static String get(String msgKey) {
|
try {
|
return messageSource.getMessage(msgKey, null, LocaleContextHolder.getLocale());
|
} catch (Throwable e) {
|
return msgKey;
|
}
|
}
|
|
/**
|
* 获取单个国际化翻译值
|
* @param msgKey 资源代号
|
* @param objects 翻译时使用的对象
|
* @return 翻译后的值
|
*/
|
public static String get(String msgKey,Object[] objects){
|
try {
|
return messageSource.getMessage(msgKey, objects, LocaleContextHolder.getLocale());
|
} catch (Throwable e) {
|
return msgKey;
|
}
|
}
|
}
|