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

The following examples show how to use java.util.Locale#getISO3Country() . 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
public static Map<String, String> getCountries() {
    Map<String, String> countries = new HashMap<String, String>();

    String[] isoCountries = Locale.getISOCountries();
    for (String country : isoCountries) {
        Locale locale = new Locale("", country);
        String iso = locale.getISO3Country();
        String code = locale.getCountry();
        String name = locale.getDisplayCountry();

        if (!"".equals(iso) && !"".equals(code)
                && !"".equals(name)) {
            countries.put(code, name);
        }
    }

    return countries;
}
 
Example 2
private static boolean isValid(@NonNull Locale locale) {
  try {
    return locale.getISO3Language() != null && locale.getISO3Country() != null;
  } catch (Exception ex) {
    return false;
  }
}
 
Example 3
Source Project: nextreports-designer   File: LocaleUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static List<Country> getCountries() {
	List<Country> countries = new ArrayList<Country>();
	Locale[] locales = Locale.getAvailableLocales();
	for (Locale locale : locales) {
		String iso = "NA";
		try {
			iso = locale.getISO3Country();
		} catch (MissingResourceException ex) {				
		}
		String code = locale.getCountry();
		String name = locale.getDisplayCountry();
		String language = locale.getLanguage();

		if (!"".equals(iso) && !"".equals(code) && !"".equals(name)) {				
			Country c = new Country(iso, code, name, language);
			if (!countries.contains(c)) {
				countries.add(c);
			}	
		}
	}

	Collections.sort(countries, new Comparator<Country>() {
		@Override
		public int compare(Country c1, Country c2) {
			 return Collator.getInstance().compare(c1.getName(), c2.getName());
		}
		
	});	
	return countries;
}
 
Example 4
/**
 * @bug 4147315
 * java.util.Locale.getISO3Country() works wrong for non ISO-3166 codes.
 * Should throw an exception for unknown locales
 */
public void Test4147315() {
    // Try with codes that are the wrong length but happen to match text
    // at a valid offset in the mapping table
    Locale locale = new Locale("aaa", "CCC");

    try {
        String result = locale.getISO3Country();

        errln("ERROR: getISO3Country() returns: " + result +
            " for locale '" + locale + "' rather than exception" );
    } catch(MissingResourceException e) { }
}
 
Example 5
/**
 * @bug 4147315
 * java.util.Locale.getISO3Country() works wrong for non ISO-3166 codes.
 * Should throw an exception for unknown locales
 */
public void Test4147315() {
    // Try with codes that are the wrong length but happen to match text
    // at a valid offset in the mapping table
    Locale locale = new Locale("aaa", "CCC");

    try {
        String result = locale.getISO3Country();

        errln("ERROR: getISO3Country() returns: " + result +
            " for locale '" + locale + "' rather than exception" );
    } catch(MissingResourceException e) { }
}
 
Example 6
Source Project: jdk8u60   File: LocaleTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * @bug 4147315
 * java.util.Locale.getISO3Country() works wrong for non ISO-3166 codes.
 * Should throw an exception for unknown locales
 */
public void Test4147315() {
    // Try with codes that are the wrong length but happen to match text
    // at a valid offset in the mapping table
    Locale locale = new Locale("aaa", "CCC");

    try {
        String result = locale.getISO3Country();

        errln("ERROR: getISO3Country() returns: " + result +
            " for locale '" + locale + "' rather than exception" );
    } catch(MissingResourceException e) { }
}
 
Example 7
Source Project: java-trader   File: ResourceUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 列出本地化文件的名称
 */
public static List<String> listLocalizedFiles(String messageFile, Locale locale) {
    int dot = messageFile.lastIndexOf('.');
    String messageFileMain = null;
    String messageFileExt = null;
    if ( dot>0) {
        messageFileMain = messageFile.substring(0, dot);
        messageFileExt = messageFile.substring(dot);
    }else {
        messageFileMain = messageFile;
        messageFileExt = "";
    }
    List<String> paths = new ArrayList<>();

    if ( locale!=null ) {
        String cn = locale.getCountry();
        String cn3 = locale.getISO3Country();
        String ln = locale.getLanguage();

        //add file_message_zh_CN.properties
        if ( !StringUtil.isEmpty(cn) ) {
            paths.add( messageFileMain+"_"+ln+"_"+cn+messageFileExt);
        }
        if ( !StringUtil.isEmpty(cn3) ) {
            paths.add( messageFileMain+"_"+ln+"_"+cn3+messageFileExt);
        }
        //add files_message_zh.properties
        paths.add( messageFileMain+"_"+ln+messageFileExt);
    }
    paths.add( messageFile);
    return paths;
}
 
Example 8
/**
 * @bug 4147315
 * java.util.Locale.getISO3Country() works wrong for non ISO-3166 codes.
 * Should throw an exception for unknown locales
 */
