Beispiel #1
0
  public void TestSimpleResourceInfo() {
    for (int i = 0; i <= MAX_LOCALES; i++) {
      if (dataTable[LANG][i].equals("xx")) continue;

      Locale testLocale = new Locale(dataTable[LANG][i], dataTable[CTRY][i], dataTable[VAR][i]);
      logln("Testing " + testLocale + "...");

      if (!testLocale.getISO3Language().equals(dataTable[LANG3][i]))
        errln(
            "  ISO-3 language code mismatch: "
                + testLocale.getISO3Language()
                + " versus "
                + dataTable[LANG3][i]);
      if (!testLocale.getISO3Country().equals(dataTable[CTRY3][i]))
        errln(
            "  ISO-3 country code mismatch: "
                + testLocale.getISO3Country()
                + " versus "
                + dataTable[CTRY3][i]);
      /*
                  // getLCID() is currently private
                  if (!String.valueOf(testLocale.getLCID()).equals(dataTable[LCID][i]))
                      errln("  LCID mismatch: " + testLocale.getLCID() + " versus "
                                  + dataTable[LCID][i]);
      */
    }
  }
Beispiel #2
0
 private String compatibleLanguageCode(Locale language) {
   final ArrayList<Locale> compatibleLanguages = new ArrayList<Locale>();
   for (Locale supportedLanguage : supportedLanguages) {
     try {
       if (supportedLanguage.getISO3Language().equals(language.getISO3Language())) {
         compatibleLanguages.add(supportedLanguage);
       }
     } catch (MissingResourceException e) {
     } // No 3-letter language code for locale (as in cmn-Hans-CN), which can be ignored in our
       // case
   }
   if (!compatibleLanguages.isEmpty() && language.getCountry().equals(""))
     return compatibleLanguages.get(0).getLanguage(); // Not a country specific locale
   for (Locale compatibleLanguage : compatibleLanguages) {
     try {
       if (compatibleLanguage.getISO3Country().equals(language.getISO3Country())) {
         return compatibleLanguage.getLanguage()
             + "-"
             + compatibleLanguage.getCountry().toUpperCase();
       }
     } catch (MissingResourceException e) {
     } // No 3-letter country code for locale (as in cmn-Hans-CN), which can be ignored in our case
   }
   return compatibleLanguages.isEmpty() ? null : compatibleLanguages.get(0).getLanguage();
 }
 /**
  * Checks if specified locale object is valid
  *
  * @param locale object for validation
  * @return true if locale is available
  */
 public static boolean isValid(Locale locale) {
   try {
     return locale.getISO3Language() != null && locale.getISO3Country() != null;
   } catch (MissingResourceException e) {
     return false;
   }
 }
Beispiel #4
0
  public static void main(final String[] args) {
    if (args.length >= 2) {
      final String localeSetting = args[0];
      System.out.println("Setting Locale to " + localeSetting + "\n");
      Locale.setDefault(new Locale(localeSetting));

      final String timezoneSetting = args[1];
      System.out.println("Setting TimeZone to " + timezoneSetting + "\n");
      TimeZone.setDefault(TimeZone.getTimeZone(timezoneSetting));
    }

    final Locale locale = Locale.getDefault();
    System.out.println("Locale");
    System.out.println("Code: " + locale.toString());
    try {
      System.out.println("Country: " + locale.getISO3Country());
    } catch (final MissingResourceException e) {
      System.out.println("Country: " + e.getMessage());
    }
    try {
      System.out.println("Language: " + locale.getISO3Language());
    } catch (final MissingResourceException e) {
      System.out.println("Language: " + e.getMessage());
    }

    System.out.println("\nTimezone");
    final TimeZone timezone = TimeZone.getDefault();
    System.out.println("Code: " + timezone.getID());
    System.out.println("Name: " + timezone.getDisplayName());
    System.out.println("Offset: " + timezone.getRawOffset() / (1000 * 60 * 60));
    System.out.println("DST: " + timezone.getDSTSavings() / (1000 * 60 * 60));
  }
Beispiel #5
0
 @SuppressWarnings("unchecked")
 public static List<SelectItemOption<String>> getLocaleSelectBoxOptions(Locale[] locale) {
   List<SelectItemOption<String>> options = new ArrayList<SelectItemOption<String>>();
   for (Locale local : locale) {
     String country = local.getISO3Country();
     if (country != null && country.trim().length() > 0)
       options.add(
           new SelectItemOption<String>(
               local.getDisplayCountry() + "(" + local.getDisplayLanguage() + ")", country));
   }
   Collections.sort(options, new SelectComparator());
   return options;
 }
