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

The following examples show how to use java.util.Locale#getAvailableLocales() . 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: MonetaryFormatsTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
/**
 * Tests formatting and parsing back the values using all available locales.
 */
@Test
public void testRoundRobinForAllLocales_Money(){
    String report = "";
    Locale defaultLocale = Locale.getDefault();
    try {
        for (Locale locale : Locale.getAvailableLocales()) {
            Locale.setDefault(locale);
            try {
                Money money = Money.of(1.2, "EUR");
                if (!money.equals(Money.parse(money.toString()))) {
                    report += "FAILED : " + locale + "(" + money.toString() + ")\n";
                } else {
                    report += "SUCCESS: " + locale + "\n";
                }
            }catch(Exception e){
                report += "ERROR: " + locale + " -> " + e + "\n";
            }
        }
        assertFalse(report.contains("FAILED"),"Formatting and parsing failed for some locales:\n\n"+report);
    }finally{
        Locale.setDefault(defaultLocale);
    }
}
 
Example 2
Source File: InternationalBAT.java    From native-obfuscator with GNU General Public License v3.0 5 votes vote down vote up
private static boolean testRequiredLocales() {
    boolean pass = true;

    TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
    Calendar calendar = Calendar.getInstance(Locale.US);
    calendar.clear();
    calendar.set(2001, 4, 10, 12, 0, 0);
    Date date = calendar.getTime();

    Locale[] available = Locale.getAvailableLocales();
    for (int i = 0; i < requiredLocales.length; i++) {
        Locale locale = requiredLocales[i];
        boolean found = false;
        for (int j = 0; j < available.length; j++) {
            if (available[j].equals(locale)) {
                found = true;
                break;
            }
        }
        if (!found) {
            System.out.println("Locale not available: " + locale);
            pass = false;
        } else {
            DateFormat format =
                    DateFormat.getDateInstance(DateFormat.FULL, locale);
            String dateString = format.format(date);
            if (!dateString.equals(requiredLocaleDates[i])) {
                System.out.println("Incorrect date string for locale "
                        + locale + ". Expected: " + requiredLocaleDates[i]
                        + ", got: " + dateString);
                pass = false;
            }
        }
    }
    return pass;
}
 
Example 3
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 4
Source File: ViewAttributeList.java    From arx with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the local for the given isoLanguage
 * @param isoLanguage
 * @return
 */
private Locale getLocale(String isoLanguage) {
    for (Locale locale : Locale.getAvailableLocales()) {
        if (locale.getLanguage().toUpperCase().equals(isoLanguage.toUpperCase())) {
            return locale;
        }
    }
    throw new IllegalStateException("Unknown locale");
}
 
