// Author: fruit.com.cn import 'package:flutter/widgets.dart'; import 'package:shared_preferences/shared_preferences.dart'; /// Resolves the selected or system language to one of the supported app locales. /// /// Indonesian is the launch-market fallback when a device uses an unsupported /// language. The legacy `in` code is treated as Indonesian as well. Locale resolveAppLocale(Locale? selected, Locale? deviceLocale) { final code = (selected ?? deviceLocale)?.languageCode; return switch (code) { 'zh' => const Locale('zh'), 'en' => const Locale('en'), _ => const Locale('id'), }; } class LocaleController extends ChangeNotifier { LocaleController({SharedPreferencesAsync? preferences}) : _preferences = preferences ?? SharedPreferencesAsync(); final SharedPreferencesAsync _preferences; Locale? _locale; Locale? get locale => _locale; Future load() async { try { final code = await _preferences.getString('ui.locale'); _locale = switch (code) { 'en' => const Locale('en'), 'zh' => const Locale('zh'), 'id' => const Locale('id'), _ => null, }; notifyListeners(); } catch (_) { // The system locale remains usable if preferences are unavailable. } } Future select(String code) async { _locale = switch (code) { 'en' => const Locale('en'), 'zh' => const Locale('zh'), 'id' => const Locale('id'), _ => null, }; notifyListeners(); try { await _preferences.setString('ui.locale', code); } catch (_) { // Preference persistence is best effort for this prototype. } } }