/** @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!"); }
public static String get639_1Code(String lang639_2Code) { String[] languages = Locale.getISOLanguages(); Map<String, Locale> localeMap = new HashMap<String, Locale>(languages.length); // Add 639-2/T variants for (String language : languages) { Locale locale = new Locale(language); localeMap.put(locale.getISO3Language(), locale); } // Add 639-2/B variants below this line localeMap.put("alb", new Locale("sq")); localeMap.put("arm", new Locale("hy")); localeMap.put("baq", new Locale("eu")); localeMap.put("bur", new Locale("my")); localeMap.put("chi", new Locale("zh")); localeMap.put("cze", new Locale("cs")); localeMap.put("dut", new Locale("nl")); localeMap.put("fre", new Locale("fr")); localeMap.put("geo", new Locale("ka")); localeMap.put("ger", new Locale("de")); localeMap.put("gre", new Locale("el")); localeMap.put("ice", new Locale("is")); localeMap.put("mac", new Locale("mk")); localeMap.put("mao", new Locale("mi")); localeMap.put("may", new Locale("ms")); localeMap.put("per", new Locale("fa")); localeMap.put("rum", new Locale("ro")); localeMap.put("slo", new Locale("sk")); localeMap.put("tib", new Locale("bo")); localeMap.put("wel", new Locale("cy")); return localeMap.get(lang639_2Code).toString(); }
protected boolean isValidLanguage(String lang) { boolean isLangValid = false; for (String validLang : Locale.getISOLanguages()) { if (validLang.equals(lang)) { isLangValid = true; break; } } return isLangValid; }
/** Return the list of languages supported by the server. */ public static Collection<Locale> getLocales() { Collection<Locale> locales = new ArrayList<Locale>(); for (String language : Locale.getISOLanguages()) { Locale locale = new Locale(language); if (isSupported(locale)) { locales.add(locale); } } return locales; }
/** @bug 4106155 4118587 7066203 */ public void TestGetLangsAndCountries() { // It didn't seem right to just do an exhaustive test of everything here, so I check // for the following things: // 1) Does each list have the right total number of entries? // 2) Does each list contain certain language and country codes we think are important // (the G7 countries, plus a couple others)? // 3) Does each list have every entry formatted correctly? (i.e., two characters, // all lower case for the language codes, all upper case for the country codes) // 4) Is each list in sorted order? String[] test = Locale.getISOLanguages(); String[] spotCheck1 = { "en", "es", "fr", "de", "it", "ja", "ko", "zh", "th", "he", "id", "iu", "ug", "yi", "za" }; if (test.length != 188) errln("Expected getISOLanguages() to return 188 languages; it returned " + test.length); else { for (int i = 0; i < spotCheck1.length; i++) { int j; for (j = 0; j < test.length; j++) if (test[j].equals(spotCheck1[i])) break; if (j == test.length || !test[j].equals(spotCheck1[i])) errln("Couldn't find " + spotCheck1[i] + " in language list."); } } for (int i = 0; i < test.length; i++) { if (!test[i].equals(test[i].toLowerCase())) errln(test[i] + " is not all lower case."); if (test[i].length() != 2) errln(test[i] + " is not two characters long."); if (i > 0 && test[i].compareTo(test[i - 1]) <= 0) errln(test[i] + " appears in an out-of-order position in the list."); } test = Locale.getISOCountries(); String[] spotCheck2 = {"US", "CA", "GB", "FR", "DE", "IT", "JP", "KR", "CN", "TW", "TH"}; if (test.length != 249) errln("Expected getISOCountries to return 249 countries; it returned " + test.length); else { for (int i = 0; i < spotCheck2.length; i++) { int j; for (j = 0; j < test.length; j++) if (test[j].equals(spotCheck2[i])) break; if (j == test.length || !test[j].equals(spotCheck2[i])) errln("Couldn't find " + spotCheck2[i] + " in country list."); } } for (int i = 0; i < test.length; i++) { if (!test[i].equals(test[i].toUpperCase())) errln(test[i] + " is not all upper case."); if (test[i].length() != 2) errln(test[i] + " is not two characters long."); if (i > 0 && test[i].compareTo(test[i - 1]) <= 0) errln(test[i] + " appears in an out-of-order position in the list."); } }
/** * @return the locale representing the language of the source document or <code>null</code> if * there is no language set or the language code is not a valid ISO 639 language code. */ public Locale getLanguage() { Locale locale = null; if (language != null) { String ident = language.getAttributeValue(Attribute.language_ident); // try to build a locale with ident if (ident != null) { String parts[] = ident.split(Pattern.quote("-")); // we try to find the language String langCode = null; String langPart = parts[0].toLowerCase(); // validate language for (String validLangCode : Locale.getISOLanguages()) { if (validLangCode.equals(langPart)) { langCode = validLangCode; break; } } if (langCode != null) { // ok we have a valid language // try to find the country String countryCode = null; for (int i = 1; i < parts.length; i++) { String countryPart = parts[i].toUpperCase(); // validate country for (String validCountryCode : Locale.getISOCountries()) { if (validCountryCode.equals(countryPart)) { countryCode = validCountryCode; break; } } } if (countryCode != null) { // ok, locale with language and country locale = new Locale(langCode, countryCode); } else { // no country available locale = new Locale(langCode); } } } } return locale; }
protected String[] getLanguageList() { if (languageList == null) { String[] langs = Locale.getISOLanguages(); ArrayList<String> sortedLangs = new ArrayList<String>(); for (int i = 0; i < langs.length; i++) { String lang = (new Locale(langs[i])).getDisplayLanguage(Locale.getDefault()); languages.put(lang, langs[i]); sortedLangs.add(lang); } Collections.sort(sortedLangs, Collator.getInstance(Locale.getDefault())); languageList = sortedLangs.toArray(new String[sortedLangs.size()]); } return languageList; }
/** * adds a dictionary folder, so that games can make use of the same translation meachnism * * @param folder classpath folder to add */ public static void addDictionaryFolder(String folder) { for (String language : Locale.getISOLanguages()) { InputStream is = I18N.class.getClassLoader().getResourceAsStream(folder + "/" + language + ".txt"); if (is != null) { Map<String, String> dictionary = dictionaries.get(language); if (dictionary == null) { dictionary = new HashMap<String, String>(); dictionaries.put(language, dictionary); } readFile(is, dictionary); } } }
public List<SelectItem> getAvailableLanguages() { if (availableLanguages == null) { availableLanguages = new ArrayList<SelectItem>(); for (String lang : Locale.getISOLanguages()) { Locale l = new Locale(lang); availableLanguages.add( new SelectItem(lang, l.getDisplayLanguage(Locale.ENGLISH) + " (" + lang + ")")); } Collections.sort( availableLanguages, new Comparator<SelectItem>() { @Override public int compare(SelectItem o1, SelectItem o2) { return o1.getLabel().compareToIgnoreCase(o2.getLabel()); } }); } return availableLanguages; }
public void printAvailableLocales(OutputStream os) throws Exception { Locale[] locales = Locale.getAvailableLocales(); String name = null, country = null, lang = null, currencySb = null; String countryCd = null, langCd = null, currencyCd = null; Currency currency = null; PrintWriter pw = null; try { pw = new PrintWriter(os, true); for (Locale locale : locales) { name = locale.getDisplayName(); country = locale.getDisplayCountry(); countryCd = locale.getISOCountries()[0]; lang = locale.getDisplayLanguage(); langCd = locale.getISOLanguages()[0]; try { currency = Currency.getInstance(locale); currencySb = currency.getSymbol(); currencyCd = currency.getCurrencyCode(); } catch (IllegalArgumentException ex) { currencySb = ""; currencyCd = ""; } pw.print("name : " + name + ", "); pw.print("country : " + country + "(" + countryCd + "), "); pw.print("lang : " + lang + "(" + langCd + "), "); pw.println("currency : " + currencySb + "(" + currencyCd + ")"); } } catch (Exception ex) { throw ex; } finally { if (pw != null && os != (OutputStream) (System.out)) pw.close(); } }
/** * Utility class for Locales and Countries. * * @author Asterios Raptis * @version 1.0 */ public class LocaleUtils { /** Array with all country codes. */ public static final String[] COUNTRY_CODES = Locale.getISOCountries(); /** The Constant LANGUAGE_CODES. */ public static final String[] LANGUAGE_CODES = Locale.getISOLanguages(); /** * Converts the given {@code String} code like "en", "en_US" or "en_US_win" to <b>new</b> {@code * Locale}. * * @param code the code * @return the {@code Locale} object or null. */ public static Locale getLocale(final String code) { if (code == null || code.isEmpty()) { return null; } final String[] splitted = code.split("_"); if (splitted.length == 1) { return new Locale(code); } if (splitted.length == 2) { return new Locale(splitted[0], splitted[1]); } if (splitted.length == 3) { return new Locale(splitted[0], splitted[1], splitted[2]); } return null; } /** * Gets from the given properties file the locale code like "en", "en_US" or "en_US_win". For * instance if the filename is resource_en.properties than it will return "en", if the filename is * resource_en_US.properties than it will return "en_US". If nothing is found than it returns that * the properties file is the "default". * * @param propertiesFile the properties file * @return the locale code */ public static String getLocaleCode(final File propertiesFile) { final String filename = FilenameUtils.getFilenameWithoutExtension(propertiesFile); final int underscoreIndex = filename.indexOf("_"); String stringCode = "default"; if (0 < underscoreIndex) { stringCode = filename.substring(underscoreIndex + 1, filename.length()); } return stringCode; } /** * Gets the locale file name suffix that has the format 'language_COUNTRY_variant' for instance * 'de_DE' for the Locale.GERMANY. * * @param locale the locale * @return the locale name */ public static String getLocaleFilenameSuffix(final Locale locale) { return getLocaleFileSuffix(locale, true, true, false); } /** * Gets the locale file name suffix for instance '_de_DE' for the Locale.GERMANY. * * @param locale the locale * @param withCountry the with country * @return the locale file suffix */ public static String getLocaleFileSuffix(final Locale locale, final boolean withCountry) { return getLocaleFileSuffix(locale, withCountry, false); } /** * Gets the locale file name suffix for instance '_de_DE' for the Locale.GERMANY. * * @param locale the locale * @param withCountry with country * @param withVariant with variant * @return the locale file suffix */ public static String getLocaleFileSuffix( final Locale locale, final boolean withCountry, final boolean withVariant) { return getLocaleFileSuffix(locale, withCountry, withVariant, true); } /** * Gets the locale file name suffix for instance '_de_DE' for the Locale.GERMANY. * * @param locale the locale * @param withCountry with country * @param withVariant with variant * @param withUnderscorePrefix true if the result has to have the underscore prefix * @return the locale file suffix */ public static String getLocaleFileSuffix( final Locale locale, final boolean withCountry, final boolean withVariant, final boolean withUnderscorePrefix) { final StringBuilder localizedName = new StringBuilder(); if (locale != null) { if (locale.getLanguage() != null && !locale.getLanguage().isEmpty()) { if (withUnderscorePrefix) { localizedName.append("_"); } localizedName.append(locale.getLanguage()); } if (withCountry && locale.getCountry() != null && !locale.getCountry().isEmpty()) { localizedName.append("_"); localizedName.append(locale.getCountry()); } if (withVariant && locale.getVariant() != null && !locale.getVariant().isEmpty()) { localizedName.append("_"); localizedName.append(locale.getVariant()); } } return localizedName.toString().trim(); } /** * Gets the locale name for instance 'de_DE' for the Locale.GERMANY. * * @param locale the locale * @return the locale name * @deprecated use instead {@link LocaleUtils#getLocaleFilenameSuffix(Locale)} */ @Deprecated public static String getLocaleName(final Locale locale) { return getLocaleFileSuffix(locale, true, true, false); } /** * Checks the given code if its a valide ISO 3166-1 countrycode. * * @param code The code to check. * @return true if the code is valide otherwise false. */ public static boolean isISOCountryCode(String code) { if (code.length() == 2) { code = code.toUpperCase(); final List<String> lc = Arrays.asList(LocaleUtils.COUNTRY_CODES); return lc.contains(code); } else { return false; } } }
/** {@inheritDoc} */ public boolean isLegalValue(Integer tagID, String value) { if (!super.isLegalValue(tagID, value)) return false; if (value == null || value.length() == 0) return true; switch (tagID) { case IPTC_CATEGORY: final int categoryLength = value.length(); return categoryLength >= 1 && categoryLength <= 3; case IPTC_COUNTRY_CODE: for (String countryCode : java.util.Locale.getISOCountries()) if (value.equalsIgnoreCase(countryCode)) return true; return false; case IPTC_ENVELOPE_PRIORITY: try { final int priority = Integer.parseInt(value); return priority >= 1 && priority <= 9; } catch (NumberFormatException e) { return false; } case IPTC_LANGUAGE_IDENTIFIER: for (String langCode : java.util.Locale.getISOLanguages()) if (value.equalsIgnoreCase(langCode)) return true; return false; case IPTC_OBJECT_CYCLE: return value.length() == 1 && "apb".contains(value); case IPTC_SCENE: try { Integer.parseInt(value); if (value.length() != 6) return false; return getTagValuesFor(tagID, false).contains(value); } catch (NumberFormatException e) { return false; } case IPTC_SUBJECT_CODE: try { Integer.parseInt(value); if (value.length() != 8) return false; return getTagValuesFor(tagID, false).contains(value); } catch (NumberFormatException e) { return false; } case IPTC_URGENCY: try { final int urgency = Integer.parseInt(value); return urgency >= 1 && urgency <= 8; } catch (NumberFormatException e) { return false; } case IPTC_DIGITAL_CREATION_TIME: case IPTC_EXPIRATION_TIME: case IPTC_RELEASE_TIME: case IPTC_TIME_CREATED: case IPTC_TIME_SENT: // TODO case IPTC_INTELLECTUAL_GENRE: case IPTC_SUPPLEMENTAL_CATEGORIES: // TODO default: return true; } }