Example 5
Source File: bug4123285.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void start() {
    System.out.println("Hello, world!");
    Locale[] systemLocales = null;
    try {
        System.out.println("Default locale = " + Locale.getDefault());
        systemLocales = Locale.getAvailableLocales();
        System.out.println("Found " + systemLocales.length + " locales:");
        Locale[] locales = new Locale[systemLocales.length];
        for (int i = 0; i < locales.length; i++) {
            Locale lowest = null;
            for (int j = 0; j < systemLocales.length; j++) {
                if (i > 0 && locales[i - 1].toString().compareTo(systemLocales[j].toString()) >= 0)
                   continue;
                if (lowest == null || systemLocales[j].toString().compareTo(lowest.toString()) < 0)
                   lowest = systemLocales[j];
            }
            locales[i] = lowest;
        }
        for (int i = 0; i < locales.length; i++) {
            if (locales[i].getCountry().length() == 0)
               System.out.println("    " + locales[i].getDisplayLanguage() + ":");
            else {
                if (locales[i].getVariant().length() == 0)
                   System.out.println("        " + locales[i].getDisplayCountry());
                else
                    System.out.println("        " + locales[i].getDisplayCountry() + ", "
                                + locales[i].getDisplayVariant());
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: Localization.java    From unitime with Apache License 2.0 5 votes vote down vote up
private static Locale guessJavaLocale(String locale) {
	for (StringTokenizer s = new StringTokenizer(locale, ",;"); s.hasMoreTokens(); ) {
		String lang = s.nextToken();
		String cc = null;
		if (lang.indexOf('_') >= 0) {
			cc = lang.substring(lang.indexOf('_') + 1);
			lang = lang.substring(0, lang.indexOf('_'));
		}
		for (Locale loc: Locale.getAvailableLocales())
			if ((lang == null || lang.isEmpty() || lang.equals(loc.getLanguage())) && (cc == null || cc.isEmpty() || cc.equals(loc.getCountry()))) {
				return loc;
			}
	}
	return Locale.getDefault();
}
 
Example 7
Source File: bug4123285.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void start() {
    System.out.println("Hello, world!");
    Locale[] systemLocales = null;
    try {
        System.out.println("Default locale = " + Locale.getDefault());
        systemLocales = Locale.getAvailableLocales();
        System.out.println("Found " + systemLocales.length + " locales:");
        Locale[] locales = new Locale[systemLocales.length];
        for (int i = 0; i < locales.length; i++) {
            Locale lowest = null;
            for (int j = 0; j < systemLocales.length; j++) {
                if (i > 0 && locales[i - 1].toString().compareTo(systemLocales[j].toString()) >= 0)
                   continue;
                if (lowest == null || systemLocales[j].toString().compareTo(lowest.toString()) < 0)
                   lowest = systemLocales[j];
            }
            locales[i] = lowest;
        }
        for (int i = 0; i < locales.length; i++) {
            if (locales[i].getCountry().length() == 0)
               System.out.println("    " + locales[i].getDisplayLanguage() + ":");
            else {
                if (locales[i].getVariant().length() == 0)
                   System.out.println("        " + locales[i].getDisplayCountry());
                else
                    System.out.println("        " + locales[i].getDisplayCountry() + ", "
                                + locales[i].getDisplayVariant());
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 8
Source File: bug4122700.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Locale[] systemLocales = Locale.getAvailableLocales();
    if (systemLocales.length == 0)
        throw new Exception("Available locale list is empty!");
    System.out.println("Found " + systemLocales.length + " locales:");
    Locale[] locales = new Locale[systemLocales.length];
    for (int i = 0; i < locales.length; i++) {
        Locale lowest = null;
        for (int j = 0; j < systemLocales.length; j++) {
            if (i > 0 && locales[i - 1].toString().compareTo(systemLocales[j].toString()) >= 0)
                continue;
            if (lowest == null || systemLocales[j].toString().compareTo(lowest.toString()) < 0)
                lowest = systemLocales[j];
        }
        locales[i] = lowest;
    }
    for (int i = 0; i < locales.length; i++) {
        if (locales[i].getCountry().length() == 0)
            System.out.println("    " + locales[i].getDisplayLanguage() + ":");
        else {
            if (locales[i].getVariant().length() == 0)
                System.out.println("        " + locales[i].getDisplayCountry());
            else
                System.out.println("        " + locales[i].getDisplayCountry() + ", "
                                + locales[i].getDisplayVariant());
        }
    }
}
 
Example 9
Source File: StringUtilsTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test  // SPR-16651
public void testAvailableLocalesWithLanguageTag() {
	for (Locale locale : Locale.getAvailableLocales()) {
		Locale parsedLocale = StringUtils.parseLocale(locale.toLanguageTag());
		if (parsedLocale == null) {
			assertEquals("", locale.getLanguage());
		}
		else {
			assertEquals(parsedLocale.toLanguageTag(), locale.toLanguageTag());
		}
	}
}
 
Example 10
Source File: DocLocale.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Search the locale for specified language, specified country and
 * specified variant.
 */
private Locale searchLocale(String language, String country,
                            String variant) {
    Locale[] locales = Locale.getAvailableLocales();
    for (int i = 0; i < locales.length; i++) {
        if (locales[i].getLanguage().equals(language) &&
           (country == null || locales[i].getCountry().equals(country)) &&
           (variant == null || locales[i].getVariant().equals(variant))) {
            return locales[i];
        }
    }
    return null;
}
 
Example 11
Source File: LocaleUtilsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test availableLocaleSet() method.
 */
@Test
public void testAvailableLocaleSet() {
    Set<Locale> set = LocaleUtils.availableLocaleSet();
    Set<Locale> set2 = LocaleUtils.availableLocaleSet();
    assertNotNull(set);
    assertSame(set, set2);
    assertUnmodifiableCollection(set);
    
    Locale[] jdkLocaleArray = Locale.getAvailableLocales();
    List<Locale> jdkLocaleList = Arrays.asList(jdkLocaleArray);
    Set<Locale> jdkLocaleSet = new HashSet<Locale>(jdkLocaleList);
    assertEquals(jdkLocaleSet, set);
}
 
Example 12
Source File: bug4122700.java    From native-obfuscator with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Locale[] systemLocales = Locale.getAvailableLocales();
    if (systemLocales.length == 0)
        throw new Exception("Available locale list is empty!");
    System.out.println("Found " + systemLocales.length + " locales:");
    Locale[] locales = new Locale[systemLocales.length];
    for (int i = 0; i < locales.length; i++) {
        Locale lowest = null;
        for (int j = 0; j < systemLocales.length; j++) {
            if (i > 0 && locales[i - 1].toString().compareTo(systemLocales[j].toString()) >= 0)
                continue;
            if (lowest == null || systemLocales[j].toString().compareTo(lowest.toString()) < 0)
                lowest = systemLocales[j];
        }
        locales[i] = lowest;
    }
    for (int i = 0; i < locales.length; i++) {
        if (locales[i].getCountry().length() == 0)
            System.out.println("    " + locales[i].getDisplayLanguage() + ":");
        else {
            if (locales[i].getVariant().length() == 0)
                System.out.println("        " + locales[i].getDisplayCountry());
            else
                System.out.println("        " + locales[i].getDisplayCountry() + ", "
                                + locales[i].getDisplayVariant());
        }
    }
}
 
Example 13
Source File: LocaleTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @bug 4107014
 */
public void TestGetAvailableLocales() {
    Locale[] locales = Locale.getAvailableLocales();
    if (locales == null || locales.length == 0)
        errln("Locale.getAvailableLocales() returned no installed locales!");
    else {
        logln("Locale.getAvailableLocales() returned a list of " + locales.length + " locales.");
        for (int i = 0; i < locales.length; i++)
            logln(locales[i].toString());
    }
}
 
Example 14
Source File: bug4122700.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Locale[] systemLocales = Locale.getAvailableLocales();
    if (systemLocales.length == 0)
        throw new Exception("Available locale list is empty!");
    System.out.println("Found " + systemLocales.length + " locales:");
    Locale[] locales = new Locale[systemLocales.length];
    for (int i = 0; i < locales.length; i++) {
        Locale lowest = null;
        for (int j = 0; j < systemLocales.length; j++) {
            if (i > 0 && locales[i - 1].toString().compareTo(systemLocales[j].toString()) >= 0)
                continue;
            if (lowest == null || systemLocales[j].toString().compareTo(lowest.toString()) < 0)
                lowest = systemLocales[j];
        }
        locales[i] = lowest;
    }
    for (int i = 0; i < locales.length; i++) {
        if (locales[i].getCountry().length() == 0)
            System.out.println("    " + locales[i].getDisplayLanguage() + ":");
        else {
            if (locales[i].getVariant().length() == 0)
                System.out.println("        " + locales[i].getDisplayCountry());
            else
                System.out.println("        " + locales[i].getDisplayCountry() + ", "
                                + locales[i].getDisplayVariant());
        }
    }
}
 
Example 15
Source File: LocaleTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @bug 4135316
 */
public void TestBug4135316() {
    Locale[] locales1 = Locale.getAvailableLocales();
    Locale[] locales2 = Locale.getAvailableLocales();
    if (locales1 == locales2)
        errln("Locale.getAvailableLocales() doesn't clone its internal storage!");
}
 
Example 16
Source File: WorkerLoad.java    From arx with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the local for the given isoLanguage
 * @param isoLanguage
 * @return
 */
private Locale getLocale(String isoLanguage) {
    for (Locale locale : Locale.getAvailableLocales()) {
        if (locale.getLanguage().toUpperCase().equals(isoLanguage.toUpperCase())) {
            return locale;
        }
    }
    throw new IllegalStateException("Unknown locale");
}
 
Example 17
Source File: Bug8139107.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testSupportedLocales() {
    for (Locale loc:Locale.getAvailableLocales()) {
        testLocale(loc);
    }
}
 
Example 18
Source File: VoiceActivity.java    From BotLibre with Eclipse Public License 1.0 4 votes vote down vote up
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
   public void onInit(int status) { 
       if (status == TextToSpeech.SUCCESS) {
           List<String> locales = new ArrayList<String>();
           if (MainActivity.voice.language != null) {
           	locales.add(MainActivity.voice.language);
           }
           locales.add(Locale.US.toString());
           locales.add(Locale.UK.toString());
           locales.add(Locale.FRENCH.toString());
           locales.add(Locale.GERMAN.toString());
           locales.add("ES");
           locales.add("PT");
           locales.add(Locale.ITALIAN.toString());
           locales.add(Locale.CHINESE.toString());
           locales.add(Locale.JAPANESE.toString());
           locales.add(Locale.KOREAN.toString());
           for (Locale locale : Locale.getAvailableLocales()) {
           	try {
            	int code = this.tts.isLanguageAvailable(locale);
                if (code != TextToSpeech.LANG_NOT_SUPPORTED) {
                	locales.add(locale.toString());
                }
           	} catch (Exception ignore) {}
           }
   		Spinner spin = (Spinner) findViewById(R.id.languageSpin);
   		ArrayAdapter adapter = new ArrayAdapter(this,
                   android.R.layout.simple_spinner_dropdown_item, locales.toArray());
   		spin.setAdapter(adapter);
           if (MainActivity.voice.language != null) {
           	spin.setSelection(locales.indexOf(MainActivity.voice.language));
           }
   		
           int result = this.tts.setLanguage(Locale.US); 
           if (result == TextToSpeech.LANG_MISSING_DATA
                   || result == TextToSpeech.LANG_NOT_SUPPORTED) {
               Log.e("TTS", "This Language is not supported");
           }
       } else {
           Log.e("TTS", "Initilization Failed!");
       }

   }
 
Example 19
Source File: I18NJarTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String... args) throws Exception {
    boolean localeAvailable = false;
    for (Locale l : Locale.getAvailableLocales()) {
        if (l.toLanguageTag().equals(Locale.JAPAN.toLanguageTag())) {
            localeAvailable = true;
            break;
        }
    }
    if (!localeAvailable) {
        System.out.println("Warning: locale: " + Locale.JAPAN
                + " not found, test passes vacuously");
        return;
    }
    if ("C".equals(LC_ALL) || "C".equals(LANG)) {
        System.out.println("Warning: The LANG and/or LC_ALL env vars are " +
          "set to \"C\":\n" +
          "  LANG=" + LANG + "\n" +
          "  LC_ALL=" + LC_ALL + "\n" +
          "This test requires support for multi-byte filenames.\n" +
          "Test passes vacuously.");
        return;
    }
    if (encoding.equals("MS932") || encoding.equals("UTF-8")) {
        Locale.setDefault(Locale.JAPAN);
        System.out.println("using locale " + Locale.JAPAN +
                ", encoding " + encoding);
    } else {
        System.out.println("Warning: current encoding is " + encoding +
                "this test requires MS932 <Ja> or UTF-8," +
                " test passes vacuously");
        return;
    }
    dir.mkdir();
    File dirfile = new File(dir, "foo.jar");
    createJar(dirfile,
            "public static void main(String... args) {",
            "System.out.println(\"Hello World\");",
            "System.exit(0);",
            "}");

    // remove the class files, to ensure that the class is indeed picked up
    // from the jar file and not from ambient classpath.
    File[] classFiles = cwd.listFiles(createFilter(CLASS_FILE_EXT));
    for (File f : classFiles) {
        f.delete();
    }

    // test with a jar file
    TestResult tr = doExec(javaCmd, "-jar", dirfile.getAbsolutePath());
    System.out.println(tr);
    if (!tr.isOK()) {
        throw new RuntimeException("TEST FAILED");
    }

    // test the same class but by specifying it as a classpath
    tr = doExec(javaCmd, "-cp", dirfile.getAbsolutePath(), "Foo");
    System.out.println(tr);
    if (!tr.isOK()) {
        throw new RuntimeException("TEST FAILED");
    }
}
 
Example 20
Source File: CompareToEqualsTests.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String args[])
            throws Exception {
     Locale reservedLocale = Locale.getDefault();
     try {
        /**
         * Test cases:
         * 1) Same RDNs.
         * 2) same RDN sequence with an AVA ordered differently.
         * 3) RDN sequences of a differing AVA.
         * 4) RDN sequence of different length.
         * 5) RDN sequence of different Case.
         * 6) Matching binary return values.
         * 7) Binary values that don't match.
         */
        String names1[] = new String [] {
            "ou=Sales+cn=Bob", "ou=Sales+cn=Bob", "ou=Sales+cn=Bob",
            "ou=Sales+cn=Scott+c=US", "cn=config"};

        String names2[] = new String [] {
            "ou=Sales+cn=Bob", "cn=Bob+ou=Sales", "ou=Sales+cn=Scott",
            "ou=Sales+cn=Scott", "Cn=COnFIG"};

        int expectedResults[] = {0, 0, -1, -1, 0};

        for (Locale locale : Locale.getAvailableLocales()) {
            // reset the default locale
            Locale.setDefault(locale);

            for (int i = 0; i < names1.length; i++) {
                checkResults(new LdapName(names1[i]),
                    new LdapName(names2[i]), expectedResults[i]);
            }

            byte[] value = "abcxyz".getBytes();
            Rdn rdn1 = new Rdn("binary", value);
            ArrayList rdns1 = new ArrayList();
            rdns1.add(rdn1);
            LdapName l1 = new LdapName(rdns1);

            Rdn rdn2 = new Rdn("binary", value);
            ArrayList rdns2 = new ArrayList();
            rdns2.add(rdn2);
            LdapName l2 = new LdapName(rdns2);
            checkResults(l1, l2, 0);

            l2 = new LdapName("binary=#61626378797A");
            checkResults(l1, l2, 0);

            l2 = new LdapName("binary=#61626378797B");
            checkResults(l1, l2, -1);

            System.out.println("Tests passed");
        }
    } finally {
        // restore the reserved locale
        Locale.setDefault(reservedLocale);
    }
}