java.util.Locale#getVariant ( )源码实例Demo

下面列出了java.util.Locale#getVariant ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

private static boolean isSupportedCalendarLocale(Locale locale) {
    Locale base = locale;

    if (base.hasExtensions() || base.getVariant() != "") {
        base = new Locale.Builder()
                        .setLocale(locale)
                        .clearExtensions()
                        .build();
    }

    if (!supportedLocaleSet.contains(base)) {
        return false;
    }

    String requestedCalType = locale.getUnicodeLocaleType("ca");
    String nativeCalType =
        getCalendarID(base.toLanguageTag()).replaceFirst("gregorian", "gregory");

    if (requestedCalType == null) {
        return Calendar.getAvailableCalendarTypes().contains(nativeCalType);
    } else {
        return requestedCalType.equals(nativeCalType);
    }
}
 
源代码2 项目: android_9.0.0_r45   文件: TtsEngines.java
/**
 * This method tries its best to return a valid {@link Locale} object from the TTS-specific
 * Locale input (returned by {@link TextToSpeech#getLanguage}
 * and {@link TextToSpeech#getDefaultLanguage}). A TTS Locale language field contains
 * a three-letter ISO 639-2/T code (where a proper Locale would use a two-letter ISO 639-1
 * code), and the country field contains a three-letter ISO 3166 country code (where a proper
 * Locale would use a two-letter ISO 3166-1 code).
 *
 * This method tries to convert three-letter language and country codes into their two-letter
 * equivalents. If it fails to do so, it keeps the value from the TTS locale.
 */
public static Locale normalizeTTSLocale(Locale ttsLocale) {
    String language = ttsLocale.getLanguage();
    if (!TextUtils.isEmpty(language)) {
        String normalizedLanguage = sNormalizeLanguage.get(language);
        if (normalizedLanguage != null) {
            language = normalizedLanguage;
        }
    }

    String country = ttsLocale.getCountry();
    if (!TextUtils.isEmpty(country)) {
        String normalizedCountry= sNormalizeCountry.get(country);
        if (normalizedCountry != null) {
            country = normalizedCountry;
        }
    }
    return new Locale(language, country, ttsLocale.getVariant());
}
 
源代码3 项目: Time4A   文件: PropertyBundle.java
private static String toResourceName(
    String baseName,
    Locale locale
) {

    String language = LanguageMatch.getAlias(locale);
    String country = locale.getCountry();
    String variant = locale.getVariant();

    StringBuilder sb = new StringBuilder(baseName.length() + 20);
    sb.append(baseName.replace('.', '/'));

    if (!language.isEmpty()) {
        sb.append('_').append(language);
        if (!variant.isEmpty()) {
            sb.append('_').append(country).append('_').append(variant);
        } else if (!country.isEmpty()) {
            sb.append('_').append(country);
        }
    }

    return sb.append(".properties").toString();

}
 
源代码4 项目: RipplePower   文件: MessageHandler.java
static String bestFitFormat(Locale locale, Message[] messages) {
	String lang = locale.getLanguage();
	String country = locale.getCountry();
	String variant = locale.getVariant();

	String[] localeList = new String[] { "", lang, lang + "_" + country, lang + "_" + country + "_" + variant };

	String[] formats = new String[] { "", "", "", "", "", "" };
	int mostFit = 0;

	for (Message message : messages) {
		String localeStr = message.locale();
		for (int i = mostFit; i < localeList.length; i++) {
			if (localeList[i].equals(localeStr)) {
				mostFit++;
				formats[mostFit] = message.value();
				break;
			}
		}
	}

	return formats[mostFit];
}
 
源代码5 项目: BiglyBT   文件: PropertyFilesTest.java
private static String resourceBundleSuffixFor(Locale locale) {
	String suffix = "";
	if (Locale.ROOT != locale) {
		if (!locale.getLanguage().isEmpty()) {
			suffix += "_" + locale.getLanguage();
		}
		if (!locale.getScript().isEmpty()) {
			suffix += "_" + locale.getScript();
		}
		if (!locale.getCountry().isEmpty()) {
			suffix += "_" + locale.getCountry();
		}
		if (!locale.getVariant().isEmpty()) {
			suffix += "_" + locale.getVariant();
		}
	}
	suffix += ".properties";
	return suffix;
}
 
