Overview
Location:lib/logic/locale_logic.dart
The LocaleLogic class manages app localization, loading translations, and handling locale changes. It integrates with Flutter’s AppLocalizations system and detects the system locale.
Registration
Properties
Access to all localized strings for the current locale.Example:
Returns
true if translations have been loaded, false otherwise.Returns
true if the current locale is English (en).Methods
load()
Loads translations for the appropriate locale. Called during app bootstrap.- Checks
settingsLogic.currentLocale.valuefor saved preference - Falls back to system locale via
findSystemLocale() - Validates locale is in
AppLocalizations.supportedLocales - Defaults to English (
en) if locale not supported - Loads translations via
AppLocalizations.delegate.load() - Saves selected locale to
settingsLogic
en- Englishzh- Chinese (Simplified)
loadIfChanged()
Reloads translations if the locale has changed.locale: The new locale to load
- Only reloads if the locale differs from current
strings.localeName - Validates locale is supported before loading
Usage Examples
Access Localized Strings
Check Current Locale
Debug Locale Override
For testing, you can force a locale in debug mode:Integration with MaterialApp
Fromlib/main.dart:56-73:
Localization Files
Configuration:l10n.yaml
lib/l10n/app_en.arb- English translationslib/l10n/app_zh.arb- Chinese translations
lib/l10n/app_localizations.dart- Base classlib/l10n/app_localizations_en.dart- English implementationlib/l10n/app_localizations_zh.dart- Chinese implementation
System Locale Detection
Usesintl_standalone package to detect the system locale:
en_US, zh_CN, etc. The language code is extracted:
Initialization
Locale is loaded during app bootstrap inAppLogic.bootstrap():
Related
- SettingsLogic - User preferences including locale
- Localization - Adding translations guide
- Theming - Locale-aware typography