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

The following examples show how to use java.util.Locale#getISOLanguages() . These examples are extracted from open source projects. 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 Project: MediaSDK   File: Util.java    License: Apache License 2.0 6 votes vote down vote up
private static HashMap<String, String> createIso3ToIso2Map() {
  String[] iso2Languages = Locale.getISOLanguages();
  HashMap<String, String> iso3ToIso2 =
      new HashMap<>(
          /* initialCapacity= */ iso2Languages.length + iso3BibliographicalToIso2.length);
  for (String iso2 : iso2Languages) {
    try {
      // This returns the ISO 639-2/T code for the language.
      String iso3 = new Locale(iso2).getISO3Language();
      if (!TextUtils.isEmpty(iso3)) {
        iso3ToIso2.put(iso3, iso2);
      }
    } catch (MissingResourceException e) {
      // Shouldn't happen for list of known languages, but we don't want to throw either.
    }
  }
  // Add additional ISO 639-2/B codes to mapping.
  for (int i = 0; i < iso3BibliographicalToIso2.length; i += 2) {
    iso3ToIso2.put(iso3BibliographicalToIso2[i], iso3BibliographicalToIso2[i + 1]);
  }
  return iso3ToIso2;
}
 
Example 2
public static void main (String[] args) throws Exception {
    Locale es = new Locale ("es");
    String[] isoLangs = es.getISOLanguages ();
    String error = "";

    for (int i = 0; i < isoLangs.length; i++) {
        Locale current = new Locale (isoLangs[i]);
        String localeString = current.getDisplayLanguage (es);
        String startLetter = localeString.substring (0,1);
        if (!startLetter.toLowerCase (es).equals (startLetter)){
            error = error + "\n\t"+ isoLangs[i] + " " + localeString;
        }
    }

    if (error.length () > 0){
        throw new Exception ("\nFollowing language names starts with upper-case letter: "
                + error + "\nLower-case expected!");
    }

}
 
Example 3
/**
 * @bug 4126880
 */
void Test4126880() {
    String[] test;

    test = Locale.getISOCountries();
    test[0] = "SUCKER!!!";
    test = Locale.getISOCountries();
    if (test[0].equals("SUCKER!!!"))
        errln("Changed internal country code list!");

    test = Locale.getISOLanguages();
    test[0] = "HAHAHAHA!!!";
    test = Locale.getISOLanguages();
    if (test[0].equals("HAHAHAHA!!!")) // Fixed typo
        errln("Changes internal language code list!");
}
 
Example 4
Source Project: j2objc   File: LocaleTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * java.util.Locale#getISOLanguages()
 */
public void test_getISOLanguages() {
    // Test for method java.lang.String []
    // java.util.Locale.getISOLanguages()
    // Assumes always at least 131 ISOlanguages...
    String[] isoLang = Locale.getISOLanguages();
    int length = isoLang.length;

    // BEGIN android-changed
    // Language codes are 2- and 3-letter, with preference given
    // to 2-letter codes where possible. 3-letter codes are used
    // when lack a 2-letter equivalent.
    assertTrue("Random element in wrong format.",
               (isoLang[length / 2].length() == 2 || isoLang[length / 2].length() == 3)
               && isoLang[length / 2].toLowerCase().equals(isoLang[length / 2]));
    // END android-changed

    assertTrue("Wrong number of ISOLanguages.", length > 130);
}
 
Example 5
public static void main (String[] args) throws Exception {
    Locale es = new Locale ("es");
    String[] isoLangs = es.getISOLanguages ();
    String error = "";

    for (int i = 0; i < isoLangs.length; i++) {
        Locale current = new Locale (isoLangs[i]);
        String localeString = current.getDisplayLanguage (es);
        String startLetter = localeString.substring (0,1);
        if (!startLetter.toLowerCase (es).equals (startLetter)){
            error = error + "\n\t"+ isoLangs[i] + " " + localeString;
        }
    }

    if (error.length () > 0){
        throw new Exception ("\nFollowing language names starts with upper-case letter: "
                + error + "\nLower-case expected!");
    }

}
 
Example 6
Source Project: Mizuu   File: IdentifyTvShowFragment.java    License: Apache License 2.0 6 votes vote down vote up
public LanguageAdapter() {

            mInflater = LayoutInflater.from(getActivity());

            mSystemLocales = Locale.getAvailableLocales();
            String[] languageCodes = Locale.getISOLanguages();

            ArrayList<Locale> mTemp = new ArrayList<Locale>();
            for (String code : languageCodes) {
                if (code.length() == 2) { // We're only interested in two character codes
                    Locale l = new Locale(code);
                    if (hasLocale(l))
                        mTemp.add(l);
                }
            }

            Collections.sort(mTemp, new Comparator<Locale>() {
                @Override
                public int compare(Locale lhs, Locale rhs) {
                    return lhs.getDisplayLanguage(Locale.getDefault()).compareToIgnoreCase(rhs.getDisplayLanguage(Locale.getDefault()));
                }
            });

            mLocales = new ArrayList<Locale>(mTemp);
        }
 
