toLocale function
Implementation
Locale toLocale(String? locale) {
const defaultLocale = Locale('en', 'US');
if (locale == null || locale.isEmpty) {
return defaultLocale;
}
try {
final parts = locale.split('_');
switch (parts.length) {
case 1:
return Locale(parts[0]);
case 2:
return Locale(parts[0], parts[1]);
default:
return Locale.fromSubtags(
languageCode: parts[0],
countryCode: parts.length > 1 ? parts[1] : null,
scriptCode: parts.length > 2 ? parts[2] : null,
);
}
} catch (e) {
return defaultLocale;
}
}