Ejemplo n.º 1
0
  private static String localeToLanguageTag(Locale locale) {
    /*
     * This might seem redundant, but a language can also contain a
     * country/region and a variant. Stating that e.g language
     * "ar" should return "ar" means that "messages_ar.properties"
     * will be used for any country/region and variant of Arabic.
     * This should be true until UMS contains multiple dialects of Arabic,
     * in which case different codes would have to be returned for the
     * different dialects.
     */

    if (locale == null) {
      return null;
    }
    String languageTag = locale.getLanguage();
    if (languageTag != null && !languageTag.isEmpty()) {
      switch (languageTag) {
        case "en":
          if (locale.getCountry().equalsIgnoreCase("GB")) {
            return "en-GB";
          } else {
            return "en-US";
          }
        case "pt":
          if (locale.getCountry().equalsIgnoreCase("BR")) {
            return "pt-BR";
          } else {
            return "pt";
          }
        case "nb":
        case "nn":
          return "no";
        case "cmn":
        case "zh":
          if (locale.getScript().equalsIgnoreCase("Hans")) {
            return "zh-Hans";
          } else if (locale.getCountry().equalsIgnoreCase("CN")
              || locale.getCountry().equalsIgnoreCase("SG")) {
            return "zh-Hans";
          } else {
            return "zh-Hant";
          }
        default:
          return languageTag;
      }
    } else {
      return null;
    }
  }