📄 localeutils.java
字号:
} buf.append(") "); // Use a friendly english timezone name if the locale is en, otherwise use the timezone id if ("en".equals(locale.getLanguage())) { String name = nameMap.get(zoneID); if (name == null) { name = zoneID; } buf.append(name); } else { buf.append( zone.getDisplayName(true, TimeZone.LONG, locale).replace('_', ' ').replace('/', ' ')); } return buf.toString(); } /** * Returns the specified resource bundle, which is a properties file * that aids in localization of skins. This method is handy since it * uses the class loader that other Jive classes are loaded from (hence, * it can load bundles that are stored in jive.jar). * * @param baseName the name of the resource bundle to load. * @param locale the desired Locale. * @return the specified resource bundle, if it exists. */ public static ResourceBundle getResourceBundle(String baseName, Locale locale) { return ResourceBundle.getBundle(baseName, locale); } /** * Returns an internationalized string loaded from a resource bundle. * The locale used will be the locale specified by JiveGlobals.getLocale(). * * @param key the key to use for retrieving the string from the * appropriate resource bundle. * @return the localized string. */ public static String getLocalizedString(String key) { Locale locale = JiveGlobals.getLocale(); ResourceBundle bundle = ResourceBundle.getBundle(resourceBaseName, locale); return getLocalizedString(key, locale, null, bundle); } /** * Returns an internationalized string loaded from a resource bundle using * the passed in Locale. * * @param key the key to use for retrieving the string from the * appropriate resource bundle. * @param locale the locale to use for retrieving the appropriate * locale-specific string. * @return the localized string. */ public static String getLocalizedString(String key, Locale locale) { ResourceBundle bundle = ResourceBundle.getBundle(resourceBaseName, locale); return getLocalizedString(key, locale, null, bundle); } /** * Returns an internationalized string loaded from a resource bundle using * the locale specified by JiveGlobals.getLocale() substituting the passed * in arguments. Substitution is handled using the * {@link java.text.MessageFormat} class. * * @param key the key to use for retrieving the string from the * appropriate resource bundle. * @param arguments a list of objects to use which are formatted, then * inserted into the pattern at the appropriate places. * @return the localized string. */ public static String getLocalizedString(String key, List arguments) { Locale locale = JiveGlobals.getLocale(); ResourceBundle bundle = ResourceBundle.getBundle(resourceBaseName, locale); return getLocalizedString(key, locale, arguments, bundle); } /** * Returns an internationalized string loaded from a resource bundle from the passed * in plugin. If the plugin name is <tt>null</tt>, the key will be looked up using * the standard resource bundle. * * @param key the key to use for retrieving the string from the * appropriate resource bundle. * @param pluginName the name of the plugin to load the require resource bundle from. * @return the localized string. */ public static String getLocalizedString(String key, String pluginName) { return getLocalizedString(key, pluginName, null); } /** * Returns an internationalized string loaded from a resource bundle from the passed * in plugin. If the plugin name is <tt>null</tt>, the key will be looked up using * the standard resource bundle. * * @param key the key to use for retrieving the string from the * appropriate resource bundle. * @param pluginName the name of the plugin to load the require resource bundle from. * @param arguments a list of objects to use which are formatted, then * inserted into the pattern at the appropriate places. * @return the localized string. */ public static String getLocalizedString(String key, String pluginName, List arguments) { if (pluginName == null) { return getLocalizedString(key, arguments); } Locale locale = JiveGlobals.getLocale(); String i18nFile = pluginName + "_i18n"; // Retrieve classloader from pluginName. final XMPPServer xmppServer = XMPPServer.getInstance(); PluginManager pluginManager = xmppServer.getPluginManager(); Plugin plugin = pluginManager.getPlugin(pluginName); if (plugin == null) { throw new NullPointerException("Plugin could not be located: " + pluginName); } ClassLoader pluginClassLoader = pluginManager.getPluginClassloader(plugin).getClassLoader(); try { ResourceBundle bundle = ResourceBundle.getBundle(i18nFile, locale, pluginClassLoader); return getLocalizedString(key, locale, arguments, bundle); } catch (MissingResourceException mre) { Log.error(mre); return key; } } /** * Retrieve the <code>ResourceBundle</code> that is used with this plugin. * * @param pluginName the name of the plugin. * @return the ResourceBundle used with this plugin. * @throws Exception thrown if an exception occurs. */ public static ResourceBundle getPluginResourceBundle(String pluginName) throws Exception { final Locale locale = JiveGlobals.getLocale(); String i18nFile = pluginName + "_i18n"; // Retrieve classloader from pluginName. final XMPPServer xmppServer = XMPPServer.getInstance(); PluginManager pluginManager = xmppServer.getPluginManager(); Plugin plugin = pluginManager.getPlugin(pluginName); if (plugin == null) { throw new NullPointerException("Plugin could not be located."); } ClassLoader pluginClassLoader = pluginManager.getPluginClassloader(plugin).getClassLoader(); return ResourceBundle.getBundle(i18nFile, locale, pluginClassLoader); } /** * Returns an internationalized string loaded from a resource bundle using * the passed in Locale substituting the passed in arguments. Substitution * is handled using the {@link MessageFormat} class. * * @param key the key to use for retrieving the string from the * appropriate resource bundle. * @param locale the locale to use for retrieving the appropriate * locale-specific string. * @param arguments a list of objects to use which are formatted, then * inserted into the pattern at the appropriate places. * @return the localized string. */ public static String getLocalizedString(String key, Locale locale, List arguments, ResourceBundle bundle) { if (key == null) { throw new NullPointerException("Key cannot be null"); } if (locale == null) { locale = JiveGlobals.getLocale(); } String value; // See if the bundle has a value try { // The jdk caches resource bundles on it's own, so we won't bother. value = bundle.getString(key); // perform argument substitutions if (arguments != null) { MessageFormat messageFormat = new MessageFormat(""); messageFormat.setLocale(bundle.getLocale()); messageFormat.applyPattern(value); try { // This isn't fool-proof, but it's better than nothing // The idea is to try and convert strings into the // types of objects that the formatters expects // i.e. Numbers and Dates Format[] formats = messageFormat.getFormats(); for (int i = 0; i < formats.length; i++) { Format format = formats[i]; if (format != null) { if (format instanceof DateFormat) { if (arguments.size() > i) { Object val = arguments.get(i); if (val instanceof String) { DateFormat dateFmt = (DateFormat)format; try { val = dateFmt.parse((String)val); arguments.set(i, val); } catch (ParseException e) { Log.error(e); } } } } else if (format instanceof NumberFormat) { if (arguments.size() > i) { Object val = arguments.get(i); if (val instanceof String) { NumberFormat nbrFmt = (NumberFormat)format; try { val = nbrFmt.parse((String)val); arguments.set(i, val); } catch (ParseException e) { Log.error(e); } } } } } } value = messageFormat.format(arguments.toArray()); } catch (IllegalArgumentException e) { Log.error("Unable to format resource string for key: " + key + ", argument type not supported"); value = ""; } } } catch (java.util.MissingResourceException mre) { Log.warn("Missing resource for key: " + key + " in locale " + locale.toString()); value = ""; } return value; } /** * Returns an internationalized String representation of the number using * the default locale. * * @param number the number to format. * @return an internationalized String representation of the number. */ public static String getLocalizedNumber(long number) { return NumberFormat.getInstance().format(number); } /** * Returns an internationalized String representation of the number using * the specified locale. * * @param number the number to format. * @param locale the locale to use for formatting. * @return an internationalized String representation of the number. */ public static String getLocalizedNumber(long number, Locale locale) { return NumberFormat.getInstance(locale).format(number); } /** * Returns an internationalized String representation of the number using * the default locale. * * @param number the number to format. * @return an internationalized String representation of the number. */ public static String getLocalizedNumber(double number) { return NumberFormat.getInstance().format(number); } /** * Returns an internationalized String representation of the number using * the specified locale. * * @param number the number to format. * @param locale the locale to use for formatting. * @return an internationalized String representation of the number. */ public static String getLocalizedNumber(double number, Locale locale) { return NumberFormat.getInstance(locale).format(number); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -