setLocale method

Future<bool> setLocale(
  1. Locale locale
)

Set the application locale

Implementation

Future<bool> setLocale(Locale locale) async {
  if (!_isLocaleSupported(locale)) {
    return false;
  }

  // Set loading state
  runInAction(() => isLoadingLocale.value = true);

  try {
    vyuh.log.info(
      'LocalePlugin: Starting locale change from "${currentLocale.value.languageCode}" to "${locale.languageCode}"',
    );

    // IMPORTANT: Load all deferred translation libraries BEFORE updating the observable
    // This ensures translations are ready when the UI rebuilds
    for (final registration in _registrations) {
      try {
        await registration.onLocaleChange(locale);
      } catch (e) {
        vyuh.log.error(
            'LocalePlugin: Error notifying "${registration.name}" of locale change: $e');
      }
    }

    vyuh.log.info(
      'LocalePlugin: All translation systems notified, updating currentLocale to "${locale.languageCode}"',
    );

    // Now update the observable to trigger UI rebuild with loaded translations
    runInAction(() => currentLocale.value = locale);

    vyuh.log.info(
      'LocalePlugin: currentLocale updated, now="${currentLocale.value.languageCode}"',
    );

    // Persist the preference
    await _prefs?.setString(_localePreferenceKey, locale.languageCode);

    return true;
  } finally {
    // Clear loading state
    runInAction(() => isLoadingLocale.value = false);
  }
}