messagecatalog.java
来自「java jdk 1.4的源码」· Java 代码 · 共 574 行 · 第 1/2 页
JAVA
574 行
for (int i = 0; i < parameters.length; i++) { if (!(parameters[i] instanceof String) && !(parameters[i] instanceof Number) && !(parameters[i] instanceof java.util.Date)) { if (parameters [i] == null) parameters [i] = "(null)"; else parameters[i] = parameters[i].toString(); } } // similarly, cope with unsupported locale... if (locale == null) locale = Locale.getDefault (); // get the appropriately localized MessageFormat object ResourceBundle bundle; MessageFormat format; try { bundle = ResourceBundle.getBundle (bundleName, locale); format = new MessageFormat (bundle.getString (messageId)); } catch (MissingResourceException e) { String retval; retval = packagePrefix (messageId); for (int i = 0; i < parameters.length; i++) { retval += ' '; retval += parameters [i]; } return retval; } format.setLocale (locale); // return the formatted message StringBuffer result = new StringBuffer (); result = format.format (parameters, result, new FieldPosition (0)); return result.toString (); } /** * Chooses a client locale to use, using the first language specified in * the list that is supported by this catalog. If none of the specified * languages is supported, a null value is returned. Such a list of * languages might be provided in an HTTP/1.1 "Accept-Language" header * field, or through some other content negotiation mechanism. * * <P> The language specifiers recognized are RFC 1766 style ("fr" for * all French, "fr-ca" for Canadian French), although only the strict * ISO subset (two letter language and country specifiers) is currently * supported. Java-style locale strings ("fr_CA") are also supported. * * @see java.util.Locale * * @param languages Array of language specifiers, ordered with the most * preferable one at the front. For example, "en-ca" then "fr-ca", * followed by "zh_CN". * @return The most preferable supported locale, or null. */ public Locale chooseLocale (String languages []) { if ((languages = canonicalize (languages)) != null) { for (int i = 0; i < languages.length; i++) if (isLocaleSupported (languages [i])) return getLocale (languages [i]); } return null; } // // Canonicalizes the RFC 1766 style language strings ("en-in") to // match standard Java usage ("en_IN"), removing strings that don't // use two character ISO language and country codes. Avoids all // memory allocations possible, so that if the strings passed in are // just lowercase ISO codes (a common case) the input is returned. // private String [] canonicalize (String languages []) { boolean didClone = false; int trimCount = 0; if (languages == null) return languages; for (int i = 0; i < languages.length; i++) { String lang = languages [i]; int len = lang.length (); // no RFC1766 extensions allowed; "zh" and "zh-tw" (etc) are OK // as are regular locale names with no variant ("de_CH"). if (!(len == 2 || len == 5)) { if (!didClone) { languages = (String []) languages.clone (); didClone = true; } languages [i] = null; trimCount++; continue; } // language code ... if already lowercase, we change nothing if (len == 2) { lang = lang.toLowerCase (); if (lang != languages [i]) { if (!didClone) { languages = (String []) languages.clone (); didClone = true; } languages [i] = lang; } continue; } // language_country ... fixup case, force "_" char buf [] = new char [5]; buf [0] = Character.toLowerCase (lang.charAt (0)); buf [1] = Character.toLowerCase (lang.charAt (1)); buf [2] = '_'; buf [3] = Character.toUpperCase (lang.charAt (3)); buf [4] = Character.toUpperCase (lang.charAt (4)); if (!didClone) { languages = (String []) languages.clone (); didClone = true; } languages [i] = new String (buf); } // purge any shadows of deleted RFC1766 extended language codes if (trimCount != 0) { String temp [] = new String [languages.length - trimCount]; int i; for (i = 0, trimCount = 0; i < temp.length; i++) { while (languages [i + trimCount] == null) trimCount++; temp [i] = languages [i + trimCount]; } languages = temp; } return languages; } // // Returns a locale object supporting the specified locale, using // a small cache to speed up some common languages and reduce the // needless allocation of memory. // private Locale getLocale (String localeName) { String language, country; int index; index = localeName.indexOf ('_'); if (index == -1) { // // Special case the builtin JDK languages // if (localeName.equals ("de")) return Locale.GERMAN; if (localeName.equals ("en")) return Locale.ENGLISH; if (localeName.equals ("fr")) return Locale.FRENCH; if (localeName.equals ("it")) return Locale.ITALIAN; if (localeName.equals ("ja")) return Locale.JAPANESE; if (localeName.equals ("ko")) return Locale.KOREAN; if (localeName.equals ("zh")) return Locale.CHINESE; language = localeName; country = ""; } else { if (localeName.equals ("zh_CN")) return Locale.SIMPLIFIED_CHINESE; if (localeName.equals ("zh_TW")) return Locale.TRADITIONAL_CHINESE; // // JDK also has constants for countries: en_GB, en_US, en_CA, // fr_FR, fr_CA, de_DE, ja_JP, ko_KR. We don't use those. // language = localeName.substring (0, index); country = localeName.substring (index + 1); } return new Locale (language, country); } // // cache for isLanguageSupported(), below ... key is a language // or locale name, value is a Boolean // private Hashtable cache = new Hashtable (5); /** * Returns true iff the specified locale has explicit language support. * For example, the traditional Chinese locale "zh_TW" has such support * if there are message bundles suffixed with either "zh_TW" or "zh". * * <P> This method is used to bypass part of the search path mechanism * of the <code>ResourceBundle</code> class, specifically the parts which * force use of default locales and bundles. Such bypassing is required * in order to enable use of a client's preferred languages. Following * the above example, if a client prefers "zh_TW" but can also accept * "ja", this method would be used to detect that there are no "zh_TW" * resource bundles and hence that "ja" messages should be used. This * bypasses the ResourceBundle mechanism which will return messages in * some other locale (picking some hard-to-anticipate default) instead * of reporting an error and letting the client choose another locale. * * @see java.util.Locale * * @param localeName A standard Java locale name, using two character * language codes optionally suffixed by country codes. * @return True iff the language of that locale is supported. */ public boolean isLocaleSupported (String localeName) { // // Use previous results if possible. We expect that the codebase // is immutable, so we never worry about changing the cache. // Boolean value = (Boolean) cache.get (localeName); if (value != null) return value.booleanValue (); // // Try "language_country_variant", then "language_country", // then finally "language" ... assuming the longest locale name // is passed. If not, we'll try fewer options. // ClassLoader loader = null; for (;;) { String name = bundleName + "_" + localeName; // look up classes ... try { Class.forName (name); cache.put (localeName, Boolean.TRUE); return true; } catch (Exception e) {} // ... then property files (only for ISO Latin/1 messages) InputStream in; if (loader == null) loader = getClass ().getClassLoader (); name = name.replace ('.', '/'); name = name + ".properties"; if (loader == null) in = ClassLoader.getSystemResourceAsStream (name); else in = loader.getResourceAsStream (name); if (in != null) { cache.put (localeName, Boolean.TRUE); return true; } int index = localeName.indexOf ('_'); if (index > 0) localeName = localeName.substring (0, index); else break; } // // If we got this far, we failed. Remember for later. // cache.put (localeName, Boolean.FALSE); return false; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?