Validates the language tag. Section 2.2.9 of the bcp47 spec
defines a valid tag.

ICU is too permissible and lets invalid tags, like
hant-cmn-cn, through.

Returns false if the language tag is invalid.
function isValidLanguageTag(locale) {
  // Check if it's well-formed, including grandfadered tags.
  if (IS_NULL(InternalRegExpMatch(GetLanguageTagRE(), locale))) {
    return false;
  }

  // Just return if it's a x- form. It's all private.
  if (%_Call(StringIndexOf, locale, 'x-') === 0) {
    return true;
  }

  // Check if there are any duplicate variants or singletons (extensions).

  // Remove private use section.
  locale = %_Call(StringSplit, locale, '-x-')[0];

  // Skip language since it can match variant regex, so we start from 1.
  // We are matching i-klingon here, but that's ok, since i-klingon-klingon
  // is not valid and would fail LANGUAGE_TAG_RE test.
  var variants = new InternalArray();
  var extensions = new InternalArray();
  var parts = %_Call(StringSplit, locale, '-');
  for (var i = 1; i < parts.length; i++) {
    var value = parts[i];
    if (!IS_NULL(InternalRegExpMatch(GetLanguageVariantRE(), value)) &&
        extensions.length === 0) {
      if (%_Call(ArrayIndexOf, variants, value) === -1) {
        %_Call(ArrayPush, variants, value);
      } else {
        return false;
      }
    }

    if (!IS_NULL(InternalRegExpMatch(GetLanguageSingletonRE(), value))) {
      if (%_Call(ArrayIndexOf, extensions, value) === -1) {
        %_Call(ArrayPush, extensions, value);
      } else {
        return false;
      }
    }
  }

  return true;
 }