Beispiel #6
0
  /**
   * render display string for an ISO3 location name.
   *
   * @param locationName - ISO3 location name
   * @return display string that contains display country name and display language name. For
   *     example, output for "VNM" would be "Vietnam(Vietnamese)".
   */
  public static String getLocationDisplayString(String locationName) {
    Locale[] avai = Locale.getAvailableLocales();
    Locale locale = null;
    for (Locale l : avai) {
      try {
        if (l.getISO3Country().equalsIgnoreCase(locationName)) {
          locale = l;
          break;
        }
      } catch (MissingResourceException ex) {
        log.debug(
            "Three-letter country abbreviation is not available for locale: " + l.getDisplayName(),
            ex);
      }
    }

    if (locale != null) {
      String country = locale.getISO3Country();
      if (country != null && country.trim().length() > 0)
        return locale.getDisplayCountry() + "(" + locale.getDisplayLanguage() + ")";
    }
    return locationName;
  }
  /**
   * @param locale
   * @param currency
   * @return A clone of {@link NumberFormat} from the instance in the local cache, if the cache does
   *     not contain an instance of a NumberFormat for a given locale and currency one would be
   *     added.
   */
  protected NumberFormat createCurrencyFormat(final Locale locale, final CurrencyModel currency) {
    final String key = locale.getISO3Country() + "_" + currency.getIsocode();

    NumberFormat numberFormat = currencyFormats.get(key);
    if (numberFormat == null) {
      final NumberFormat currencyFormat = createNumberFormat(locale, currency);
      numberFormat = currencyFormats.putIfAbsent(key, currencyFormat);
      if (numberFormat == null) {
        numberFormat = currencyFormat;
      }
    }
    // don't allow multiple references
    return (NumberFormat) numberFormat.clone();
  }
 static String a(Address paramAddress) {
   Locale localLocale = paramAddress.getLocale();
   Object localObject2 = paramAddress.getCountryName();
   Object localObject1 = localObject2;
   if (TextUtils.isEmpty((CharSequence) localObject2)) {
     localObject1 = localLocale.getDisplayCountry(localLocale);
   }
   localObject2 = localObject1;
   if (TextUtils.isEmpty((CharSequence) localObject1)) {
     localObject2 = localLocale.getISO3Country();
   }
   localObject1 = localObject2;
   if (TextUtils.isEmpty((CharSequence) localObject2)) {
     localObject1 = paramAddress.getCountryCode();
   }
   return (String) localObject1;
 }
Beispiel #9
0
  /**
   * @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) {
    }
  }
Beispiel #10
0
  /** @bug 4011756 4011380 */
  public void TestISO3Fallback() {
    Locale test = new Locale("xx", "YY", "");
    boolean gotException = false;
    String result = "";

    try {
      result = test.getISO3Language();
    } catch (MissingResourceException e) {
      gotException = true;
    }
    if (!gotException)
      errln("getISO3Language() on xx_YY returned " + result + " instead of throwing an exception");

    gotException = false;
    try {
      result = test.getISO3Country();
    } catch (MissingResourceException e) {
      gotException = true;
    }
    if (!gotException)
      errln("getISO3Country() on xx_YY returned " + result + " instead of throwing an exception");
  }
