📄 locale.java
字号:
this(language, "", ""); } /** * Gets the current value of the default locale for this instance * of the Java Virtual Machine. * <p> * The Java Virtual Machine sets the default locale during startup * based on the host environment. It is used by many locale-sensitive * methods if no locale is explicitly specified. * It can be changed using the * {@link #setDefault(java.util.Locale) setDefault} method. * * @return the default locale for this instance of the Java Virtual Machine */ public static Locale getDefault() { // do not synchronize this method - see 4071298 // it's OK if more than one default locale happens to be created if (defaultLocale == null) { String language, region, country, variant; language = (String) AccessController.doPrivileged( new GetPropertyAction("user.language", "en")); // for compatibility, check for old user.region property region = (String) AccessController.doPrivileged( new GetPropertyAction("user.region")); if (region != null) { // region can be of form country, country_variant, or _variant int i = region.indexOf('_'); if (i >= 0) { country = region.substring(0, i); variant = region.substring(i + 1); } else { country = region; variant = ""; } } else { country = (String) AccessController.doPrivileged( new GetPropertyAction("user.country", "")); variant = (String) AccessController.doPrivileged( new GetPropertyAction("user.variant", "")); } defaultLocale = new Locale(language, country, variant); } return defaultLocale; } /** * Sets the default locale for this instance of the Java Virtual Machine. * This does not affect the host locale. * <p> * If there is a security manager, its <code>checkPermission</code> * method is called with a <code>PropertyPermission("user.language", "write")</code> * permission before the default locale is changed. * <p> * The Java Virtual Machine sets the default locale during startup * based on the host environment. It is used by many locale-sensitive * methods if no locale is explicitly specified. * <p> * Since changing the default locale may affect many different areas * of functionality, this method should only be used if the caller * is prepared to reinitialize locale-sensitive code running * within the same Java Virtual Machine, such as the user interface. * * @throws SecurityException * if a security manager exists and its * <code>checkPermission</code> method doesn't allow the operation. * @throws NullPointerException if <code>newLocale</code> is null * @param newLocale the new default locale * @see SecurityManager#checkPermission * @see java.util.PropertyPermission */ public static synchronized void setDefault(Locale newLocale) { if (newLocale == null) throw new NullPointerException("Can't set default locale to NULL"); SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkPermission(new PropertyPermission ("user.language", "write")); defaultLocale = newLocale; } /** * Returns a list of all installed locales. */ public static Locale[] getAvailableLocales() { return LocaleData.getAvailableLocales("LocaleString"); } /** * Returns a list of all 2-letter country codes defined in ISO 3166. * Can be used to create Locales. */ public static String[] getISOCountries() { if (isoCountries == null) { isoCountries = new String[compressedIsoCountries.length() / 6]; for (int i = 0; i < isoCountries.length; i++) isoCountries[i] = compressedIsoCountries.substring((i * 6) + 1, (i * 6) + 3); } String[] result = new String[isoCountries.length]; System.arraycopy(isoCountries, 0, result, 0, isoCountries.length); return result; } /** * Returns a list of all 2-letter language codes defined in ISO 639. * Can be used to create Locales. * [NOTE: ISO 639 is not a stable standard-- some languages' codes have changed. * The list this function returns includes both the new and the old codes for the * languages whose codes have changed.] */ public static String[] getISOLanguages() { if (isoLanguages == null) { isoLanguages = new String[compressedIsoLanguages.length() / 6]; for (int i = 0; i < isoLanguages.length; i++) isoLanguages[i] = compressedIsoLanguages.substring((i * 6) + 1, (i * 6) + 3); } String[] result = new String[isoLanguages.length]; System.arraycopy(isoLanguages, 0, result, 0, isoLanguages.length); return result; } /** * Returns the language code for this locale, which will either be the empty string * or a lowercase ISO 639 code. * <p>NOTE: ISO 639 is not a stable standard-- some languages' codes have changed. * Locale's constructor recognizes both the new and the old codes for the languages * whose codes have changed, but this function always returns the old code. If you * want to check for a specific language whose code has changed, don't do <pre> * if (locale.getLanguage().equals("he") * ... * </pre>Instead, do<pre> * if (locale.getLanguage().equals(new Locale("he", "", "").getLanguage()) * ...</pre> * @see #getDisplayLanguage */ public String getLanguage() { return language; } /** * Returns the country/region code for this locale, which will either be the empty string * or an upercase ISO 3166 2-letter code. * @see #getDisplayCountry */ public String getCountry() { return country; } /** * Returns the variant code for this locale. * @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 upper case. * If the language is missing, the string will begin with an underbar. * If both the language and country fields are missing, this function * will return the empty string, even if the variant field is filled in * (you can't have a locale with just a variant-- the variant must accompany * a valid language or country code). * Examples: "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr__MAC" * @see #getDisplayName */ public final String toString() { boolean l = language.length() != 0; boolean c = country.length() != 0; boolean v = variant.length() != 0; StringBuffer result = new StringBuffer(language); if (c||(l&&v)) { result.append('_').append(country); // This may just append '_' } if (v&&(l||c)) { result.append('_').append(variant); } return result.toString(); } /** * Returns a three-letter abbreviation for this locale's language. If the locale * doesn't specify a language, this will be the empty string. Otherwise, this will * be a lowercase ISO 639-2/T language code. * The ISO 639-2 language codes can be found on-line at * <a href="ftp://dkuug.dk/i18n/iso-639-2.txt"><code>ftp://dkuug.dk/i18n/iso-639-2.txt</code></a> * @exception MissingResourceException Throws MissingResourceException if the * three-letter language abbreviation is not available for this locale. */ public String getISO3Language() throws MissingResourceException { int length = language.length(); if (length == 0) { return ""; } int index = compressedIsoLanguages.indexOf("," + language); if (index == -1 || length != 2) { throw new MissingResourceException("Couldn't find 3-letter language code for " + language, "LocaleElements_" + toString(), "ShortLanguage"); } return compressedIsoLanguages.substring(index + 3, index + 6); } /** * Returns a three-letter abbreviation for this locale's country. If the locale * doesn't specify a country, this will be tbe the empty string. Otherwise, this will * be an uppercase ISO 3166 3-letter country code. * @exception MissingResourceException Throws MissingResourceException if the * three-letter country abbreviation is not available for this locale. */ public String getISO3Country() throws MissingResourceException { int length = country.length(); if (length == 0) { return ""; } int index = compressedIsoCountries.indexOf("," + country); if (index == -1 || length != 2) { throw new MissingResourceException("Couldn't find 3-letter country code for " + country, "LocaleElements_" + toString(), "ShortCountry"); } return compressedIsoCountries.substring(index + 3, index + 6); } /** * Returns a name for the locale's language that is appropriate for display to the * user. * If possible, the name returned will be localized for the default locale. * 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 name returned cannot be localized for the default locale, * (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. * If possible, the name returned will be localized according to inLocale. * 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 name returned cannot be localized according to inLocale, * (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 = LocaleData.getLocaleElements(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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -