import '../i18n/strings.g.dart'; import '../security/secret_store.dart'; /// Remembers the language the user picked in Settings so it survives restarts. /// An empty/absent value means "follow the device language". Backed by the OS /// keystore (via [SecretStore]), like the rest of the app's small preferences — /// honouring "no plaintext at rest". /// /// Why persist at all: the device language picker is an unreliable way to reach /// minority languages (Android often doesn't offer Asturian, and slang would /// fall back to the device default on every launch), so the choice made in-app /// must be remembered explicitly. Asturian itself is modelled as the `es-AST` /// locale (Spanish language + `AST` region), not `ast`, so Flutter's /// Material/Cupertino localizations — which don't ship `ast` — resolve it /// through Spanish instead of throwing. See [SettingsScreen] for the picker. class LocaleStore { LocaleStore(this._store); final SecretStore _store; static const _key = 'tane.locale'; /// The saved locale, or null when the user follows the device language. /// Unknown tags (e.g. a locale removed in a later build) also read as null. Future saved() async { final raw = await _store.read(_key); if (raw == null || raw.isEmpty) return null; for (final locale in AppLocale.values) { if (locale.name == raw) return locale; } return null; } /// Persists [locale] and applies it immediately. Future setLocale(AppLocale locale) async { await _store.write(_key, locale.name); await LocaleSettings.setLocale(locale); } /// Forgets the explicit choice and follows the device language from now on. Future useDeviceLocale() async { await _store.write(_key, ''); await LocaleSettings.useDeviceLocale(); } }