package com.vci.client.ui.locale; import java.text.MessageFormat; import java.util.HashMap; import java.util.Locale; import java.util.ResourceBundle; import com.vci.client.ui.exception.VCIException; /** * 国际化信息提取文件 * 使用方法:通过构造函数设置国际化文件名称以及国际化的区域,而后通过getLocaleString方法获取国际值 * * @author Administrator * */ public class LocaleDisplay { private static HashMap map = new HashMap(); public LocaleDisplay() { } /** * 从指定文件中获取指定key的国际化信息 * @param key,主键 * @param fileName,国际化文件名称 * @param locale,当前区域 * @return */ 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); } /** * * @param exp * @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 */ public 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; } } /** * 根据异常结果,获取国际化异常提示信息。 * 文件中有如下三种类型值: * 1、一个参数类型,如10000=客户端文件夹{0}下版本配置文件读取错误 * 2、无参数类型:如10001=客户端配置文件读取错误 * 3、多各参数类型:如10002=客户端配置文件{0}读取错误,具体错误信息为{1} * * @param exp * @return */ public 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; } //有参数时,根据参数及properties文件获取结果组合成显示信息返回 String msg = MessageFormat.format(message, exp.getException_objArray()); return msg; } }