locale.java

来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 967 行 · 第 1/3 页

JAVA
967
字号
        defaultLocale = newLocale;
    }

    /**
     * Getter for programmatic name of field,
     * an lowercased two-letter ISO-639 code.
     * @see #getDisplayLanguage
     */
    public String getLanguage() {
        return language;
    }

    /**
     * Getter for programmatic name of field,
     * an uppercased two-letter ISO-3166 code.
     * @see #getDisplayCountry
     */
    public String getCountry() {
        return country;
    }

    /**
     * Getter for programmatic name of field.
     * @see #getDisplayVariant
     */
    public String getVariant() {
        return variant;
    }

    /**
     * Getter for the programmatic name of the entire locale,
     * with the language, country and variant separated by underbars.
     * Language is always lower case, and country is always uppcer case.
     * If a field is missing, at most one underbar will occur.
     * Example: "Een, "de_DE", "en_US_WIN", "de_POSIX", "fr_MAC"
     * @see #getDisplayName
     */
    public final String toString() {
        StringBuffer result = new StringBuffer(language);
        if (country.length() != 0) {
            result.append('_');
            result.append(country);
            if (variant.length() != 0) {
                result.append('_');
                result.append(variant);
            }
        }
        return result.toString();
    }

    /**
     * Getter for the three-letter ISO language abbreviation
     * of the locale.  Returns the empty string if the locale doesn't specify a language.
     * @exception MissingResourceException Throws MissingResourceException if the
     * three-letter language abbreviation is not available for this locale.
     */
    public String getISO3Language() throws MissingResourceException {
        if (language.length() == 0)
            return "";

        // the call to getISO2Language() will throw a MissingResourceException if
        // the appropriate locale isn't installed
        getISO2Language();

        ResourceBundle resource = ResourceBundle.getBundle
                ("java.text.resources.LocaleElements", this);
        return resource.getString("ShortLanguage");
    }

    /**
     * Getter for the three-letter ISO country abbreviation
     * of the locale.  Returns the empty string if the locale doesn't specify a country.
     * @exception MissingResourceException Throws MissingResourceException if the
     * three-letter language abbreviation is not available for this locale.
     */
    public String getISO3Country() throws MissingResourceException {
        if (country.length() == 0)
            return "";

        // the call to getISO2Country() will throw a MissingResourceException if
        // the appropriate locale isn't installed
        getISO2Country();

        ResourceBundle resource = ResourceBundle.getBundle
                ("java.text.resources.LocaleElements", this);
        return resource.getString("ShortCountry");
    }

    /**
     * Getter for the two-letter ISO language abbreviation
     * of the locale.  Returns the empty string if the locale doesn't specify a language.
     * @exception MissingResourceException Throws MissingResourceException if the
     * two-letter language abbreviation is not available for this locale.
     */
    /*public*/ String getISO2Language() throws MissingResourceException {
        if (language.length() == 0)
            return "";

        ResourceBundle resource = ResourceBundle.getBundle
                ("java.text.resources.LocaleElements", this);
        String localeID = resource.getString("LocaleString");
        String result = localeID.substring(0, 2);
        if (!result.equals(language))
            throw new MissingResourceException("Requested resource bundle not installed",
                "LocaleElements", "LocaleString");
        return result;
    }

    /**
     * Getter for the two-letter ISO country abbreviation
     * of the locale.  Returns the empty string if the locale doesn't specify a country.
     * @exception MissingResourceException Throws MissingResourceException if the
     * two-letter language abbreviation is not available for this locale.
     */
    /*public*/ String getISO2Country() throws MissingResourceException {
        if (country.length() == 0)
            return "";

        ResourceBundle resource = ResourceBundle.getBundle
                ("java.text.resources.LocaleElements", this);
        String localeID = resource.getString("LocaleString");
        String result = localeID.substring(3, 5);
        if (!result.equals(country))
            throw new MissingResourceException("Requested resource bundle not installed",
                "LocaleElements", "LocaleString");
        return result;
    }

    /**
     * Returns a name for the locale's language that is appropriate for display to the
     * user.  This will be the name the locale's language localized for the default locale,
     * if that data is available.  For example, if the locale is fr_FR and the default locale
     * is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and
     * the default locale is fr_FR, getDisplayLanguage() will return "anglais".  If the
     * appropriate name isn't available (say, we don't have a Japanese name for Croatian),
     * this function falls back on the English name and uses the ISO code as a last-resort
     * value.  If the locale doesn't specify a language, this function returns the empty string.
     */
    public final String getDisplayLanguage() {
        return getDisplayLanguage(getDefault());
    }

    /**
     * Returns a name for the locale's language that is appropriate for display to the
     * user.  This will be the name the locale's language localized for inLocale,
     * if that data is available.  For example, if the locale is fr_FR and inLocale
     * is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and
     * inLocale is fr_FR, getDisplayLanguage() will return "anglais".  If the
     * appropriate name isn't available (say, we don't have a Japanese name for Croatian),
     * this function falls back on the default locale, on the English name, and finally
     * on the ISO code as a last-resort value.  If the locale doesn't specify a language,
     * this function returns the empty string.
     */
    public String getDisplayLanguage(Locale inLocale) {
        String  langCode = language;
        if (langCode.length() == 0)
            return "";

        Locale  workingLocale = (Locale)inLocale.clone();
        String  result = null;
        int     phase = 0;
        boolean done = false;

        if (workingLocale.variant.length() == 0)
            phase = 1;
        if (workingLocale.country.length() == 0)
            phase = 2;

        while (!done) {
            try {
                ResourceBundle bundle = ResourceBundle.getBundle(
                    "java.text.resources.LocaleElements", workingLocale);
                result = findStringMatch((String[][])bundle.getObject("Languages"),
                                    langCode, langCode);
                if (result.length() != 0)
                    done = true;
            }
            catch (Exception e) {
                // just fall through
            }

            if (!done) {
                switch (phase) {
                    case 0:
                        workingLocale.variant = "";
                        break;

                    case 1:
                        workingLocale.country = "";
                        break;

                    case 2:
                        workingLocale = getDefault();
                        break;

                    case 3:
                        workingLocale = new Locale("", "", "");
                        break;

                    default:
                        return langCode;
                }
                phase++;
            }
        }
        return result;
    }

    /**
     * Returns a name for the locale's country that is appropriate for display to the
     * user.  This will be the name the locale's country localized for the default locale,
     * if that data is available.  For example, if the locale is fr_FR and the default locale
     * is en_US, getDisplayCountry() will return "France"; if the locale is en_US and
     * the default locale is fr_FR, getDisplayLanguage() will return "Etats-Unis".  If the
     * appropriate name isn't available (say, we don't have a Japanese name for Croatia),
     * this function falls back on the English name and uses the ISO code as a last-resort
     * value.  If the locale doesn't specify a country, this function returns the empty string.
     */
    public final String getDisplayCountry() {
        return getDisplayCountry(getDefault());
    }

    /**
     * Returns a name for the locale's country that is appropriate for display to the
     * user.  This will be the name the locale's country localized for inLocale,
     * if that data is available.  For example, if the locale is fr_FR and inLocale
     * is en_US, getDisplayCountry() will return "France"; if the locale is en_US and
     * inLocale is fr_FR, getDisplayLanguage() will return "Etats-Unis".  If the
     * appropriate name isn't available (say, we don't have a Japanese name for Croatia),
     * this function falls back on the default locale, on the English name, and finally
     * on the ISO code as a last-resort value.  If the locale doesn't specify a country,
     * this function returns the empty string.
     */
    public String getDisplayCountry(Locale inLocale) {
        String  ctryCode = country;
        if (ctryCode.length() == 0)
            return "";

        Locale  workingLocale = (Locale)inLocale.clone();
        String  result = null;
        int     phase = 0;
        boolean done = false;

        if (workingLocale.variant.length() == 0)
            phase = 1;
        if (workingLocale.country.length() == 0)
            phase = 2;

        while (!done) {
            try {
                ResourceBundle bundle = ResourceBundle.getBundle(
                    "java.text.resources.LocaleElements", workingLocale);
                result = findStringMatch((String[][])bundle.getObject("Countries"),
                                    ctryCode, ctryCode);
                if (result.length() != 0)
                    done = true;
            }
            catch (Exception e) {
                // just fall through
            }

            if (!done) {
                switch (phase) {
                    case 0:
                        workingLocale.variant = "";
                        break;

                    case 1:
                        workingLocale.country = "";
                        break;

                    case 2:
                        workingLocale = getDefault();
                        break;

                    case 3:
                        workingLocale = new Locale("", "", "");
                        break;

                    default:
                        return ctryCode;
                }
                phase++;
            }
        }
        return result;
    }

    /**
     * Returns a name for the locale's variant code that is appropriate for display to the
     * user.  If possible, the name will be localized for the default locale.  If the locale
     * doesn't specify a variant code, this function returns the empty string.
     */
    public final String getDisplayVariant() {
        return getDisplayVariant(getDefault());
    }

    /**
     * Returns a name for the locale's variant code that is appropriate for display to the
     * user.  If possible, the name will be localized for inLocale.  If the locale
     * doesn't specify a variant code, this function returns the empty string.
     */
    public String getDisplayVariant(Locale inLocale) {
        if (variant.length() == 0)
            return "";

        ResourceBundle bundle = ResourceBundle.getBundle(
                "java.text.resources.LocaleElements", inLocale);

        String names[] = getDisplayVariantArray(bundle);

        // Get the localized patterns for formatting a list, and use
        // them to format the list.
        String[] patterns;
        try {
            patterns = (String[])bundle.getObject("LocaleNamePatterns");
        }
        catch (MissingResourceException e) {
            patterns = null;
        }
        return formatList(patterns, names);
   }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?