源代码6 项目: jdk8u-jdk   文件: HostLocaleProviderAdapterImpl.java
private static boolean isSupportedCalendarLocale(Locale locale) {
    Locale base = locale;

    if (base.hasExtensions() || base.getVariant() != "") {
        base = new Locale.Builder()
                        .setLocale(locale)
                        .clearExtensions()
                        .build();
    }

    if (!supportedLocaleSet.contains(base)) {
        return false;
    }

    String requestedCalType = locale.getUnicodeLocaleType("ca");
    String nativeCalType =
        getCalendarID(base.toLanguageTag()).replaceFirst("gregorian", "gregory");

    if (requestedCalType == null) {
        return Calendar.getAvailableCalendarTypes().contains(nativeCalType);
    } else {
        return requestedCalType.equals(nativeCalType);
    }
}
 
/**
 * Calculate the filenames for the given bundle basename and Locale,
 * appending language code, country code, and variant code.
 * E.g.: basename "messages", Locale "de_AT_oo" -> "messages_de_AT_OO",
 * "messages_de_AT", "messages_de".
 * <p>Follows the rules defined by {@link java.util.Locale#toString()}.
 * @param basename the basename of the bundle
 * @param locale the locale
 * @return the List of filenames to check
 */
protected List<String> calculateFilenamesForLocale(String basename, Locale locale) {
	List<String> result = new ArrayList<String>(3);
	String language = locale.getLanguage();
	String country = locale.getCountry();
	String variant = locale.getVariant();
	StringBuilder temp = new StringBuilder(basename);

	temp.append('_');
	if (language.length() > 0) {
		temp.append(language);
		result.add(0, temp.toString());
	}

	temp.append('_');
	if (country.length() > 0) {
		temp.append(country);
		result.add(0, temp.toString());
	}

	if (variant.length() > 0 && (language.length() > 0 || country.length() > 0)) {
		temp.append('_').append(variant);
		result.add(0, temp.toString());
	}

	return result;
}
 
源代码8 项目: jdk8u_jdk   文件: Bug4210525.java
public static void main(String[] args) throws Exception {
    String language = "en";
    String country = "US";
    String variant = "socal";

    Locale aLocale = new Locale(language, country, variant);

    String localeVariant = aLocale.getVariant();
    if (localeVariant.equals(variant)) {
        System.out.println("passed");
    } else {
        System.out.println("failed");
        throw new Exception("Bug4210525 test failed.");
    }
}
 
/**
 * Calculate the filenames for the given bundle basename and Locale,
 * appending language code, country code, and variant code.
 * E.g.: basename "messages", Locale "de_AT_oo" -> "messages_de_AT_OO",
 * "messages_de_AT", "messages_de".
 * <p>Follows the rules defined by {@link java.util.Locale#toString()}.
 * @param basename the basename of the bundle
 * @param locale the locale
 * @return the List of filenames to check
 */
protected List<String> calculateFilenamesForLocale(String basename, Locale locale) {
	List<String> result = new ArrayList<String>(3);
	String language = locale.getLanguage();
	String country = locale.getCountry();
	String variant = locale.getVariant();
	StringBuilder temp = new StringBuilder(basename);

	temp.append('_');
	if (language.length() > 0) {
		temp.append(language);
		result.add(0, temp.toString());
	}

	temp.append('_');
	if (country.length() > 0) {
		temp.append(country);
		result.add(0, temp.toString());
	}

	if (variant.length() > 0 && (language.length() > 0 || country.length() > 0)) {
		temp.append('_').append(variant);
		result.add(0, temp.toString());
	}

	return result;
}
 