Example 7
/**
 * @bug 4126880
 */
void Test4126880() {
    String[] test;

    test = Locale.getISOCountries();
    test[0] = "SUCKER!!!";
    test = Locale.getISOCountries();
    if (test[0].equals("SUCKER!!!"))
        errln("Changed internal country code list!");

    test = Locale.getISOLanguages();
    test[0] = "HAHAHAHA!!!";
    test = Locale.getISOLanguages();
    if (test[0].equals("HAHAHAHA!!!")) // Fixed typo
        errln("Changes internal language code list!");
}
 
Example 8
public static void main (String[] args) throws Exception {
    Locale es = new Locale ("es");
    String[] isoLangs = es.getISOLanguages ();
    String error = "";

    for (int i = 0; i < isoLangs.length; i++) {
        Locale current = new Locale (isoLangs[i]);
        String localeString = current.getDisplayLanguage (es);
        String startLetter = localeString.substring (0,1);
        if (!startLetter.toLowerCase (es).equals (startLetter)){
            error = error + "\n\t"+ isoLangs[i] + " " + localeString;
        }
    }

    if (error.length () > 0){
        throw new Exception ("\nFollowing language names starts with upper-case letter: "
                + error + "\nLower-case expected!");
    }

}
 
Example 9
Source Project: Mizuu   File: IdentifyMovieFragment.java    License: Apache License 2.0 6 votes vote down vote up
public LanguageAdapter() {

            mInflater = LayoutInflater.from(getActivity());

            mSystemLocales = Locale.getAvailableLocales();
            String[] languageCodes = Locale.getISOLanguages();

            ArrayList<Locale> mTemp = new ArrayList<Locale>();
            for (String code : languageCodes) {
                if (code.length() == 2) { // We're only interested in two character codes
                    Locale l = new Locale(code);
                    if (hasLocale(l))
                        mTemp.add(l);
                }
            }

            Collections.sort(mTemp, new Comparator<Locale>() {
                @Override
                public int compare(Locale lhs, Locale rhs) {
                    return lhs.getDisplayLanguage(Locale.getDefault()).compareToIgnoreCase(rhs.getDisplayLanguage(Locale.getDefault()));
                }
            });

            mLocales = new ArrayList<Locale>(mTemp);
        }
 
Example 10
/**
 * @bug 4126880
 */
void Test4126880() {
    String[] test;

    test = Locale.getISOCountries();
    test[0] = "SUCKER!!!";
    test = Locale.getISOCountries();
    if (test[0].equals("SUCKER!!!")) {
        errln("Changed internal country code list!");
    }

    test = Locale.getISOLanguages();
    test[0] = "HAHAHAHA!!!";
    test = Locale.getISOLanguages();
    if (test[0].equals("HAHAHAHA!!!")) { // Fixed typo
        errln("Changes internal language code list!");
    }
}
 
Example 11
/**
 * @bug 4126880
 */
void Test4126880() {
    String[] test;

    test = Locale.getISOCountries();
    test[0] = "SUCKER!!!";
    test = Locale.getISOCountries();
    if (test[0].equals("SUCKER!!!"))
        errln("Changed internal country code list!");

    test = Locale.getISOLanguages();
    test[0] = "HAHAHAHA!!!";
    test = Locale.getISOLanguages();
    if (test[0].equals("HAHAHAHA!!!")) // Fixed typo
        errln("Changes internal language code list!");
}
 
Example 12
public static void main (String[] args) throws Exception {
    Locale es = new Locale ("es");
    String[] isoLangs = es.getISOLanguages ();
    String error = "";

    for (int i = 0; i < isoLangs.length; i++) {
        Locale current = new Locale (isoLangs[i]);
        String localeString = current.getDisplayLanguage (es);
        String startLetter = localeString.substring (0,1);
        if (!startLetter.toLowerCase (es).equals (startLetter)){
            error = error + "\n\t"+ isoLangs[i] + " " + localeString;
        }
    }

    if (error.length () > 0){
        throw new Exception ("\nFollowing language names starts with upper-case letter: "
                + error + "\nLower-case expected!");
    }

}
 
Example 13
Source Project: hottub   File: LocaleTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * @bug 4126880
 */
void Test4126880() {
    String[] test;

    test = Locale.getISOCountries();
    test[0] = "SUCKER!!!";
    test = Locale.getISOCountries();
    if (test[0].equals("SUCKER!!!"))
        errln("Changed internal country code list!");

    test = Locale.getISOLanguages();
    test[0] = "HAHAHAHA!!!";
    test = Locale.getISOLanguages();
    if (test[0].equals("HAHAHAHA!!!")) // Fixed typo
        errln("Changes internal language code list!");
}
 
