Canonicalizes the language tag, or throws in case the tag is invalid.
function canonicalizeLanguageTag(localeID) {
  // null is typeof 'object' so we have to do extra check.
  if ((!IS_STRING(localeID) && !IS_RECEIVER(localeID)) ||
      IS_NULL(localeID)) {
    throw MakeTypeError(kLanguageID);
  }

  // Optimize for the most common case; a language code alone in
  // the canonical form/lowercase (e.g. "en", "fil").
  if (IS_STRING(localeID) &&
      !IS_NULL(InternalRegExpMatch(/^[a-z]{2,3}$/, localeID))) {
    return localeID;
  }

  var localeString = TO_STRING(localeID);

  if (isValidLanguageTag(localeString) === false) {
    throw MakeRangeError(kInvalidLanguageTag, localeString);
  }

  var tag = %CanonicalizeLanguageTag(localeString);
  if (tag === 'invalid-tag') {
    throw MakeRangeError(kInvalidLanguageTag, localeString);
  }

  return tag;
}