setLocale method

Future<void> setLocale(
  1. Locale newLocale
)

Sets a new locale for the application.

The newLocale parameter specifies the locale to apply. Only locales that are in the supportedLocales list will be accepted. If an unsupported locale is provided, the method returns early without making any changes.

If the new locale differs from the current one, it updates the internal state, persists the language code to SharedPreferences, and notifies all listeners about the change.

Parameters:

  • newLocale: The locale to set for the application

Example:

// Set Spanish locale
await localeProvider.setLocale(const Locale('es'));

Implementation

Future<void> setLocale(Locale newLocale) async {
  if (!supportedLocales.contains(newLocale)) {
    return;
  } // Only allow supported locales

  if (_locale != newLocale) {
    _locale = newLocale;
    await _prefs.setString(_selectedLanguageCodeKey, newLocale.languageCode);
    notifyListeners();
  }
}