📄 locale.java
字号:
* 'Deutsch'. If the display name can not be localized to the supplied * locale, it will fall back on other output in the following order: * </p> * <ul> * <li>the display name in the default locale</li> * <li>the display name in English</li> * <li>the ISO code</li> * </ul> * <p> * If the language is unspecified by this locale, then the empty string is * returned. * </p> * * @param inLocale the locale to use for formatting the display string. * @return the language name of this locale localized to the given locale, * with the default locale, English and the ISO code as backups. * @throws NullPointerException if the supplied locale is null. */ public String getDisplayLanguage(Locale inLocale) { try { ResourceBundle bundle = ResourceBundle.getBundle("gnu.java.locale.iso639", inLocale); return bundle.getString(language); } catch (MissingResourceException ex) { return language; } } /** * Returns the country name of this locale localized to the * default locale. If the localized is not found, the ISO code * is returned. This has the same effect as * <pre> * getDisplayCountry(Locale.getDefault()); * </pre> * * @return the country name of this locale localized to the given locale, * with the ISO code as backup */ public String getDisplayCountry() { return getDisplayCountry(defaultLocale); } /** * <p> * Gets the name of the country specified by this locale, in a form suitable * for display to the user. If possible, the display name will be localized * to the specified locale. For example, if the locale instance is * <code>Locale.GERMANY</code>, and the specified locale is <code>Locale.UK</code>, * the result would be 'Germany'. Using the German locale would instead give * 'Deutschland'. If the display name can not be localized to the supplied * locale, it will fall back on other output in the following order: * </p> * <ul> * <li>the display name in the default locale</li> * <li>the display name in English</li> * <li>the ISO code</li> * </ul> * <p> * If the country is unspecified by this locale, then the empty string is * returned. * </p> * * @param inLocale the locale to use for formatting the display string. * @return the country name of this locale localized to the given locale, * with the default locale, English and the ISO code as backups. * @throws NullPointerException if the supplied locale is null. */ public String getDisplayCountry(Locale inLocale) { try { ResourceBundle bundle = ResourceBundle.getBundle("gnu.java.locale.iso3166", inLocale); return bundle.getString(country); } catch (MissingResourceException ex) { return country; } } /** * Returns the variant name of this locale localized to the * default locale. If the localized is not found, the variant code * itself is returned. This has the same effect as * <pre> * getDisplayVariant(Locale.getDefault()); * </pre> * * @return the variant code of this locale localized to the given locale, * with the ISO code as backup */ public String getDisplayVariant() { return getDisplayVariant(defaultLocale); } /** * <p> * Gets the name of the variant specified by this locale, in a form suitable * for display to the user. If possible, the display name will be localized * to the specified locale. For example, if the locale instance is a revised * variant, and the specified locale is <code>Locale.UK</code>, the result * would be 'REVISED'. Using the German locale would instead give * 'Revidiert'. If the display name can not be localized to the supplied * locale, it will fall back on other output in the following order: * </p> * <ul> * <li>the display name in the default locale</li> * <li>the display name in English</li> * <li>the ISO code</li> * </ul> * <p> * If the variant is unspecified by this locale, then the empty string is * returned. * </p> * * @param inLocale the locale to use for formatting the display string. * @return the variant name of this locale localized to the given locale, * with the default locale, English and the ISO code as backups. * @throws NullPointerException if the supplied locale is null. */ public String getDisplayVariant(Locale inLocale) { // XXX - load a bundle? return variant; } /** * Gets all local components suitable for display to the user, formatted * for the default locale. For the language component, getDisplayLanguage * is called. For the country component, getDisplayCountry is called. * For the variant set component, getDisplayVariant is called. * * <p>The returned String will be one of the following forms:<br> * <pre> * language (country, variant) * language (country) * language (variant) * country (variant) * language * country * variant * </pre> * * @return String version of this locale, suitable for display to the user */ public String getDisplayName() { return getDisplayName(defaultLocale); } /** * Gets all local components suitable for display to the user, formatted * for a specified locale. For the language component, * getDisplayLanguage(Locale) is called. For the country component, * getDisplayCountry(Locale) is called. For the variant set component, * getDisplayVariant(Locale) is called. * * <p>The returned String will be one of the following forms:<br> * <pre> * language (country, variant) * language (country) * language (variant) * country (variant) * language * country * variant * </pre> * * @param locale locale to use for formatting * @return String version of this locale, suitable for display to the user */ public String getDisplayName(Locale locale) { StringBuffer result = new StringBuffer(); int count = 0; String[] delimiters = {"", " (", ","}; if (language.length() != 0) { result.append(delimiters[count++]); result.append(getDisplayLanguage(locale)); } if (country.length() != 0) { result.append(delimiters[count++]); result.append(getDisplayCountry(locale)); } if (variant.length() != 0) { result.append(delimiters[count++]); result.append(getDisplayVariant(locale)); } if (count > 1) result.append(")"); return result.toString(); } /** * Does the same as <code>Object.clone()</code> but does not throw * a <code>CloneNotSupportedException</code>. Why anyone would * use this method is a secret to me, since this class is immutable. * * @return the clone */ public Object clone() { // This class is final, so no need to use native super.clone(). return new Locale(language, country, variant); } /** * Return the hash code for this locale. The hashcode is the logical * xor of the hash codes of the language, the country and the variant. * The hash code is precomputed, since <code>Locale</code>s are often * used in hash tables. * * @return the hashcode */ public int hashCode() { return hashcode; } /** * Compares two locales. To be equal, obj must be a Locale with the same * language, country, and variant code. * * @param obj the other locale * @return true if obj is equal to this */ public boolean equals(Object obj) { if (this == obj) return true; if (! (obj instanceof Locale)) return false; Locale l = (Locale) obj; return (language == l.language && country == l.country && variant == l.variant); } /** * Write the locale to an object stream. * * @param output the stream to write to * @throws IOException if the write fails * @serialData The first three fields are Strings representing language, * country, and variant. The fourth field is a placeholder for * the cached hashcode, but this is always written as -1, and * recomputed when reading it back. */ private void writeObject(ObjectOutputStream s) throws IOException { s.writeObject(language); s.writeObject(country); s.writeObject(variant); // Hashcode field is always written as -1. s.writeInt(-1); } /** * Reads a locale from the input stream. * * @param input the stream to read from * @throws IOException if reading fails * @throws ClassNotFoundException if reading fails * @serialData the hashCode is always invalid and must be recomputed */ private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException { language = ((String) s.readObject()).intern(); country = ((String) s.readObject()).intern(); variant = ((String) s.readObject()).intern(); // Recompute hashcode. hashcode = language.hashCode() ^ country.hashCode() ^ variant.hashCode(); }} // class Locale
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -