Example usage for java.util Locale getISOLanguages

List of usage examples for java.util Locale getISOLanguages

Introduction

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

Prototype

public static String[] getISOLanguages() 

Source Link

Document

Returns a list of all 2-letter language codes defined in ISO 639.

Usage

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    String[] languages = Locale.getISOLanguages();

    System.out.println("\nLanguages:\n");
    for (int i = 0; i < languages.length; i++)
        System.out.println(languages[i]);
}

From source file:CountryLanguageCodes.java

public static void main(String[] argv) {

    String[] countries = Locale.getISOCountries();
    String[] languages = Locale.getISOLanguages();
    int i;//  w w  w  . j  a  va  2 s.  c om

    System.out.println("\nCountries:\n");
    for (i = 0; i < countries.length; i++)
        System.out.println(countries[i]);
    System.out.println("\nLanguages:\n");
    for (i = 0; i < languages.length; i++)
        System.out.println(languages[i]);
}

From source file:Main.java

public static void main(String[] args) {
    Locale locale = new Locale("en", "US", "WIN");

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

    // get ISO languages
    String[] languages = Locale.getISOLanguages();

    // print countries
    System.out.println("Languages are:");
    for (int i = 0; i < languages.length; i++) {
        System.out.println(i + ":" + languages[i]);
    }// w  w  w . j  a va2s  .c om
}

From source file:com.carlomicieli.jtrains.validation.ISOValidationUtils.java

/**
 * Returns {@code true} if the value is a valid 2-letter language code as defined in ISO 639.
 * @param lang the language code//from   www  . j a  v  a 2s . co  m
 * @return {@code true} if the value is a valid language; {@code false} otherwise
 */
public static boolean languageIsValid(String lang) {
    if (StringUtils.isBlank(lang)) {
        return true;
    }
    return Arrays.binarySearch(Locale.getISOLanguages(), lang.toLowerCase()) >= 0;
}

From source file:com.michellemay.mappings.ISO639Alpha2Mapping.java

/**
 * Instantiates a new ISO 639 alpha 2 mapping.
 *//*  w w  w. j a v  a 2 s . c  o m*/
public ISO639Alpha2Mapping() {
    super(NAME);

    // Build reverse map
    HashMap<String, Locale> map = new HashMap<String, Locale>();
    for (String isoCode : Locale.getISOLanguages()) {
        if (isoCode.length() > 0) {
            String displayValue = isoCode.toLowerCase();
            if (!map.containsKey(displayValue)) {
                map.put(displayValue, LocaleUtils.toLocale(isoCode));
            }
        }
    }
    this.withMapping(map).withCaseSensitive(false);
}

From source file:com.michellemay.mappings.DisplayNamesMapping.java

/**
 * Instantiates a new Display names mapping.
 *
 * @param name the name/*from  w ww  . j a  va2s .  c  om*/
 * @param displayLocale the display locale
 */
public DisplayNamesMapping(String name, Locale displayLocale) {
    super(name);
    this.displayLocale = displayLocale;

    // Build reverse map
    HashMap<String, Locale> map = new HashMap<String, Locale>();
    for (String isoCode : Locale.getISOLanguages()) {
        Locale loc = LocaleUtils.toLocale(isoCode);
        String displayName = loc.getDisplayName(displayLocale).toLowerCase();
        if (isoCode.length() > 0 && !map.containsKey(displayName)) {
            map.put(displayName, loc);
        }
    }

    this.withMapping(map).withCaseSensitive(false);
}

From source file:com.trenako.validation.ISOLanguageValidator.java

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
    if (StringUtils.isBlank(value)) {
        return true;
    }/*w ww  . j  a  v a 2 s .co  m*/
    return Arrays.binarySearch(Locale.getISOLanguages(), value.toLowerCase()) >= 0;
}

From source file:org.cee.service.language.LanguageService.java

public LanguageList getSupportedLanguages() {
    List<String> langIds = searchService.getSupportedLanguages();
    if (langIds == null) {
        //search service does not support languages at all, provide user with full list of languages
        langIds = Arrays.asList(Locale.getISOLanguages());
    }//from   w w  w  .  j a va  2 s  .c  o  m
    List<EntityKey> languages = new ArrayList<EntityKey>();
    int defaultLanguageIndex = 0;
    String defaultLanguage = "en";
    Locale currentUserLocale = LocaleContextHolder.getLocaleContext().getLocale();
    if (currentUserLocale != null) {
        defaultLanguage = currentUserLocale.getLanguage();
    }
    for (String langId : langIds) {
        Locale locale = Locale.forLanguageTag(langId);
        if (locale != null) {
            if (locale.getLanguage().startsWith(defaultLanguage)
                    || defaultLanguage.startsWith(locale.getLanguage())) {
                defaultLanguageIndex = languages.size();
            }
            languages.add(EntityKey.get(locale.getDisplayLanguage(), langId));
        } else {
            languages.add(EntityKey.get(langId, langId));
        }
    }
    LanguageList languageList = new LanguageList();
    languageList.setLanguages(languages);
    languageList.setDefaultLanguage(defaultLanguageIndex);
    return languageList;
}

