locale static method

Locale? locale(
  1. DataSource source,
  2. List<Object> key
)

Returns a Locale from the specified string.

The string is split on hyphens ("-").

If the string is null, returns null.

If there is no hyphen in the list, uses the one-argument form of Locale, passing the whole string.

If there is one hyphen in the list, uses the two-argument form of Locale, passing the parts before and after the hyphen respectively.

If there are two or more hyphens, uses the Locale.fromSubtags constructor.

Implementation

static Locale? locale(DataSource source, List<Object> key) {
  final String? value = source.v<String>(key);
  if (value == null) {
    return null;
  }
  final List<String> subtags = value.split('-');
  if (subtags.isEmpty) {
    return null;
  }
  if (subtags.length == 1) {
    return Locale(value);
  }
  if (subtags.length == 2) {
    return Locale(subtags[0], subtags[1]);
  }
  // TODO(ianh): verify this is correct (I tried looking up the Unicode spec but it was... confusing)
  return Locale.fromSubtags(languageCode: subtags[0], scriptCode: subtags[1], countryCode: subtags[2]);
}