public void Test4147315() {
    // Try with codes that are the wrong length but happen to match text
    // at a valid offset in the mapping table
    Locale locale = new Locale("aaa", "CCC");

    try {
        String result = locale.getISO3Country();

        errln("ERROR: getISO3Country() returns: " + result +
            " for locale '" + locale + "' rather than exception" );
    } catch(MissingResourceException e) { }
}
 
Example 9
/**
 * @bug 4147315
 * java.util.Locale.getISO3Country() works wrong for non ISO-3166 codes.
 * Should throw an exception for unknown locales
 */
public void Test4147315() {
    // Try with codes that are the wrong length but happen to match text
    // at a valid offset in the mapping table
    Locale locale = new Locale("aaa", "CCC");

    try {
        String result = locale.getISO3Country();

        errln("ERROR: getISO3Country() returns: " + result +
            " for locale '" + locale + "' rather than exception" );
    } catch(MissingResourceException e) { }
}
 
Example 10
/**
 * @bug 4147315
 * java.util.Locale.getISO3Country() works wrong for non ISO-3166 codes.
 * Should throw an exception for unknown locales
 */
public void Test4147315() {
    // Try with codes that are the wrong length but happen to match text
    // at a valid offset in the mapping table
    Locale locale = new Locale("aaa", "CCC");

    try {
        String result = locale.getISO3Country();

        errln("ERROR: getISO3Country() returns: " + result
                + " for locale '" + locale + "' rather than exception");
    } catch (MissingResourceException e) {
    }
}
 
Example 11
@VisibleForTesting
static boolean isValidLangauge(String lang) {
  String[] parts = lang.split("_", 3);

  if (parts.length != 2) return false;

  try {
    final Locale loc = new Locale(parts[0], parts[1]);

    return (loc.getISO3Language() != null) && (loc.getISO3Country() != null);
  } catch (MissingResourceException e) {
    return false;
  }
}
 
Example 12
/**
 * @bug 4147315
 * java.util.Locale.getISO3Country() works wrong for non ISO-3166 codes.
 * Should throw an exception for unknown locales
 */
public void Test4147315() {
    // Try with codes that are the wrong length but happen to match text
    // at a valid offset in the mapping table
    Locale locale = new Locale("aaa", "CCC");

    try {
        String result = locale.getISO3Country();

        errln("ERROR: getISO3Country() returns: " + result +
            " for locale '" + locale + "' rather than exception" );
    } catch(MissingResourceException e) { }
}
 
Example 13
/**
 * Get list of available languages for TextToSpeech.  Do not call unless the TTS
 * engine is initialized.
 *
 */
private void getLanguageAndCountryLists() {
  // We do compute these lists pre-Donut.  We probably could
  // arrange to also do this in earlier releases, but that would be
  // relying on the use of an external textToSpeech application and those
  // old releases are obsolete anyway.
  if (SdkLevel.getLevel() >= SdkLevel.LEVEL_DONUT) {
    String tempLang;
    String tempCountry;
    for (Locale locale : Locale.getAvailableLocales()) {
      // isLanguageAvailable requires tts to be initialized
      int res = tts.isLanguageAvailable(locale);
      if (!(res == android.speech.tts.TextToSpeech.LANG_NOT_SUPPORTED)){
        tempLang = locale.getLanguage();
        // We record only the ISO3 country codes for now.  We should update the TTS control
        // to use voices, and then we can straighten this out, maybe getting rid of
        // country modifiers in TTS altogether.
        tempCountry = locale.getISO3Country();
        if (!tempLang.equals("") && (!languageList.contains(tempLang))){
          languageList.add(tempLang);
        }
        if (!tempCountry.equals("") && (!countryList.contains(tempCountry))){
          countryList.add(tempCountry);
        }
      }
    }
    Collections.sort(languageList);
    Collections.sort(countryList);
    allLanguages = YailList.makeList(languageList);
    allCountries = YailList.makeList(countryList);
  }
}
 
Example 14
Source Project: hottub   File: LocaleTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * @bug 4147315
 * java.util.Locale.getISO3Country() works wrong for non ISO-3166 codes.
 * Should throw an exception for unknown locales
 */
public void Test4147315() {
    // Try with codes that are the wrong length but happen to match text
    // at a valid offset in the mapping table
    Locale locale = new Locale("aaa", "CCC");

    try {
        String result = locale.getISO3Country();

        errln("ERROR: getISO3Country() returns: " + result +
            " for locale '" + locale + "' rather than exception" );
    } catch(MissingResourceException e) { }
}
 
Example 15
/**
 * @bug 4147315
 * java.util.Locale.getISO3Country() works wrong for non ISO-3166 codes.
 * Should throw an exception for unknown locales
 */
public void Test4147315() {
    // Try with codes that are the wrong length but happen to match text
    // at a valid offset in the mapping table
    Locale locale = new Locale("aaa", "CCC");

    try {
        String result = locale.getISO3Country();

        errln("ERROR: getISO3Country() returns: " + result +
            " for locale '" + locale + "' rather than exception" );
    } catch(MissingResourceException e) { }
}
 
