|
@@ -24,13 +24,23 @@ public class I18nUtil {
|
|
|
private final MessageSource MESSAGE_SOURCE = SpringUtil.getBean(MessageSource.class);
|
|
private final MessageSource MESSAGE_SOURCE = SpringUtil.getBean(MessageSource.class);
|
|
|
private static final String SLF4J_STYLE_PLACEHOLDER = StrPool.DELIM_START + StrPool.DELIM_END;
|
|
private static final String SLF4J_STYLE_PLACEHOLDER = StrPool.DELIM_START + StrPool.DELIM_END;
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 取Spring提供的MessageSource
|
|
|
|
|
+ */
|
|
|
public MessageSource getMessageSource() {
|
|
public MessageSource getMessageSource() {
|
|
|
return MESSAGE_SOURCE;
|
|
return MESSAGE_SOURCE;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 根据消息键和参数,获取国际化翻译值
|
|
|
|
|
|
|
+ * 取默认locale
|
|
|
|
|
+ * 强国际化SaaS需求,也许可以改造成从特定ThreadLocal或者UserContextHolder中获取
|
|
|
|
|
+ */
|
|
|
|
|
+ public Locale getDefaultLocale() {
|
|
|
|
|
+ return Locale.getDefault();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 根据消息键和参数,获取国际化翻译值;默认使用当前JVM的默认locale
|
|
|
* 支持模板填充,如: Nickname '{}' is already exist, do you like '{}'?
|
|
* 支持模板填充,如: Nickname '{}' is already exist, do you like '{}'?
|
|
|
*
|
|
*
|
|
|
* @param code 消息代码
|
|
* @param code 消息代码
|
|
@@ -38,12 +48,25 @@ public class I18nUtil {
|
|
|
* @return null or 国际化翻译值
|
|
* @return null or 国际化翻译值
|
|
|
*/
|
|
*/
|
|
|
public String messageOf(String code, Object... templateParams) {
|
|
public String messageOf(String code, Object... templateParams) {
|
|
|
|
|
+ return messageOf(getDefaultLocale(), code, templateParams);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 根据消息键和参数,获取国际化翻译值
|
|
|
|
|
+ * 支持模板填充,如: Nickname '{}' is already exist, do you like '{}'?
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param locale locale
|
|
|
|
|
+ * @param code 消息代码
|
|
|
|
|
+ * @param templateParams 模板填充参数
|
|
|
|
|
+ * @return null or 国际化翻译值
|
|
|
|
|
+ */
|
|
|
|
|
+ public String messageOf(Locale locale, String code, Object... templateParams) {
|
|
|
if (code == null) {
|
|
if (code == null) {
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
- String msg = getMessageSource().getMessage(code, null, Locale.getDefault());
|
|
|
|
|
|
|
+ String msg = getMessageSource().getMessage(code, null, locale != null ? locale : getDefaultLocale());
|
|
|
if (StrUtil.isEmpty(msg)) {
|
|
if (StrUtil.isEmpty(msg)) {
|
|
|
return msg;
|
|
return msg;
|
|
|
}
|
|
}
|
|
@@ -61,7 +84,7 @@ public class I18nUtil {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 根据消息键和参数,获取国际化翻译值
|
|
|
|
|
|
|
+ * 根据消息键和参数,获取国际化翻译值;默认使用当前JVM的默认locale
|
|
|
* 支持模板填充,如: Nickname '{}' is already exist, do you like '{}'?
|
|
* 支持模板填充,如: Nickname '{}' is already exist, do you like '{}'?
|
|
|
*
|
|
*
|
|
|
* @param code 消息代码
|
|
* @param code 消息代码
|
|
@@ -70,12 +93,26 @@ public class I18nUtil {
|
|
|
* @return null or 国际化翻译值
|
|
* @return null or 国际化翻译值
|
|
|
*/
|
|
*/
|
|
|
public String messageOf(String code, String defaultValue, Object... templateParams) {
|
|
public String messageOf(String code, String defaultValue, Object... templateParams) {
|
|
|
- String msg = messageOf(code, templateParams);
|
|
|
|
|
|
|
+ return messageOf(getDefaultLocale(), code, defaultValue, templateParams);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 根据消息键和参数,获取国际化翻译值
|
|
|
|
|
+ * 支持模板填充,如: Nickname '{}' is already exist, do you like '{}'?
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param locale locale
|
|
|
|
|
+ * @param code 消息代码
|
|
|
|
|
+ * @param defaultValue 未找到对应国际化翻译值的情况下,默认返回值
|
|
|
|
|
+ * @param templateParams 模板填充参数
|
|
|
|
|
+ * @return null or 国际化翻译值
|
|
|
|
|
+ */
|
|
|
|
|
+ public String messageOf(Locale locale, String code, String defaultValue, Object... templateParams) {
|
|
|
|
|
+ String msg = messageOf(locale, code, templateParams);
|
|
|
return msg == null ? defaultValue : msg;
|
|
return msg == null ? defaultValue : msg;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 根据枚举值和参数,获取国际化翻译值
|
|
|
|
|
|
|
+ * 根据枚举值和参数,获取国际化翻译值;默认使用当前JVM的默认locale
|
|
|
* 支持模板填充,如: Nickname '{}' has been existing, do you like '{}'?
|
|
* 支持模板填充,如: Nickname '{}' has been existing, do you like '{}'?
|
|
|
*
|
|
*
|
|
|
* @param enumField 枚举值
|
|
* @param enumField 枚举值
|
|
@@ -83,6 +120,19 @@ public class I18nUtil {
|
|
|
* @return null or 国际化翻译值
|
|
* @return null or 国际化翻译值
|
|
|
*/
|
|
*/
|
|
|
public String messageOf(Enum<?> enumField, Object... templateParams) {
|
|
public String messageOf(Enum<?> enumField, Object... templateParams) {
|
|
|
|
|
+ return messageOf(getDefaultLocale(), enumField, templateParams);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 根据枚举值和参数,获取国际化翻译值
|
|
|
|
|
+ * 支持模板填充,如: Nickname '{}' has been existing, do you like '{}'?
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param locale locale
|
|
|
|
|
+ * @param enumField 枚举值
|
|
|
|
|
+ * @param templateParams 模板填充参数
|
|
|
|
|
+ * @return null or 国际化翻译值
|
|
|
|
|
+ */
|
|
|
|
|
+ public String messageOf(Locale locale, Enum<?> enumField, Object... templateParams) {
|
|
|
if (enumField == null) {
|
|
if (enumField == null) {
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
@@ -91,14 +141,14 @@ public class I18nUtil {
|
|
|
|
|
|
|
|
// 尝试以较长的 枚举类短名.枚举值name 为 code 尝试获取翻译值
|
|
// 尝试以较长的 枚举类短名.枚举值name 为 code 尝试获取翻译值
|
|
|
i18nCode = String.format("%s.%s", enumField.getDeclaringClass().getSimpleName(), enumField.name());
|
|
i18nCode = String.format("%s.%s", enumField.getDeclaringClass().getSimpleName(), enumField.name());
|
|
|
- String i18nMessage = messageOf(i18nCode, templateParams);
|
|
|
|
|
|
|
+ String i18nMessage = messageOf(locale, i18nCode, templateParams);
|
|
|
if (StrUtil.isNotEmpty(i18nMessage)) {
|
|
if (StrUtil.isNotEmpty(i18nMessage)) {
|
|
|
return i18nMessage;
|
|
return i18nMessage;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 尝试以 枚举值name 为 code 尝试获取翻译值
|
|
// 尝试以 枚举值name 为 code 尝试获取翻译值
|
|
|
i18nCode = enumField.name();
|
|
i18nCode = enumField.name();
|
|
|
- i18nMessage = messageOf(i18nCode, templateParams);
|
|
|
|
|
|
|
+ i18nMessage = messageOf(locale, i18nCode, templateParams);
|
|
|
if (StrUtil.isNotEmpty(i18nMessage)) {
|
|
if (StrUtil.isNotEmpty(i18nMessage)) {
|
|
|
return i18nMessage;
|
|
return i18nMessage;
|
|
|
}
|
|
}
|