assetPickerTextDelegateFromLocale function

AssetPickerTextDelegate assetPickerTextDelegateFromLocale(
  1. Locale? locale
)

Obtain the text delegate from the given locale.

Implementation

AssetPickerTextDelegate assetPickerTextDelegateFromLocale(Locale? locale) {
  if (locale == null) {
    return const AssetPickerTextDelegate();
  }

  final String languageCode = locale.languageCode;
  final String? scriptCode = locale.scriptCode;
  final String? countryCode = locale.countryCode;

  final matchedByLanguage = assetPickerTextDelegates.where(
    (e) => e.languageCode == languageCode,
  );
  if (matchedByLanguage.isEmpty) {
    return const AssetPickerTextDelegate();
  }

  final matchedByScript = scriptCode != null
      ? matchedByLanguage.where((e) => e.scriptCode == scriptCode)
      : null;
  if (matchedByScript == null || matchedByScript.isEmpty) {
    return matchedByLanguage.first;
  }

  final matchedByCountry = countryCode != null
      ? matchedByScript.where((e) => e.countryCode == countryCode)
      : null;

  return matchedByCountry?.firstOrNull ?? matchedByScript.first;
}