resolve method

String? resolve(
  1. Locale? locale
)

Resolving intl recognizable String by the given locale.

The pattern of returned String for resolved locale should be either (language) or (language)_(COUNTRY).

The flow of resolve should be followed in this order:

  1. If locale is nulled already, it just return null back.
  2. If locale provided it's country code already, the generated String will refer it directly.
  3. Otherwise, it try to find the corresponded country code by isAssigned and apply as a country code.
  4. When the given locale's country code is undefined, no country code is applied.

For example, using default assigned value: zh_Hant to apply DateFormat setting:

final resolvedStr = IntlScriptRecognizer.resolve(const Locale("zh", "Hant")));

print(DateFormat.yMMMMd(resolvedStr) // Applied as `zh_TW` instead
    .add_E()
    .format(DateTime.now()));

The returned DateFormat result will obey the given country code instead of fallback by intl library.

Implementation

String? resolve(Locale? locale) {
  if (locale == null) {
    return null;
  }

  StringBuffer buf = StringBuffer()..write(locale.languageCode);
  if (locale.countryCode != null) {
    buf.write("_${locale.countryCode}");
  } else if (isAssigned(locale)) {
    buf.write("_${_localCountryMapper[locale]}");
  }

  return buf.toString();
}