源代码10 项目: openjdk-8   文件: HostLocaleProviderAdapterImpl.java
private static boolean isSupportedCalendarLocale(Locale locale) {
    Locale base = locale;

    if (base.hasExtensions() || base.getVariant() != "") {
        // strip off extensions and variant.
        base = new Locale.Builder()
                        .setLocale(locale)
                        .clearExtensions()
                        .build();
    }

    if (!supportedLocaleSet.contains(base)) {
        return false;
    }

    int calid = getCalendarID(base.toLanguageTag());
    if (calid <= 0 || calid >= calIDToLDML.length) {
        return false;
    }

    String requestedCalType = locale.getUnicodeLocaleType("ca");
    String nativeCalType = calIDToLDML[calid]
            .replaceFirst("_.*", ""); // remove locale part.

    if (requestedCalType == null) {
        return Calendar.getAvailableCalendarTypes().contains(nativeCalType);
    } else {
        return requestedCalType.equals(nativeCalType);
    }
}
 
源代码11 项目: openjdk-8-source   文件: Bug4210525.java
public static void main(String[] args) throws Exception {
    String language = "en";
    String country = "US";
    String variant = "socal";

    Locale aLocale = new Locale(language, country, variant);

    String localeVariant = aLocale.getVariant();
    if (localeVariant.equals(variant)) {
        System.out.println("passed");
    } else {
        System.out.println("failed");
        throw new Exception("Bug4210525 test failed.");
    }
}
 
源代码12 项目: TelePlus-Android   文件: LocaleController.java
private String getLocaleString(Locale locale)
{
    if (locale == null)
    {
        return "en";
    }
    String languageCode = locale.getLanguage();
    String countryCode = locale.getCountry();
    String variantCode = locale.getVariant();
    if (languageCode.length() == 0 && countryCode.length() == 0)
    {
        return "en";
    }
    StringBuilder result = new StringBuilder(11);
    result.append(languageCode);
    if (countryCode.length() > 0 || variantCode.length() > 0)
    {
        result.append('_');
    }
    result.append(countryCode);
    if (variantCode.length() > 0)
    {
        result.append('_');
    }
    result.append(variantCode);
    return result.toString();
}
 
源代码13 项目: okta-sdk-java   文件: LocalesTest.java
@Test
public void testParseAllLocales() {
    final Locale[] locales = Locale.getAvailableLocales();
    int failures = 0;
    for (final Locale l : locales) {
        // Check if it's possible to recreate the Locale using just the standard constructor
        final Locale locale = new Locale(l.getLanguage(), l.getCountry(), l.getVariant());
        if (l.equals(locale)) { // it is possible for LocaleUtils.toLocale to handle these Locales
            String str = l.toString();
            // Look for the script/extension suffix
            int suff = str.indexOf("_#");
            if (suff == - 1) {
                suff = str.indexOf("#");
            }
            if (suff >= 0) { // we have a suffix
                try {
                    Locales.toLocale(str); // should cause IAE
                    System.out.println("Should not have parsed: " + str);
                    failures++;
                    continue; // try next Locale
                } catch (final IllegalArgumentException iae) {
                    // expected; try without suffix
                    str = str.substring(0, suff);
                }
            }
            final Locale loc = Locales.toLocale(str);
            if (!l.equals(loc)) {
                System.out.println("Failed to parse: " + str);
                failures++;
            }
        }
    }
    if (failures > 0) {
        fail("Failed "+failures+" test(s)");
    }
}
 
源代码14 项目: jdk8u60   文件: Bug4210525.java
public static void main(String[] args) throws Exception {
    String language = "en";
    String country = "US";
    String variant = "socal";

    Locale aLocale = new Locale(language, country, variant);

    String localeVariant = aLocale.getVariant();
    if (localeVariant.equals(variant)) {
        System.out.println("passed");
    } else {
        System.out.println("failed");
        throw new Exception("Bug4210525 test failed.");
    }
}
 
源代码15 项目: jdk8u_jdk   文件: HostLocaleProviderAdapterImpl.java
private static boolean isSupportedCalendarLocale(Locale locale) {
    Locale base = locale;

    if (base.hasExtensions() || base.getVariant() != "") {
        // strip off extensions and variant.
        base = new Locale.Builder()
                        .setLocale(locale)
                        .clearExtensions()
                        .build();
    }

    if (!supportedLocaleSet.contains(base)) {
        return false;
    }

    int calid = getCalendarID(base.toLanguageTag());
    if (calid <= 0 || calid >= calIDToLDML.length) {
        return false;
    }

    String requestedCalType = locale.getUnicodeLocaleType("ca");
    String nativeCalType = calIDToLDML[calid]
            .replaceFirst("_.*", ""); // remove locale part.

    if (requestedCalType == null) {
        return Calendar.getAvailableCalendarTypes().contains(nativeCalType);
    } else {
        return requestedCalType.equals(nativeCalType);
    }
}
 
