canonicalizedLocale method

String canonicalizedLocale (String aLocale)

Return the name aLocale turned into xx_YY where it might possibly be in the wrong case or with a hyphen instead of an underscore. If aLocale is null, for example, if you tried to get it from IE, return the current system locale.

Implementation

static String canonicalizedLocale(String aLocale) {
  // Locales of length < 5 are presumably two-letter forms, or else malformed.
  // We return them unmodified and if correct they will be found.
  // Locales longer than 6 might be malformed, but also do occur. Do as
  // little as possible to them, but make the '-' be an '_' if it's there.
  // We treat C as a special case, and assume it wants en_ISO for formatting.
  // TODO(alanknight): en_ISO is probably not quite right for the C/Posix
  // locale for formatting. Consider adding C to the formats database.
  if (aLocale == null) return getCurrentLocale();
  if (aLocale == "C") return "en_ISO";
  if (aLocale.length < 5) return aLocale;
  if (aLocale[2] != '-' && (aLocale[2] != '_')) return aLocale;
  var region = aLocale.substring(3);
  // If it's longer than three it's something odd, so don't touch it.
  if (region.length <= 3) region = region.toUpperCase();
  return '${aLocale[0]}${aLocale[1]}_$region';
}