Example usage for java.util Locale getScript

List of usage examples for java.util Locale getScript

Introduction

In this page you can find the example usage for java.util Locale getScript.

Prototype

public String getScript() 

Source Link

Document

Returns the script for this locale, which should either be the empty string or an ISO 15924 4-letter script code.

Usage

From source file:Main.java

public static void main(String[] args) {
    Locale locale = Locale.CANADA_FRENCH;

    System.out.println("Locale1:" + locale);

    System.out.println(locale.getScript());

}

From source file:net.pms.util.Languages.java

private static String localeToLanguageCode(Locale locale) {
    /*/*from   ww w.j  a  va 2s  . c o m*/
     * This might seem redundant, but a language can also contain a
     * country/region and a variant. Stating that e.g language
     * "ar" should return "ar" means that "messages_ar.properties"
     * will be used for any country/region and variant of Arabic.
     * This should be true until UMS contains multiple dialects of Arabic,
     * in which case different codes would have to be returned for the
     * different dialects.
     */

    if (locale == null) {
        return null;
    }
    String languageCode = locale.getLanguage();
    if (languageCode != null && !languageCode.isEmpty()) {
        switch (languageCode) {
        case "en":
            if (locale.getCountry().equalsIgnoreCase("GB")) {
                return "en-GB";
            } else {
                return "en-US";
            }
        case "pt":
            if (locale.getCountry().equalsIgnoreCase("BR")) {
                return "pt-BR";
            } else {
                return "pt";
            }
        case "nb":
        case "nn":
            return "no";
        case "cmn":
        case "zh":
            if (locale.getScript().equalsIgnoreCase("Hans")) {
                return "zh-Hans";
            } else if (locale.getCountry().equalsIgnoreCase("CN")
                    || locale.getCountry().equalsIgnoreCase("SG")) {
                return "zh-Hans";
            } else {
                return "zh-Hant";
            }
        default:
            return languageCode;
        }
    } else {
        return null;
    }
}

From source file:org.azyva.dragom.cliutil.CliUtil.java

/**
 * Returns the version of a text resource appropriate for the current default
 * Locale./* w  w w. java 2 s.c  o  m*/
 *
 * <p>A Reader is returned so that character encoding is taken into consideration.
 * The text resource is assumed to be encoded with UTF-8, regardless of the
 * platform default encoding.
 *
 * <p>The algorithm used for selecting the appropriate resource is similar to the
 * one implemented by ResourceBundle.getBundle.
        
 * <p>The resource base name is split on the last ".", if any, and the candidate
 * variants are inserted before it.
 *
 * @param clazz Class to which the resource belongs.
 * @param resourceBaseName Base name of the resource.
 * @return Resource as an InputStream, just as Class.getResourceAsStream would
 *   return. null if no resource version exists.
 */
public static Reader getLocalizedTextResourceReader(Class<?> clazz, String resourceBaseName) {
    int indexDot;
    String resourceBaseNamePrefix;
    String resourceBaseNameSuffix;
    Locale locale;
    String[] arrayCandidate;

    indexDot = resourceBaseName.lastIndexOf('.');

    if (indexDot != -1) {
        resourceBaseNamePrefix = resourceBaseName.substring(0, indexDot);
        resourceBaseNameSuffix = resourceBaseName.substring(indexDot);
    } else {
        resourceBaseNamePrefix = resourceBaseName;
        resourceBaseNameSuffix = "";
    }

    locale = Locale.getDefault();

    arrayCandidate = new String[7];

    arrayCandidate[0] = resourceBaseNamePrefix + "_" + locale.getLanguage() + "_" + locale.getScript() + "_"
            + locale.getCountry() + "_" + locale.getVariant();
    arrayCandidate[1] = resourceBaseNamePrefix + "_" + locale.getLanguage() + "_" + locale.getScript() + "_"
            + locale.getCountry();
    arrayCandidate[2] = resourceBaseNamePrefix + "_" + locale.getLanguage() + "_" + locale.getScript();
    arrayCandidate[3] = resourceBaseNamePrefix + "_" + locale.getLanguage() + "_" + locale.getCountry() + "_"
            + locale.getVariant();
    arrayCandidate[4] = resourceBaseNamePrefix + "_" + locale.getLanguage() + "_" + locale.getCountry();
    arrayCandidate[5] = resourceBaseNamePrefix + "_" + locale.getLanguage();
    arrayCandidate[6] = resourceBaseNamePrefix;

    for (String candidate : arrayCandidate) {
        if (!candidate.endsWith("_")) {
            InputStream inputStreamResource;

            inputStreamResource = clazz.getResourceAsStream(candidate + resourceBaseNameSuffix);

            if (inputStreamResource != null) {
                try {
                    return new InputStreamReader(inputStreamResource, "UTF-8");
                } catch (UnsupportedEncodingException uee) {
                    throw new RuntimeException(uee);
                }
            }
        }
    }

    return null;
}

From source file:org.jahia.utils.LanguageCodeConverters.java

private static boolean isSupportedLocale(Locale l) {
    // we do not support locales with variants
    // we do not support scripts
    return StringUtils.isEmpty(l.getVariant()) && (StringUtils.isEmpty(l.getScript()));
}

From source file:org.primeframework.mvc.control.form.LocaleSelect.java

/**
 * Adds the countries Map and then calls super.
 *///from  www.j av a 2 s.c  o  m
@Override
protected Map<String, Object> makeParameters() {
    LinkedHashMap<String, String> locales = new LinkedHashMap<>();
    String preferred = (String) attributes.get("preferredLocales");
    if (preferred != null) {
        String[] parts = preferred.split(",");
        for (String part : parts) {
            Locale locale = LocaleUtils.toLocale(part);
            locales.put(locale.toString(), locale.getDisplayName(locale));
        }
    }

    boolean includeCountries = attributes.containsKey("includeCountries")
            ? (Boolean) attributes.get("includeCountries")
            : true;
    List<Locale> allLocales = new ArrayList<>();
    Collections.addAll(allLocales, Locale.getAvailableLocales());
    allLocales.removeIf((locale) -> locale.getLanguage().isEmpty() || locale.hasExtensions()
            || !locale.getScript().isEmpty() || !locale.getVariant().isEmpty()
            || (!includeCountries && !locale.getCountry().isEmpty()));
    allLocales.sort((one, two) -> one.getDisplayName(locale).compareTo(two.getDisplayName(locale)));

    for (Locale locale : allLocales) {
        if (!locales.containsKey(locale.getCountry())) {
            locales.put(locale.toString(), locale.getDisplayName(this.locale));
        }
    }

    attributes.put("items", locales);

    return super.makeParameters();
}