Example 16
Source Project: Stringlate   File: LocaleString.java    License: MIT License 5 votes vote down vote up
private static boolean isValid(final Locale locale) {
    try {
        return locale.getISO3Language() != null && locale.getISO3Country() != null;
    } catch (MissingResourceException ignored) {
        return false;
    }
}
 
Example 17
/**
 * @bug 4147315
 * java.util.Locale.getISO3Country() works wrong for non ISO-3166 codes.
 * Should throw an exception for unknown locales
 */
public void Test4147315() {
    // Try with codes that are the wrong length but happen to match text
    // at a valid offset in the mapping table
    Locale locale = new Locale("aaa", "CCC");

    try {
        String result = locale.getISO3Country();

        errln("ERROR: getISO3Country() returns: " + result +
            " for locale '" + locale + "' rather than exception" );
    } catch(MissingResourceException e) { }
}
 
Example 18
/**
 * @bug 4147315
 * java.util.Locale.getISO3Country() works wrong for non ISO-3166 codes.
 * Should throw an exception for unknown locales
 */
public void Test4147315() {
    // Try with codes that are the wrong length but happen to match text
    // at a valid offset in the mapping table
    Locale locale = new Locale("aaa", "CCC");

    try {
        String result = locale.getISO3Country();

        errln("ERROR: getISO3Country() returns: " + result +
            " for locale '" + locale + "' rather than exception" );
    } catch(MissingResourceException e) { }
}
 
Example 19
/**
 * @bug 4147315
 * java.util.Locale.getISO3Country() works wrong for non ISO-3166 codes.
 * Should throw an exception for unknown locales
 */
public void Test4147315() {
    // Try with codes that are the wrong length but happen to match text
    // at a valid offset in the mapping table
    Locale locale = new Locale("aaa", "CCC");

    try {
        String result = locale.getISO3Country();

        errln("ERROR: getISO3Country() returns: " + result +
            " for locale '" + locale + "' rather than exception" );
    } catch(MissingResourceException e) { }
}
 
Example 20
/**
 * Adds support for plus and minus menu commands, typically for zooming in
 * and out. This is designed for menu items with key accelerators using
 * KeyEvent.VK_ADD and KeyEvent.VK_SUBTRACT (which are typically on the
 * numerical key pad). It adds support for KeyEvent.VK_MINUS and
 * KeyEvent.VK_PLUS, and KeyEvent.VK_EQUALS for keyboard layouts with the
 * plus sign as secondary key for the equals.
 *
 * @param inputMap add key event to this InputMap
 * @param actionMap add action to this ActionMap
 * @param zoomInAction action to call when the + key and the menu shortcut
 * key are pressed
 * @param zoomOutAction action to call when the - key and the menu shortcut
 * key are pressed
 */
public static void zoomMenuCommands(InputMap inputMap, ActionMap actionMap, Action zoomInAction, Action zoomOutAction) {
    int menuKeyMask = java.awt.Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();

    // add support for minus key
    KeyStroke minusMenueKeyStroke = KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_MINUS, menuKeyMask);
    inputMap.put(minusMenueKeyStroke, "zoomOutWithMinusKey");
    actionMap.put("zoomOutWithMinusKey", zoomOutAction);

    // add support for plus key to zoom in. This only works if the keyboard 
    // layout allows access to the plus character without pressing the shift 
    // key, which is not the case for US and UK keyboards.
    KeyStroke plusMenuKeyStroke = KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_PLUS, menuKeyMask);
    inputMap.put(plusMenuKeyStroke, "zoomInWithPlusKey");
    actionMap.put("zoomInWithPlusKey", zoomInAction);

    // add support for cases where the plus character is the secondary 
    // character for the equal sign key. That is, plus is accessed by pressing
    // the shift key and the equal key. This is the case for US and UK 
    // keyboard layouts, which are also used in Ireland, India, Australia, Canada,
    // Hong Kong, New Zealand, South Africa, Malaysia, Singapore and Philippines.
    // See https://stackoverflow.com/questions/15605109/java-keybinding-plus-key
    // and https://en.wikipedia.org/wiki/QWERTY
    // The French Canadian keyboard also has = and + on the same key.
    Locale locale = InputContext.getInstance().getLocale();
    String isoCountry = locale.getISO3Country();
    // https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
    if ("USA".equals(isoCountry)
            || "GBR".equals(isoCountry)
            || "IRL".equals(isoCountry)
            || "IND".equals(isoCountry)
            || "AUS".equals(isoCountry)
            || "CAN".equals(isoCountry)
            || "HKG".equals(isoCountry)
            || "NZL".equals(isoCountry)
            || "ZAF".equals(isoCountry)
            || "MYS".equals(isoCountry)
            || "SGP".equals(isoCountry)
            || "PHL".equals(isoCountry)) {
        KeyStroke euqalsMenuKeyStroke = KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_EQUALS, menuKeyMask);
        inputMap.put(euqalsMenuKeyStroke, "zoomInWithEqualsKey");
        actionMap.put("zoomInWithEqualsKey", zoomInAction);
    }
}