Beispiel #11
0
  static String describeLocale(String name) {
    final StringBuilder result = new StringBuilder();
    result.append("<html>");

    final Locale locale = localeByName(name);

    result.append("<p>");
    append(result, "Display Name", locale.getDisplayName());
    append(result, "Localized Display Name", locale.getDisplayName(locale));

    if (locale.getLanguage().length() > 0) {
      String iso3Language = "(not available)";
      try {
        iso3Language = locale.getISO3Language();
      } catch (MissingResourceException ignored) {
      }

      result.append("<p>");
      append(result, "Display Language", locale.getDisplayLanguage());
      append(result, "Localized Display Language", locale.getDisplayLanguage(locale));
      append(result, "2-Letter Language Code", locale.getLanguage());
      append(result, "3-Letter Language Code", iso3Language);
    }
    if (locale.getCountry().length() > 0) {
      String iso3Country = "(not available)";
      try {
        iso3Country = locale.getISO3Country();
      } catch (MissingResourceException ignored) {
      }

      result.append("<p>");
      append(result, "Display Country", locale.getDisplayCountry());
      append(result, "Localized Display Country", locale.getDisplayCountry(locale));
      append(result, "2-Letter Country Code", locale.getCountry());
      append(result, "3-Letter Country Code", iso3Country);
    }
    if (locale.getVariant().length() > 0) {
      result.append("<p>");
      append(result, "Display Variant", locale.getDisplayVariant());
      append(result, "Localized Display Variant", locale.getDisplayVariant(locale));
      append(result, "Variant Code", locale.getVariant());
    }

    result.append("<p><b>Number Formatting</b>");
    describeNumberFormat(result, "Decimal", NumberFormat.getInstance(locale), 1234.5, -1234.5);
    describeNumberFormat(result, "Integer", NumberFormat.getIntegerInstance(locale), 1234, -1234);
    describeNumberFormat(
        result, "Currency", NumberFormat.getCurrencyInstance(locale), 1234.5, -1234.5);
    describeNumberFormat(result, "Percent", NumberFormat.getPercentInstance(locale), 12.3);

    boolean hasLocaleData = hasLocaleData();

    if (!hasLocaleData) {
      result.append("<p><b>Decimal Format Symbols</b>");
      NumberFormat nf = NumberFormat.getInstance(locale);
      if (nf instanceof DecimalFormat) {
        describeDecimalFormatSymbols(result, ((DecimalFormat) nf).getDecimalFormatSymbols());
      } else {
        result.append("(Didn't expect " + nf.getClass() + ".)");
      }
    }

    Date now =
        new Date(); // FIXME: it might be more useful to always show a time in the afternoon, to
    // make 24-hour patterns more obvious.
    result.append("<p><b>Date/Time Formatting</b>");
    describeDateFormat(
        result, "Full Date", DateFormat.getDateInstance(DateFormat.FULL, locale), now);
    describeDateFormat(
        result, "Long Date", DateFormat.getDateInstance(DateFormat.LONG, locale), now);
    describeDateFormat(
        result, "Medium Date", DateFormat.getDateInstance(DateFormat.MEDIUM, locale), now);
    describeDateFormat(
        result, "Short Date", DateFormat.getDateInstance(DateFormat.SHORT, locale), now);
    result.append("<p>");
    describeDateFormat(
        result, "Full Time", DateFormat.getTimeInstance(DateFormat.FULL, locale), now);
    describeDateFormat(
        result, "Long Time", DateFormat.getTimeInstance(DateFormat.LONG, locale), now);
    describeDateFormat(
        result, "Medium Time", DateFormat.getTimeInstance(DateFormat.MEDIUM, locale), now);
    describeDateFormat(
        result, "Short Time", DateFormat.getTimeInstance(DateFormat.SHORT, locale), now);
    result.append("<p>");
    describeDateFormat(
        result,
        "Full Date/Time",
        DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale),
        now);
    describeDateFormat(
        result,
        "Long Date/Time",
        DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale),
        now);
    describeDateFormat(
        result,
        "Medium Date/Time",
        DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale),
        now);
    describeDateFormat(
        result,
        "Short Date/Time",
        DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale),
        now);

    if (!hasLocaleData) {
      result.append("<p><b>Date Format Symbols</b><p>");
      DateFormat edf = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
      DateFormatSymbols edfs = ((SimpleDateFormat) edf).getDateFormatSymbols();
      DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, locale);
      DateFormatSymbols dfs = ((SimpleDateFormat) df).getDateFormatSymbols();
      append(result, "Local Pattern Chars", dfs.getLocalPatternChars());
      append(result, "Am/pm", Arrays.toString(dfs.getAmPmStrings()));
      append(result, "Eras", Arrays.toString(dfs.getEras()));
      append(result, "Months", Arrays.toString(dfs.getMonths()));
      append(result, "Short Months", Arrays.toString(dfs.getShortMonths()));
      append(result, "Weekdays", Arrays.toString(dfs.getWeekdays()));
      append(result, "Short Weekdays", Arrays.toString(dfs.getShortWeekdays()));
    }

    result.append("<p><b>Calendar</b><p>");
    Calendar c = Calendar.getInstance(locale);
    int firstDayOfWeek = c.getFirstDayOfWeek();
    String firstDayOfWeekString = new DateFormatSymbols(locale).getWeekdays()[firstDayOfWeek];
    String englishFirstDayOfWeekString =
        new DateFormatSymbols(Locale.US).getWeekdays()[firstDayOfWeek];
    String firstDayOfWeekDetails = firstDayOfWeek + " '" + firstDayOfWeekString + "'";
    if (!englishFirstDayOfWeekString.equals(firstDayOfWeekString)) {
      firstDayOfWeekDetails += " (" + englishFirstDayOfWeekString + ")";
    }
    append(result, "First Day of the Week", firstDayOfWeekDetails);
    append(result, "Minimal Days in First Week", c.getMinimalDaysInFirstWeek());

    // If this locale specifies a country, check out the currency.
    // Languages don't have currencies; countries do.
    if (!locale.getCountry().equals("")) {
      result.append("<p><b>Currency</b><p>");
      try {
        Currency currency = Currency.getInstance(locale);
        append(result, "ISO 4217 Currency Code", currency.getCurrencyCode());
        append(
            result,
            "Currency Symbol",
            unicodeString(currency.getSymbol(locale)) + " (" + currency.getSymbol(Locale.US) + ")");
        append(result, "Default Fraction Digits", currency.getDefaultFractionDigits());
      } catch (IllegalArgumentException ex) {
        result.append(
            "<p>(This version of Android is unable to return a Currency for this Locale.)");
      }
    }

    result.append("<p><b>Data Availability</b><p>");
    appendAvailability(result, locale, "BreakIterator", BreakIterator.class);
    appendAvailability(result, locale, "Calendar", NumberFormat.class);
    appendAvailability(result, locale, "Collator", Collator.class);
    appendAvailability(result, locale, "DateFormat", DateFormat.class);
    appendAvailability(result, locale, "DateFormatSymbols", DateFormatSymbols.class);
    appendAvailability(result, locale, "DecimalFormatSymbols", DecimalFormatSymbols.class);
    appendAvailability(result, locale, "NumberFormat", NumberFormat.class);

    if (hasLocaleData) {
      result.append("<p><b>libcore.icu.LocaleData</b>");
      try {
        Object enUsData = getLocaleDataInstance(Locale.US);
        Object localeData = getLocaleDataInstance(locale);
        String[] previous;

        result.append("<p>");
        describeStringArray(result, "amPm", enUsData, localeData, null);
        describeStringArray(result, "eras", enUsData, localeData, null);

        result.append("<p>");
        previous = describeStringArray(result, "longMonthNames", enUsData, localeData, null);
        describeStringArray(result, "longStandAloneMonthNames", enUsData, localeData, previous);
        previous = describeStringArray(result, "shortMonthNames", enUsData, localeData, null);
        describeStringArray(result, "shortStandAloneMonthNames", enUsData, localeData, previous);
        previous = describeStringArray(result, "tinyMonthNames", enUsData, localeData, null);
        describeStringArray(result, "tinyStandAloneMonthNames", enUsData, localeData, previous);

        result.append("<p>");
        previous = describeStringArray(result, "longWeekdayNames", enUsData, localeData, null);
        describeStringArray(result, "longStandAloneWeekdayNames", enUsData, localeData, previous);
        previous = describeStringArray(result, "shortWeekdayNames", enUsData, localeData, null);
        describeStringArray(result, "shortStandAloneWeekdayNames", enUsData, localeData, previous);
        previous = describeStringArray(result, "tinyWeekdayNames", enUsData, localeData, null);
        describeStringArray(result, "tinyStandAloneWeekdayNames", enUsData, localeData, previous);

        result.append("<p>");
        describeString(result, "yesterday", enUsData, localeData);
        describeString(result, "today", enUsData, localeData);
        describeString(result, "tomorrow", enUsData, localeData);

        result.append("<p>");
        describeString(result, "timeFormat12", enUsData, localeData);
        describeString(result, "timeFormat24", enUsData, localeData);

        result.append("<p>");
        describeChar(result, "zeroDigit", enUsData, localeData);
        describeChar(result, "decimalSeparator", enUsData, localeData);
        describeChar(result, "groupingSeparator", enUsData, localeData);
        describeChar(result, "patternSeparator", enUsData, localeData);
        describeChar(result, "percent", enUsData, localeData);
        describeChar(result, "perMill", enUsData, localeData);
        describeChar(result, "monetarySeparator", enUsData, localeData);
        describeChar(result, "minusSign", enUsData, localeData);
        describeString(result, "exponentSeparator", enUsData, localeData);
        describeString(result, "infinity", enUsData, localeData);
        describeString(result, "NaN", enUsData, localeData);

      } catch (Exception ex) {
        result.append("(" + ex.getClass().getSimpleName() + " thrown: " + ex.getMessage() + ")");
        System.err.println(ex);
      }
    }

    return result.toString();
  }