欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

异常信息国际化

程序员文章站 2022-03-03 12:39:54
...
JAVA代码

package com.ce.common.util;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.support.ResourceBundleMessageSource;

public class I18N {

private static Map<String, I18N> i18nMap = new HashMap<String, I18N>();
private String namespace = "default_i18n";
private String[] basenames = new String[0];
private ResourceBundleMessageSource resourceBundleMessageSource = null;

public I18N() {
i18nMap.put("default_i18n", this);
}

public static I18N getI18N(String namespace) {
return i18nMap.get(namespace);
}

public String getNamespace() {
return namespace;
}

public void setNamespace(String namespace) {
this.namespace = namespace;
if (i18nMap.containsKey(namespace)) {
throw new IllegalArgumentException("duplicated namespace: "
+ namespace);
}
i18nMap.put(namespace, this);
}

public String[] getBasenames() {
return basenames;
}

public void setBasenames(String[] basenames) {
this.basenames = basenames;
}

public ResourceBundleMessageSource getResourceBundleMessageSource() {
if (resourceBundleMessageSource == null) {
resourceBundleMessageSource = new ResourceBundleMessageSource();
resourceBundleMessageSource.setBasenames(getBasenames());
}
return resourceBundleMessageSource;
}

public void setResourceBundleMessageSource(
ResourceBundleMessageSource resourceBundleMessageSource) {
this.resourceBundleMessageSource = resourceBundleMessageSource;
}

public String getMessage(String code, Object[] args, String defaultMessage,
Locale locale) {
String message = code;
try {
message = getResourceBundleMessageSource().getMessage(code,
praseArgs(args, locale, this), defaultMessage, locale);
} catch (Exception e) {
}
return message;
}

public String getMessage(String code, Locale locale)
throws NoSuchMessageException {
String message = code;
try {
message = getResourceBundleMessageSource().getMessage(code, null,
locale);
} catch (Exception e) {
}
return message;
}

public String getMessage(String code, Object[] args, Locale locale)
throws NoSuchMessageException {
String message = code;
try {
message = getResourceBundleMessageSource().getMessage(code,
praseArgs(args, locale, this), locale);
} catch (Exception e) {
}
return message;
}

public String getMessage(MessageSourceResolvable resolvable, Locale locale)
throws NoSuchMessageException {
return getResourceBundleMessageSource().getMessage(resolvable, locale);
}

private static Pattern p = Pattern.compile("\\$\\{([\\w.]+)\\}");

private static Object[] praseArgs(Object[] args, Locale locale, I18N i18n) {
if (args == null || args.length == 0) {
return args;
}
StringBuffer sb = new StringBuffer(128);
for (int i = 0; i < args.length; i++) {
try {
if (!(args[i] != null && (args[i] instanceof String))) {
continue;
}
String str = (String) args[i];
if (!(str.indexOf("${") > -1)) {
continue;
}
sb.setLength(0);
Matcher matcher = p.matcher(str);
while (matcher.find()) {
String replace = matcher.group();
try {
replace = i18n.getMessage(matcher.group(1), locale);
} catch (Exception e) {
e.printStackTrace();
}
matcher.appendReplacement(sb, replace);
}
matcher.appendTail(sb);
args[i] = sb.toString();
} catch (Exception e) {
}
}
return args;
}
}

异常信息国际化
1.在Spring配置文件中配置异常信息国际化
	<bean class="com.ce.common.util.I18N" lazy-init="false">
<property name="namespace" value="ATMS" />
<property name="resourceBundleMessageSource">
<bean
class="org.springframework.context.support.ResourceBundleMessageSource"
lazy-init="false">
<property name="basenames">
<list>
<value>i18n.atms.atms</value>
<value>i18n.atms.messages</value>
</list>
</property>
</bean>
</property>
</bean>

其中namespace中必需的,不同的项目其namespace需要不同,重复的namespace会出错
2.结合Action使用
	public String getMessage(HttpServletRequest request, String code,
String defaultMessage, Object[] args) {
return getI18N().getMessage(code, args, defaultMessage, request.getLocale());
}

public String getMessage(HttpServletRequest request, String code) {
return getI18N().getMessage(code, request.getLocale());
}

public String getMessage(HttpServletRequest request, String code,
Object[] args) {
return getI18N().getMessage(code, args, request.getLocale());
}

public I18N getI18N() {
return I18N.getI18N("ATMS");
}

3.使用效果示例
error.a=添加 {0} 出错
error.b=实体B
error.c=实体C
getMessage(request, "error.a") --> 添加 {0} 出错
getMessage(request, "error.a", new Object[]{"Test"}) --> 添加 Test 出错
getMessage(request, "error.a", new Object[]{"${error.b}"}) --> 添加 实体B 出错