Skip to content
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

assign _savedLocale when _saveLocal() #676

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
- add _supportedLocales in EasyLocalizationController; log and check the deviceLocale when resetLocale;
- add scriptCode to desiredLocale if useOnlyLangCode is true. scriptCode is needed sometimes, for example zh-Hans, zh-Hant
- add savedLocale get method for context. if context.savedLocale is null, then language option is `following system`, i can display the option in user selection form.
- fix the bug: _savedLocale not assigned in _saveLocale(), and notify listeners after _saveLocale() because _savedLocale might be needed
- saveLocale even if the locale not changed, when using _savedLocale as `following system` option and currently system language is en, then change the locale from system to en, won't really change the locale but needs to store it to distinguish the difference of `system` and `english`
- `shouldReload(LocalizationsDelegate<Localization> old)` return true, the LocalizationsDelegate does not reload when return false, tested in my current configuration.

### [3.0.5]

Expand Down
11 changes: 10 additions & 1 deletion lib/src/easy_localization_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ class _EasyLocalizationProvider extends InheritedWidget {
assert(parent.supportedLocales.contains(locale));
await _localeState.setLocale(locale);
}
else {
// given current language en (option is following system, en is the system language)
// then set the locale to en won't change the locale
// but needs change the savedLocale
// savedLocale == null will give a language following the system
// savedLocale == Locale('en') will give english
assert(parent.supportedLocales.contains(locale));
await _localeState.storeLocale(locale);
}
}

/// Clears a saved locale from device storage
Expand Down Expand Up @@ -312,5 +321,5 @@ class _EasyLocalizationDelegate extends LocalizationsDelegate<Localization> {
}

@override
bool shouldReload(LocalizationsDelegate<Localization> old) => false;
bool shouldReload(LocalizationsDelegate<Localization> old) => true;
}
8 changes: 7 additions & 1 deletion lib/src/easy_localization_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,20 @@ class EasyLocalizationController extends ChangeNotifier {

Future<void> setLocale(Locale l) async {
_locale = l;
EasyLocalization.logger('Locale $locale changed');
await _saveLocale(_locale);
await loadTranslations();
notifyListeners();
EasyLocalization.logger('Locale $locale changed');
}

// avoid name conflict with the variable saveLocale
Future<void> storeLocale(Locale? locale) async {
await _saveLocale(_locale);
}

Future<void> _saveLocale(Locale? locale) async {
if (!saveLocale) return;
_savedLocale = locale;
final preferences = await SharedPreferences.getInstance();
await preferences.setString('locale', locale.toString());
EasyLocalization.logger('Locale $locale saved');
Expand Down
Loading