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

The following are Jave code examples for showing how to use getVariant() of the java.util.Locale class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: airgram   File: LocaleController.java   View Source Code Vote up 7 votes
public static String getLocaleStringIso639() {
    Locale locale = getInstance().getSystemDefaultLocale();
    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 2
Project: fitnotifications   File: LocaleUtility.java   View Source Code Vote up 6 votes
/**
 * Fallback from the given locale name by removing the rightmost _-delimited
 * element. If there is none, return the root locale ("", "", ""). If this
 * is the root locale, return null. NOTE: The string "root" is not
 * recognized; do not use it.
 * 
 * @return a new Locale that is a fallback from the given locale, or null.
 */
public static Locale fallback(Locale loc) {

    // Split the locale into parts and remove the rightmost part
    String[] parts = new String[]
        { loc.getLanguage(), loc.getCountry(), loc.getVariant() };
    int i;
    for (i=2; i>=0; --i) {
        if (parts[i].length() != 0) {
            parts[i] = "";
            break;
        }
    }
    if (i<0) {
        return null; // All parts were empty
    }
    return new Locale(parts[0], parts[1], parts[2]);
}
 
Example 3
Project: openjdk-jdk10   File: VerifyLocale.java   View Source Code Vote up 6 votes
public static void main(String[] args) {
    String thisFile = "" +
        new java.io.File(System.getProperty("test.src", "."),
                         "VerifyLocale.java");

    for (Locale loc : Locale.getAvailableLocales()) {
        language = loc.getLanguage();
        country = loc.getCountry();
        variant = loc.getVariant();
        if (!language.equals("")) {
            String[] command_line = {
                // jumble the options in some weird order
                "-doclet", "VerifyLocale",
                "-locale", language + (country.equals("") ? "" : ("_" + country + (variant.equals("") ? "" : "_" + variant))),
                "-docletpath", System.getProperty("test.classes", "."),
                thisFile
            };
            if (jdk.javadoc.internal.tool.Main.execute(command_line) != 0)
                throw new Error("Javadoc encountered warnings or errors.");
        }
    }
}
 
Example 4
Project: PlusGram   File: LocaleController.java   View Source Code Vote up 6 votes
private String getLocaleString(Locale locale) {
    if (locale == null) {
        return "fa";
    }
    String languageCode = locale.getLanguage();
    String countryCode = locale.getCountry();
    String variantCode = locale.getVariant();
    if (languageCode.length() == 0 && countryCode.length() == 0) {
        return "fa";
    }
    StringBuilder result = new StringBuilder(11);
    result.append(languageCode);
    if (countryCode.length() > 0 || variantCode.length() > 0) {
        //result.append('-');
        result.append('_');
    }
    result.append(countryCode);
    if (variantCode.length() > 0) {
        result.append('_');
    }
    result.append(variantCode);
    return result.toString();
}
 
Example 5
Project: jdk8u-jdk   File: HostLocaleProviderAdapterImpl.java   View Source Code Vote up 6 votes
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 6
Project: incubator-netbeans   File: NbiProperties.java   View Source Code Vote up 5 votes
private String[] getLocaleParts(
        final Locale locale) {
    if (locale == null) {
        return new String[0];
    }
    
    if (!locale.getLanguage().equals("")) {
        if (!locale.getCountry().equals("")) {
            if (!locale.getVariant().equals("")) {
                return new String[] {
                    locale.getLanguage(),
                    locale.getCountry(),
                    locale.getVariant()
                };
            }
            
            return new String[] {
                locale.getLanguage(),
                locale.getCountry()
            };
        }
        
        return new String[] {
            locale.getLanguage()
        };
    }
    
    return new String[0];
}
 
Example 7
Project: OpenDiabetes   File: RefCapablePropertyResourceBundle.java   View Source Code Vote up 5 votes
private RefCapablePropertyResourceBundle(String baseName,
        PropertyResourceBundle wrappedBundle, ClassLoader loader) {
    this.baseName = baseName;
    this.wrappedBundle = wrappedBundle;
    Locale locale = wrappedBundle.getLocale();
    this.loader = loader;
    language = locale.getLanguage();
    country = locale.getCountry();
    variant = locale.getVariant();
    if (language.length() < 1) language = null;
    if (country.length() < 1) country = null;
    if (variant.length() < 1) variant = null;
}
 
Example 8
Project: solo-spring   File: AbstractPlugin.java   View Source Code Vote up 5 votes
/**
 * Processes languages. Retrieves language labels with default locale, then
 * sets them into the specified data model.
 *
 * @param dataModel
 *            the specified data model
 */
private void handleLangs(final Map<String, Object> dataModel) {
	final Locale locale = Latkes.getLocale();
	final String language = locale.getLanguage();
	final String country = locale.getCountry();
	final String variant = locale.getVariant();

	final StringBuilder keyBuilder = new StringBuilder(language);

	if (!StringUtils.isBlank(country)) {
		keyBuilder.append("_").append(country);
	}
	if (!StringUtils.isBlank(variant)) {
		keyBuilder.append("_").append(variant);
	}

	final String localKey = keyBuilder.toString();
	final Properties props = langs.get(localKey);

	if (null == props) {
		return;
	}

	final Set<Object> keySet = props.keySet();

	for (final Object key : keySet) {
		dataModel.put((String) key, props.getProperty((String) key));
	}
}
 
Example 9
Project: openjdk-jdk10   File: Bug4210525.java   View Source Code Vote up 5 votes
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 10
Project: configx   File: ConfigMessageSource.java   View Source Code Vote up 5 votes
/**
 * 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" -&gt; "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 11
Project: jdk8u-jdk   File: Bug4152725.java   View Source Code Vote up 5 votes
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 12
Project: openjdk-jdk10   File: ExecutableInputMethodManager.java   View Source Code Vote up 5 votes
private String createLocalePath(Locale locale) {
    String language = locale.getLanguage();
    String country = locale.getCountry();
    String variant = locale.getVariant();
    String localePath = null;
    if (!variant.equals("")) {
        localePath = "_" + language + "/_" + country + "/_" + variant;
    } else if (!country.equals("")) {
        localePath = "_" + language + "/_" + country;
    } else {
        localePath = "_" + language;
    }

    return localePath;
}
 
Example 13
Project: s-store   File: RefCapablePropertyResourceBundle.java   View Source Code Vote up 5 votes
private RefCapablePropertyResourceBundle(String baseName,
        PropertyResourceBundle wrappedBundle, ClassLoader loader) {
    this.baseName = baseName;
    this.wrappedBundle = wrappedBundle;
    Locale locale = wrappedBundle.getLocale();
    this.loader = loader;
    language = locale.getLanguage();
    country = locale.getCountry();
    variant = locale.getVariant();
    if (language.length() < 1) language = null;
    if (country.length() < 1) country = null;
    if (variant.length() < 1) variant = null;
}
 
Example 14
Project: Equella   File: LocaleUtils.java   View Source Code Vote up 5 votes
@SuppressWarnings("nls")
public static String toHtmlLang(Locale locale)
{
	String lang = locale.getLanguage();
	String country = locale.getCountry();
	String variant = locale.getVariant();
	return lang + (!Check.isEmpty(country) ? '-' + country : "") + (!Check.isEmpty(variant) ? '-' + variant : "");
}
 
Example 15
Project: Equella   File: LocaleData.java   View Source Code Vote up 5 votes
public LocaleData(Locale locale, boolean rightToLeft)
{
	this.locale = locale;
	this.rightToLeft = rightToLeft;

	language = locale.getLanguage();
	country = locale.getCountry();
	variant = locale.getVariant();
}
 
Example 16
Project: openjdk-jdk10   File: HostLocaleProviderAdapterImpl.java   View Source Code Vote up 5 votes
private static Locale stripVariantAndExtensions(Locale locale) {
    if (locale.hasExtensions() || locale.getVariant() != "") {
        // strip off extensions and variant.
        locale = new Locale.Builder()
                        .setLocale(locale)
                        .clearExtensions()
                        .build();
    }

    return locale;
}
 
Example 17
Project: jdk8u-jdk   File: HostLocaleProviderAdapterImpl.java   View Source Code Vote up 5 votes
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 18
Project: OpenJSharp   File: HostLocaleProviderAdapterImpl.java   View Source Code Vote up 5 votes
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 19
Project: dev-courses   File: RefCapablePropertyResourceBundle.java   View Source Code Vote up 5 votes
private RefCapablePropertyResourceBundle(String baseName,
        PropertyResourceBundle wrappedBundle, ClassLoader loader) {
    this.baseName = baseName;
    this.wrappedBundle = wrappedBundle;
    Locale locale = wrappedBundle.getLocale();
    this.loader = loader;
    language = locale.getLanguage();
    country = locale.getCountry();
    variant = locale.getVariant();
    if (language.length() < 1) language = null;
    if (country.length() < 1) country = null;
    if (variant.length() < 1) variant = null;
}
 
Example 20
Project: OpenJSharp   File: ExecutableInputMethodManager.java   View Source Code Vote up 5 votes
private String createLocalePath(Locale locale) {
    String language = locale.getLanguage();
    String country = locale.getCountry();
    String variant = locale.getVariant();
    String localePath = null;
    if (!variant.equals("")) {
        localePath = "_" + language + "/_" + country + "/_" + variant;
    } else if (!country.equals("")) {
        localePath = "_" + language + "/_" + country;
    } else {
        localePath = "_" + language;
    }

    return localePath;
}