getLanguageName method

String getLanguageName(
  1. Locale locale,
  2. BuildContext context
)

Returns a human-readable name for the given locale.

This method provides localized display names for supported locales. Currently returns hardcoded names for supported languages, but could be enhanced to use the AppLocalizations strings themselves if a proper dependency injection pattern is implemented.

Parameters:

  • locale: The locale for which to get the display name
  • context: The build context (currently unused but reserved for future enhancements that might use AppLocalizations for language names)

Returns: A human-readable string representing the locale name, or the uppercase language code if the locale is not explicitly supported.

Example:

String name = localeProvider.getLanguageName(Locale('es'), context);
print(name); // Outputs: "Español"

Implementation

String getLanguageName(Locale locale, BuildContext context) {
  // You might want more sophisticated display names
  // For now, we'll use built-in names if possible, or language codes
  // This part would ideally use AppLocalizations itself for language names
  // but that creates a circular dependency if AppLocalizations needs LocaleProvider.
  // So, hardcoding for simplicity here or using a separate map.
  switch (locale.languageCode) {
    case 'en':
      return "English"; // Or AppLocalizations.of(context)!.languageEnglish
    case 'es':
      return "Español"; // Or AppLocalizations.of(context)!.languageSpanish
    case 'ru':
      return "Русский"; // Or AppLocalizations.of(context)!.languageRussian
    default:
      return locale.languageCode.toUpperCase();
  }
}