package com.vci.common.exception; import java.text.MessageFormat; import java.util.HashMap; import java.util.Locale; import java.util.ResourceBundle; public class LocaleCommonDisplay { private static HashMap map = new HashMap(); public LocaleCommonDisplay() { } /** * 从指定文件中获取指定key的国际化信息 * @param key,主键 * @param fileName,国际化文件名称 * @param locale,当前区域 * @return */ public static String getI18nString(VciException exp, String fileName, Locale locale) { ResourceBundle resStrings = map.get(fileName); if (resStrings == null) { try { resStrings = ResourceBundle.getBundle("properties." + fileName, locale); map.put(fileName, resStrings); } catch (Exception e) { e.printStackTrace(); resStrings = null; } } return getLocaleString(resStrings, exp); } /** * 获取指定key对应的值,如果key对应的值为空或者出现获取异常,则返回key。 * @param key * @return */ private static String getLocaleString(ResourceBundle resStrings, String key) { try { String value = resStrings.getString(key); if (value == null) { value = key; } return value; } catch (Exception e) { return key; } } public static String getI18nString(String key, String fileName, Locale locale) { ResourceBundle resStrings = map.get(fileName); if (resStrings == null) { try { resStrings = ResourceBundle.getBundle("properties." + fileName, locale); map.put(fileName, resStrings); } catch (Exception e) { e.printStackTrace(); resStrings = null; } } return getLocaleString(resStrings, key); } /** * 根据异常结果,获取国际化异常提示信息。 * 文件中有如下三种类型值: * 1、一个参数类型,如10000=客户端文件夹{0}下版本配置文件读取错误 * 2、无参数类型:如10001=客户端配置文件读取错误 * 3、多各参数类型:如10002=客户端配置文件{0}读取错误,具体错误信息为{1} * * @param exp * @return */ private static String getLocaleString(ResourceBundle resStrings, VciException exp) { if (exp == null) { return ""; } String message = getLocaleString(resStrings, String.valueOf(exp.getException_code())); //无参数时,返回获取结果 if (exp.getException_objArray().length == 0) { return message; } int len = exp.getException_objArray().length; for (int i = 0; i < len - 1; i++) { String uniqueIndex = ORAUniqueProperties.getUniqueIndexName(exp.getException_objArray()[i]); if (uniqueIndex == null || uniqueIndex.trim().equals("")) { continue; } exp.getException_objArray()[i] = getI18nString(uniqueIndex, "ORA00001", resStrings.getLocale()); } //有参数时,根据参数及properties文件获取结果组合成显示信息返回 String msg = MessageFormat.format(message, exp.getException_objArray()); return msg; } }