From source file:de.cosmocode.commons.converter.LocaleLanguageIsoConverter.java

@Override
public String toTwoLetter(String iso6392) {
    // sanity check on arguments
    Preconditions.checkNotNull(iso6392, "iso6392 must not be null");
    if (Patterns.ISO_639_1.matcher(iso6392).matches()) {
        // already ISO 639-1 (two letter)
        return iso6392;
    } else if (StringUtils.isBlank(iso6392)) {
        // this is here for convenience, to allow empty languages
        return TrimMode.EMPTY.apply(iso6392);
    }/*w  w w.  j a v  a 2 s.c o m*/
    Preconditions.checkArgument(Patterns.ISO_639_2.matcher(iso6392).matches(),
            "Language Code %s not in ISO 639-2", iso6392);

    // try to get two letter code from cache
    final String fromCache = cache.get(iso6392);
    if (fromCache != null) {
        return fromCache;
    }

    // search for three letter code in the known language codes of Locale
    for (final String iso6391 : Locale.getISOLanguages()) {
        final String threeLetter = new Locale(iso6391).getISO3Language();
        if (iso6392.equals(threeLetter)) {
            LOG.trace("Found ISO 639-1 language code: {} for ISO 639-2 language code: {}", iso6391, iso6392);
            cache.put(iso6392, iso6391);
            return iso6391;
        }
    }

    // if we arrive here then the Locale class could not find the iso3 code
    throw new IsoConversionException("No known ISO 639-1 language code for " + iso6392);
}

From source file:sk.openhouse.web.recaptcha.LocaleReCaptchaImpl.java

@Override
public String createRecaptchaHtml(String errorMessage, Properties options) {

    String errorPart = "";
    if (errorMessage != null) {
        String encoding = "UTF-8";
        try {/*w  w w . j  a v a 2 s  .c  om*/
            errorPart = "&amp;error=" + URLEncoder.encode(errorMessage, encoding);
        } catch (UnsupportedEncodingException ex) {
            logger.fatal(encoding + " encoding is not supported", ex);
        }
    }

    if (options == null) {
        options = new Properties();
    }

    String prefix = "reCaptcha.";
    /* message key with default value */
    Map<String, String> keys = new HashMap<String, String>();
    keys.put("visual_challenge", "Get a visual challenge");
    keys.put("audio_challenge", "Get an audio challenge");
    keys.put("refresh_btn", "Get a new challenge");
    keys.put("instructions_visual", "Type the two words:");
    keys.put("instructions_context", "Type the words in the boxes:");
    keys.put("instructions_audio", "Type what you hear:");
    keys.put("help_btn", "Help");
    keys.put("play_again", "Play sound again");
    keys.put("cant_hear_this", "Download sound as MP3");
    keys.put("incorrect_try_again", "Incorrect. Try again.");

    /* get locale from properties if set */
    Locale locale = null;
    String langKey = "lang";
    if (options.containsKey(langKey)) {
        List<String> langs = Arrays.asList(Locale.getISOLanguages());
        String lang = options.getProperty(langKey);
        if (langs.contains(lang)) {
            locale = new Locale(lang);
        }
    }

    /* if locale is not set, get it from context */
    if (locale == null) {
        locale = LocaleContextHolder.getLocale();
    }

    /* load messages for a given locale, if it fails, default to EN */
    StringBuilder sb = new StringBuilder("{");
    try {
        for (Map.Entry<String, String> entry : keys.entrySet()) {
            String key = entry.getKey();
            sb.append(key);
            sb.append(":\"");
            sb.append(messages.getMessage(prefix + key, locale));
            sb.append("\",");
        }
    } catch (NoSuchMessageException ex) {
        locale = Locale.ENGLISH;
        for (Map.Entry<String, String> entry : keys.entrySet()) {
            String key = entry.getKey();
            sb.append(key);
            sb.append(":\"");
            sb.append(messages.getMessage(prefix + key, entry.getValue(), locale));
            sb.append("\",");
        }
    }
    sb.deleteCharAt(sb.length() - 1);
    sb.append("}");

    options.put("custom_translations", sb.toString());
    options.put("lang", locale.getLanguage());

    String message = fetchJSOptions(options);

    message += "<script type=\"text/javascript\" src=\"" + recaptchaServer + "/challenge?k=" + publicKey
            + errorPart + "\"></script>\r\n";

    if (includeNoscript) {
        String noscript = "<noscript>\r\n" + "   <iframe src=\"" + recaptchaServer + "/noscript?k=" + publicKey
                + errorPart + "\" height=\"300\" width=\"500\" frameborder=\"0\"></iframe><br>\r\n"
                + "   <textarea name=\"recaptcha_challenge_field\" rows=\"3\" cols=\"40\"></textarea>\r\n"
                + "   <input type=\"hidden\" name=\"recaptcha_response_field\" value=\"manual_challenge\">\r\n"
                + "</noscript>";
        message += noscript;
    }

    return message;
}