setLocale method
Set the application locale. The match is by language, and the
configured locale for that language becomes current, so hi_IN sets
the app's hi. Returns false for an unsupported language.
Implementation
Future<bool> setLocale(Locale requested) async {
final config = _getConfig(requested.languageCode);
if (config == null) {
return false;
}
final locale = config.locale;
// 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);
}
}