源代码16 项目: hottub   文件: Bug4210525.java
public static void main(String[] args) throws Exception {
    String language = "en";
    String country = "US";
    String variant = "socal";

    Locale aLocale = new Locale(language, country, variant);

    String localeVariant = aLocale.getVariant();
    if (localeVariant.equals(variant)) {
        System.out.println("passed");
    } else {
        System.out.println("failed");
        throw new Exception("Bug4210525 test failed.");
    }
}
 
源代码17 项目: tuxguitar   文件: TGResourceBundle.java
public static TGResourceBundle getBundle(TGContext context, String baseName, Locale locale){
	Properties properties = new Properties();
	
	String bundleName = baseName.replace('.','/');
	String bundleExtension = ".properties";
	
	// load default
	TGResourceBundle.loadResources(context, (bundleName + bundleExtension ), properties);
	
	// load language
	bundleName += "_";
	if(locale.getLanguage() != null && locale.getLanguage().length() > 0){
		bundleName += locale.getLanguage();
		TGResourceBundle.loadResources(context, (bundleName + bundleExtension ), properties);
	}
	
	// load country
	bundleName += "_";
	if(locale.getCountry() != null && locale.getCountry().length() > 0){
		bundleName += locale.getCountry();
		TGResourceBundle.loadResources(context, (bundleName + bundleExtension ), properties);
	}
	
	// load variant
	bundleName += "_";
	if(locale.getVariant() != null && locale.getVariant().length() > 0){
		bundleName += locale.getVariant();
		TGResourceBundle.loadResources(context, (bundleName + bundleExtension ), properties);
	}
	
	return new TGResourceBundle(locale, properties);
}
 
源代码18 项目: jdk8u_jdk   文件: Bug4152725.java
public static void main(String[] args) {

        if (args.length != 1) {
            throw new RuntimeException("expected locale needs to be specified");
        }

        Locale locale = Locale.getDefault();

        // don't use Locale.toString - it's bogus
        String language = locale.getLanguage();
        String country = locale.getCountry();
        String variant = locale.getVariant();
        String localeID = null;
        if (variant.length() > 0) {
            localeID = language + "_" + country + "_" + variant;
        } else if (country.length() > 0) {
            localeID = language + "_" + country;
        } else {
            localeID = language;
        }

        if (localeID.equals(args[0])) {
            System.out.println("Correctly set from command line: " + localeID);
        } else {
            throw new RuntimeException("expected default locale: " + args[0]
                    + ", actual default locale: " + localeID);
        }
    }
 
源代码19 项目: TelePlus-Android   文件: LocaleController.java
private String getLocaleString(Locale locale)
{
    if (locale == null)
    {
        return "en";
    }
    String languageCode = locale.getLanguage();
    String countryCode = locale.getCountry();
    String variantCode = locale.getVariant();
    if (languageCode.length() == 0 && countryCode.length() == 0)
    {
        return "en";
    }
    StringBuilder result = new StringBuilder(11);
    result.append(languageCode);
    if (countryCode.length() > 0 || variantCode.length() > 0)
    {
        result.append('_');
    }
    result.append(countryCode);
    if (variantCode.length() > 0)
    {
        result.append('_');
    }
    result.append(variantCode);
    return result.toString();
}
 
源代码20 项目: azure-devops-intellij   文件: CatalogServiceImpl.java
private static String localeToRFC5646LanguageTag(final Locale locale) throws IllegalArgumentException {

        // language[-variant][-region]

        String result = locale.getLanguage();

        if (locale.getVariant().length() > 0) {
            result = result + "-" + locale.getVariant(); //$NON-NLS-1$
        }

        if (locale.getCountry().length() > 0) {
            result = result + "-" + locale.getCountry(); //$NON-NLS-1$
        }

        return result;
    }