sanitizeLocale function
Implementation
String sanitizeLocale(String locale,
{bool throwOnError = false, bool verbose = true}) {
if (_sanitizeLocaleCached[locale] != null) {
return _sanitizeLocaleCached[locale]!;
}
var originalLocale = locale;
/// ISO-639 codes
locale = locale.replaceAll('_', '-');
if (locale.contains('-')) {
if (kLangIsoAllowed.contains(locale.toLowerCase())) {
_sanitizeLocaleCached[originalLocale] = locale;
return locale;
}
final lang = locale.split('-')[0];
if (lang.length < 2 || lang.length > 3) {
if (throwOnError) {
throw 'Invalid language code "$locale"';
} else {
return lang;
}
}
final keys = kGoogleLanguages.keys;
if (!keys.contains(lang)) {
if (throwOnError) {
throw 'Invalid language code for GoogleTranslate "$locale"';
} else {
return lang;
}
}
if (verbose) {
trace('Using code ISO-639 "$lang" for locale "$originalLocale"');
}
_sanitizeLocaleCached[originalLocale] = lang;
return lang;
}
_sanitizeLocaleCached[originalLocale] = locale;
return locale;
}