Example 14
Source Project: Telegram-FOSS   File: Util.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static HashMap<String, String> createIso3ToIso2Map() {
  String[] iso2Languages = Locale.getISOLanguages();
  HashMap<String, String> iso3ToIso2 =
      new HashMap<>(
          /* initialCapacity= */ iso2Languages.length + iso3BibliographicalToIso2.length);
  for (String iso2 : iso2Languages) {
    try {
      // This returns the ISO 639-2/T code for the language.
      String iso3 = new Locale(iso2).getISO3Language();
      if (!TextUtils.isEmpty(iso3)) {
        iso3ToIso2.put(iso3, iso2);
      }
    } catch (MissingResourceException e) {
      // Shouldn't happen for list of known languages, but we don't want to throw either.
    }
  }
  // Add additional ISO 639-2/B codes to mapping.
  for (int i = 0; i < iso3BibliographicalToIso2.length; i += 2) {
    iso3ToIso2.put(iso3BibliographicalToIso2[i], iso3BibliographicalToIso2[i + 1]);
  }
  return iso3ToIso2;
}
 
Example 15
/**
 * @bug 4126880
 */
void Test4126880() {
    String[] test;

    test = Locale.getISOCountries();
    test[0] = "SUCKER!!!";
    test = Locale.getISOCountries();
    if (test[0].equals("SUCKER!!!"))
        errln("Changed internal country code list!");

    test = Locale.getISOLanguages();
    test[0] = "HAHAHAHA!!!";
    test = Locale.getISOLanguages();
    if (test[0].equals("HAHAHAHA!!!")) // Fixed typo
        errln("Changes internal language code list!");
}
 
Example 16
public static void main (String[] args) throws Exception {
    Locale es = new Locale ("es");
    String[] isoLangs = es.getISOLanguages ();
    String error = "";

    for (int i = 0; i < isoLangs.length; i++) {
        Locale current = new Locale (isoLangs[i]);
        String localeString = current.getDisplayLanguage (es);
        String startLetter = localeString.substring (0,1);
        if (!startLetter.toLowerCase (es).equals (startLetter)){
            error = error + "\n\t"+ isoLangs[i] + " " + localeString;
        }
    }

    if (error.length () > 0){
        throw new Exception ("\nFollowing language names starts with upper-case letter: "
                + error + "\nLower-case expected!");
    }

}
 
Example 17
Source Project: Telegram   File: Util.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static HashMap<String, String> createIso3ToIso2Map() {
  String[] iso2Languages = Locale.getISOLanguages();
  HashMap<String, String> iso3ToIso2 =
      new HashMap<>(
          /* initialCapacity= */ iso2Languages.length + iso3BibliographicalToIso2.length);
  for (String iso2 : iso2Languages) {
    try {
      // This returns the ISO 639-2/T code for the language.
      String iso3 = new Locale(iso2).getISO3Language();
      if (!TextUtils.isEmpty(iso3)) {
        iso3ToIso2.put(iso3, iso2);
      }
    } catch (MissingResourceException e) {
      // Shouldn't happen for list of known languages, but we don't want to throw either.
    }
  }
  // Add additional ISO 639-2/B codes to mapping.
  for (int i = 0; i < iso3BibliographicalToIso2.length; i += 2) {
    iso3ToIso2.put(iso3BibliographicalToIso2[i], iso3BibliographicalToIso2[i + 1]);
  }
  return iso3ToIso2;
}
 
Example 18
/**
 * Locale to ISO.
 *
 * @return map containing ISO : locale pairs
 */
public static Map<String, Locale> localeToISO() {
	String[] languages = Locale.getISOLanguages();
	Map<String, Locale> localeMap = new HashMap<String, Locale>(languages.length);
	for (String language : languages) {
		Locale locale = new Locale(language);
		localeMap.put(locale.getISO3Language(), locale);
	}
	return localeMap;
}
 
Example 19
private boolean checkLocale(String languageISOCode) {
    String[] locales = Locale.getISOLanguages();
    for (String l : locales) {
        if (l.equalsIgnoreCase(languageISOCode)) {
            return true;
        }
    }
    return false;
}
 
Example 20
Source Project: edx-app-android   File: LocaleUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Provides the list of languages fetched from the system.
 *
 * @return A list of {@link FormOption} containing language names and their codes.
 */
public static List<FormOption> getLanguages() {
    final List<FormOption> languages = new ArrayList<>();
    for (String languageCode : Locale.getISOLanguages()) {
        try {
            final String languageName = getLanguageNameFromCode(languageCode);
            /*
            Ignore language codes that don't translate to a proper language name. This can be
            detected by case-sensitive matching of the code with its language name
            i.e. if both are same, its not a valid language.
            For example: language names like aa, ace, ada, ady, ae etc.

            This exemption is necessary because of following excerpt found inside
            Locale.getISOLanguages() function's javadoc:
            "The list returned by this method does not contain ALL valid codes that can be used
            to create Locales."

            ### Special case ###
            zxx = No linguistic content.
            */
            if (!languageCode.equals(languageName) && !languageCode.equals("zxx")) {
                languages.add(new FormOption(languageName, languageCode));
            }
        } catch (InvalidLocaleException e) {
            e.printStackTrace();
        }
    }
    sortBasedOnLocale(languages);
    return languages;
}