-
-
Notifications
You must be signed in to change notification settings - Fork 363
Ignore _savedLocale on start when saveLocale is false (#430) #683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
Ignore _savedLocale on start when saveLocale is false (aissat#430)
WalkthroughUpdated locale initialization in EasyLocalizationController to prioritize startLocale when saveLocale is disabled or no saved locale exists. Adjusted conditional logic; no public API changes or logging changes. All modifications are confined to lib/src/easy_localization_controller.dart. Changes
Sequence Diagram(s)sequenceDiagram
actor App
participant Controller as EasyLocalizationController
participant SP as SharedPreferences
participant Device as Device Locale
App->>Controller: initialize(saveLocale, startLocale, forceLocale)
Controller->>SP: read('locale')
SP-->>Controller: _savedLocale
alt forceLocale provided
Controller->>App: _locale = forceLocale
else saveLocale && _savedLocale exists
Controller->>App: _locale = _savedLocale
else (!saveLocale || _savedLocale == null) && startLocale provided
Controller->>App: _locale = startLocale (with fallback)
else
Controller->>Device: get device locale
Device-->>Controller: _deviceLocale
Controller->>App: _locale = match or fallback
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~7 minutes Assessment against linked issues
Poem
✨ Finishing Touches🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🔭 Outside diff range comments (1)
lib/src/easy_localization_controller.dart (1)
45-51
: Add targeted tests for locale resolution orderExisting tests cover
saveLocale: false
with a providedstartLocale
, but we’re missing key scenarios around saved locales and device defaults. Please add or verify tests for:
- saveLocale=false, saved locale exists, startLocale provided → startLocale wins
- saveLocale=true, saved locale exists, startLocale provided → savedLocale wins
- saveLocale=false, no startLocale → device locale is used
🧹 Nitpick comments (3)
lib/src/easy_localization_controller.dart (3)
10-15
: Consider namespacing the SharedPreferences key and migrating gracefully.While the logic now ignores _savedLocale when saveLocale=false, using a generic 'locale' key still risks collisions with app-defined values. To avoid interference and be future-proof, consider a namespaced key with backward-compatible migration.
Proposed diffs:
Add a namespaced key constant:
class EasyLocalizationController extends ChangeNotifier { static Locale? _savedLocale; static late Locale _deviceLocale; + static const String _prefsKey = 'easy_localization.locale';
Use the new key when saving:
Future<void> _saveLocale(Locale? locale) async { if (!saveLocale) return; final preferences = await SharedPreferences.getInstance(); - await preferences.setString('locale', locale.toString()); + await preferences.setString(_prefsKey, locale.toString()); EasyLocalization.logger('Locale $locale saved'); }Read with migration (prefer new key; fall back to old key once):
static Future<void> initEasyLocation() async { final preferences = await SharedPreferences.getInstance(); - final strLocale = preferences.getString('locale'); + String? strLocale = preferences.getString(_prefsKey); + strLocale ??= preferences.getString('locale'); // legacy fallback _savedLocale = strLocale?.toLocale();Remove both keys on delete:
Future<void> deleteSaveLocale() async { _savedLocale = null; final preferences = await SharedPreferences.getInstance(); - await preferences.remove('locale'); + await preferences.remove(_prefsKey); + await preferences.remove('locale'); // legacy cleanup EasyLocalization.logger('Saved locale deleted'); }Also applies to: 199-204, 206-213, 215-220
57-64
: Nit: add an explicit log when ignoring a saved locale because saveLocale=false.Helps diagnose startup behavior when a stray saved value exists.
} else { - // From Device Locale + // From Device Locale + if (!saveLocale && _savedLocale != null) { + EasyLocalization.logger( + 'Ignoring saved locale $_savedLocale because saveLocale=false', + ); + } _locale = selectLocaleFrom( supportedLocales, _deviceLocale, fallbackLocale: fallbackLocale, ); }
45-48
: Optional: choose the best supported locale for startLocale.Today this path sets _locale via _getFallbackLocale(..., startLocale) which returns startLocale directly. If startLocale isn’t in supportedLocales, consider selecting the closest supported match (same language, etc.) for consistency with other code paths.
- } else if ((!saveLocale || _savedLocale == null) && startLocale != null) { - _locale = _getFallbackLocale(supportedLocales, startLocale); + } else if ((!saveLocale || _savedLocale == null) && startLocale != null) { + _locale = selectLocaleFrom( + supportedLocales, + startLocale, + // fallback to startLocale if nothing matches; behavior remains similar + fallbackLocale: startLocale, + ); EasyLocalization.logger('Start locale loaded ${_locale.toString()}'); }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
lib/src/easy_localization_controller.dart
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Codacy Static Code Analysis
🔇 Additional comments (1)
lib/src/easy_localization_controller.dart (1)
45-48
: Fix: startLocale now honored when saveLocale=false (resolves #430).The widened condition correctly applies startLocale whenever saving is disabled, even if a saved value exists. Precedence is now: forceLocale > startLocale (when !saveLocale or no saved) > savedLocale > device. LGTM.
Always use startLocale when it's provided and saveLocale is false. Fixes #430
Summary by CodeRabbit