Beispiel #1
0
  public static LanguageTag parseLocale(BaseLocale baseLocale, LocaleExtensions localeExtensions) {
    LanguageTag tag = new LanguageTag();

    String language = baseLocale.getLanguage();
    String script = baseLocale.getScript();
    String region = baseLocale.getRegion();
    String variant = baseLocale.getVariant();

    boolean hasSubtag = false;

    String privuseVar = null; // store ill-formed variant subtags

    if (isLanguage(language)) {
      // Convert a deprecated language code to its new code
      if (language.equals("iw")) {
        language = "he";
      } else if (language.equals("ji")) {
        language = "yi";
      } else if (language.equals("in")) {
        language = "id";
      }
      tag.language = language;
    }

    if (isScript(script)) {
      tag.script = canonicalizeScript(script);
      hasSubtag = true;
    }

    if (isRegion(region)) {
      tag.region = canonicalizeRegion(region);
      hasSubtag = true;
    }

    // Special handling for no_NO_NY - use nn_NO for language tag
    if (tag.language.equals("no") && tag.region.equals("NO") && variant.equals("NY")) {
      tag.language = "nn";
      variant = "";
    }

    if (variant.length() > 0) {
      List<String> variants = null;
      StringTokenIterator varitr = new StringTokenIterator(variant, BaseLocale.SEP);
      while (!varitr.isDone()) {
        String var = varitr.current();
        if (!isVariant(var)) {
          break;
        }
        if (variants == null) {
          variants = new ArrayList<>();
        }
        variants.add(var); // Do not canonicalize!
        varitr.next();
      }
      if (variants != null) {
        tag.variants = variants;
        hasSubtag = true;
      }
      if (!varitr.isDone()) {
        // ill-formed variant subtags
        StringJoiner sj = new StringJoiner(SEP);
        while (!varitr.isDone()) {
          String prvv = varitr.current();
          if (!isPrivateuseSubtag(prvv)) {
            // cannot use private use subtag - truncated
            break;
          }
          sj.add(prvv);
          varitr.next();
        }
        if (sj.length() > 0) {
          privuseVar = sj.toString();
        }
      }
    }

    List<String> extensions = null;
    String privateuse = null;

    if (localeExtensions != null) {
      Set<Character> locextKeys = localeExtensions.getKeys();
      for (Character locextKey : locextKeys) {
        Extension ext = localeExtensions.getExtension(locextKey);
        if (isPrivateusePrefixChar(locextKey)) {
          privateuse = ext.getValue();
        } else {
          if (extensions == null) {
            extensions = new ArrayList<>();
          }
          extensions.add(locextKey.toString() + SEP + ext.getValue());
        }
      }
    }

    if (extensions != null) {
      tag.extensions = extensions;
      hasSubtag = true;
    }

    // append ill-formed variant subtags to private use
    if (privuseVar != null) {
      if (privateuse == null) {
        privateuse = PRIVUSE_VARIANT_PREFIX + SEP + privuseVar;
      } else {
        privateuse =
            privateuse
                + SEP
                + PRIVUSE_VARIANT_PREFIX
                + SEP
                + privuseVar.replace(BaseLocale.SEP, SEP);
      }
    }

    if (privateuse != null) {
      tag.privateuse = privateuse;
    }

    if (tag.language.length() == 0 && (hasSubtag || privateuse == null)) {
      // use lang "und" when 1) no language is available AND
      // 2) any of other subtags other than private use are available or
      // no private use tag is available
      tag.language = UNDETERMINED;
    }

    return tag;
  }