Java Code Examples for java.util.Locale#getVariant()

The following examples show how to use java.util.Locale#getVariant() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TtsEngines.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * 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());
}
 
Example 2
Source File: HostLocaleProviderAdapterImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example 3
Source File: HostLocaleProviderAdapterImpl.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example 4
Source File: PropertyBundle.java    From Time4A with Apache License 2.0 6 votes vote down vote up
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();

}
 
Example 5
Source File: MessageHandler.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
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];
}
 
Example 6
Source File: PropertyFilesTest.java    From BiglyBT with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 7
Source File: LocaleController.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example 8
Source File: Bug4152725.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
        }
    }
 
Example 9
Source File: TGResourceBundle.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
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);
}
 
Example 10
Source File: Bug4210525.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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.");
    }
}
 
Example 11
Source File: HostLocaleProviderAdapterImpl.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
    }
}
 
Example 12
Source File: Bug4210525.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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.");
    }
}
 
Example 13
Source File: LocalesTest.java    From okta-sdk-java with Apache License 2.0 5 votes vote down vote up
@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)");
    }
}
 
Example 14
Source File: LocaleController.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example 15
Source File: Bug4210525.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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.");
    }
}
 
Example 16
Source File: HostLocaleProviderAdapterImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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);
    }
}
 
Example 17
Source File: ReloadableResourceBundleMessageSource.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 18
Source File: Bug4210525.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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.");
    }
}
 
Example 19
Source File: ReloadableResourceBundleMessageSource.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 20
Source File: CatalogServiceImpl.java    From azure-devops-intellij with MIT License 4 votes vote down vote up
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;
    }