From df0eaac9ab6384facf02cb3b88cf855d76afd449 Mon Sep 17 00:00:00 2001 From: phamconganh Date: Mon, 28 Oct 2024 11:20:24 +0700 Subject: [PATCH] add storate and export controller --- example/lib/main.dart | 58 +++++++--------- .../Flutter/GeneratedPluginRegistrant.swift | 2 - example/pubspec.yaml | 2 + lib/src/asset_loader.dart | 1 + lib/src/easy_localization_app.dart | 5 +- lib/src/easy_localization_controller.dart | 44 ++++++++---- lib/src/utils.dart | 33 --------- packages/easy_localization_helper/.gitignore | 29 ++++++++ packages/easy_localization_helper/.metadata | 10 +++ .../easy_localization_helper/CHANGELOG.md | 3 + packages/easy_localization_helper/LICENSE | 1 + packages/easy_localization_helper/README.md | 39 +++++++++++ .../analysis_options.yaml | 4 ++ .../lib/easy_localization_helper.dart | 36 ++++++++++ .../easy_localization_helper/pubspec.yaml | 55 +++++++++++++++ .../test/easy_localization_helper_test.dart | 12 ++++ .../.gitignore | 29 ++++++++ .../.metadata | 10 +++ .../CHANGELOG.md | 3 + .../LICENSE | 1 + .../README.md | 39 +++++++++++ .../analysis_options.yaml | 4 ++ ...calization_shared_preferences_storage.dart | 3 + ...calization_shared_preferences_storage.dart | 68 +++++++++++++++++++ .../pubspec.yaml | 61 +++++++++++++++++ ...ation_shared_preferences_storage_test.dart | 12 ++++ packages/easy_localization_storage/.gitignore | 29 ++++++++ packages/easy_localization_storage/.metadata | 10 +++ .../easy_localization_storage/CHANGELOG.md | 3 + packages/easy_localization_storage/LICENSE | 1 + packages/easy_localization_storage/README.md | 39 +++++++++++ .../analysis_options.yaml | 4 ++ .../lib/easy_localization_storage.dart | 4 ++ .../easy_localization_in_memory_storage.dart | 27 ++++++++ .../lib/src/easy_localization_storage.dart | 18 +++++ .../easy_localization_storage/pubspec.yaml | 55 +++++++++++++++ .../test/easy_localization_storage_test.dart | 12 ++++ pubspec.yaml | 7 +- test/easy_localization_context_test.dart | 12 ++-- test/easy_localization_test.dart | 51 +++++++------- test/easy_localization_utils_test.dart | 1 + test/easy_localization_widget_test.dart | 33 ++++----- 42 files changed, 728 insertions(+), 142 deletions(-) create mode 100644 packages/easy_localization_helper/.gitignore create mode 100644 packages/easy_localization_helper/.metadata create mode 100644 packages/easy_localization_helper/CHANGELOG.md create mode 100644 packages/easy_localization_helper/LICENSE create mode 100644 packages/easy_localization_helper/README.md create mode 100644 packages/easy_localization_helper/analysis_options.yaml create mode 100644 packages/easy_localization_helper/lib/easy_localization_helper.dart create mode 100644 packages/easy_localization_helper/pubspec.yaml create mode 100644 packages/easy_localization_helper/test/easy_localization_helper_test.dart create mode 100644 packages/easy_localization_shared_preferences_storage/.gitignore create mode 100644 packages/easy_localization_shared_preferences_storage/.metadata create mode 100644 packages/easy_localization_shared_preferences_storage/CHANGELOG.md create mode 100644 packages/easy_localization_shared_preferences_storage/LICENSE create mode 100644 packages/easy_localization_shared_preferences_storage/README.md create mode 100644 packages/easy_localization_shared_preferences_storage/analysis_options.yaml create mode 100644 packages/easy_localization_shared_preferences_storage/lib/easy_localization_shared_preferences_storage.dart create mode 100644 packages/easy_localization_shared_preferences_storage/lib/src/easy_localization_shared_preferences_storage.dart create mode 100644 packages/easy_localization_shared_preferences_storage/pubspec.yaml create mode 100644 packages/easy_localization_shared_preferences_storage/test/easy_localization_shared_preferences_storage_test.dart create mode 100644 packages/easy_localization_storage/.gitignore create mode 100644 packages/easy_localization_storage/.metadata create mode 100644 packages/easy_localization_storage/CHANGELOG.md create mode 100644 packages/easy_localization_storage/LICENSE create mode 100644 packages/easy_localization_storage/README.md create mode 100644 packages/easy_localization_storage/analysis_options.yaml create mode 100644 packages/easy_localization_storage/lib/easy_localization_storage.dart create mode 100644 packages/easy_localization_storage/lib/src/easy_localization_in_memory_storage.dart create mode 100644 packages/easy_localization_storage/lib/src/easy_localization_storage.dart create mode 100644 packages/easy_localization_storage/pubspec.yaml create mode 100644 packages/easy_localization_storage/test/easy_localization_storage_test.dart diff --git a/example/lib/main.dart b/example/lib/main.dart index 402c0611..47d1e7c2 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,4 +1,5 @@ import 'package:easy_localization/easy_localization.dart'; +import 'package:easy_localization_storage/easy_localization_storage.dart'; //import 'package:easy_localization_loader/easy_localization_loader.dart'; // import custom loaders import 'package:flutter/material.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; @@ -8,17 +9,17 @@ import 'lang_view.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); - await EasyLocalization.ensureInitialized(); + await EasyLocalization.ensureInitialized(EasyLocalizationInMemoryStorage()); runApp(EasyLocalization( - supportedLocales: [ + supportedLocales: const [ Locale('en', 'US'), Locale('ar', 'DZ'), Locale('de', 'DE'), Locale('ru', 'RU') ], path: 'resources/langs', - child: MyApp(), + child: const MyApp(), // fallbackLocale: Locale('en', 'US'), // startLocale: Locale('de', 'DE'), // saveLocale: false, @@ -40,6 +41,8 @@ void main() async { } class MyApp extends StatelessWidget { + const MyApp({Key? key}) : super(key: key); + @override Widget build(BuildContext context) { return MaterialApp( @@ -49,18 +52,18 @@ class MyApp extends StatelessWidget { theme: ThemeData( primarySwatch: Colors.blue, ), - home: MyHomePage(title: 'Easy localization'), + home: const MyHomePage(title: 'Easy localization'), ); } } class MyHomePage extends StatefulWidget { - MyHomePage({Key? key, required this.title}) : super(key: key); + const MyHomePage({Key? key, required this.title}) : super(key: key); final String title; @override - _MyHomePageState createState() => _MyHomePageState(); + State createState() => _MyHomePageState(); } class _MyHomePageState extends State { @@ -83,7 +86,7 @@ class _MyHomePageState extends State { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: Text(LocaleKeys.title).tr(), + title: const Text(LocaleKeys.title).tr(), actions: [ TextButton( onPressed: () { @@ -93,10 +96,7 @@ class _MyHomePageState extends State { builder: (_) => LanguageView(), fullscreenDialog: true), ); }, - child: Icon( - Icons.language, - color: Colors.white, - ), + child: const Icon(Icons.language, color: Colors.white), ), ], ), @@ -104,9 +104,7 @@ class _MyHomePageState extends State { child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ - Spacer( - flex: 1, - ), + const Spacer(flex: 1), Text( LocaleKeys.gender_with_arg, style: TextStyle( @@ -124,27 +122,23 @@ class _MyHomePageState extends State { Row( mainAxisAlignment: MainAxisAlignment.center, children: [ - FaIcon(FontAwesomeIcons.male), + const FaIcon(FontAwesomeIcons.male), Switch(value: _gender, onChanged: switchGender), - FaIcon(FontAwesomeIcons.female), + const FaIcon(FontAwesomeIcons.female), ], ), - Spacer( - flex: 1, - ), - Text(LocaleKeys.msg).tr(args: ['aissat', 'Flutter']), - Text(LocaleKeys.msg_named) + const Spacer(flex: 1), + const Text(LocaleKeys.msg).tr(args: ['aissat', 'Flutter']), + const Text(LocaleKeys.msg_named) .tr(namedArgs: {'lang': 'Dart'}, args: ['Easy localization']), - Text(LocaleKeys.clicked).plural(counter), + const Text(LocaleKeys.clicked).plural(counter), TextButton( onPressed: () { incrementCounter(); }, - child: Text(LocaleKeys.clickMe).tr(), - ), - SizedBox( - height: 15, + child: const Text(LocaleKeys.clickMe).tr(), ), + const SizedBox(height: 15), Text( plural(LocaleKeys.amount, counter, format: NumberFormat.currency( @@ -153,24 +147,20 @@ class _MyHomePageState extends State { color: Colors.grey.shade900, fontSize: 18, fontWeight: FontWeight.bold)), - SizedBox( - height: 20, - ), + const SizedBox(height: 20), ElevatedButton( onPressed: () { context.resetLocale(); }, - child: Text(LocaleKeys.reset_locale).tr(), - ), - Spacer( - flex: 1, + child: const Text(LocaleKeys.reset_locale).tr(), ), + const Spacer(flex: 1), ], ), ), floatingActionButton: FloatingActionButton( onPressed: incrementCounter, - child: Text('+1'), + child: const Text('+1'), ), ); } diff --git a/example/macos/Flutter/GeneratedPluginRegistrant.swift b/example/macos/Flutter/GeneratedPluginRegistrant.swift index 724bb2ac..cccf817a 100644 --- a/example/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/example/macos/Flutter/GeneratedPluginRegistrant.swift @@ -5,8 +5,6 @@ import FlutterMacOS import Foundation -import shared_preferences_foundation func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { - SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin")) } diff --git a/example/pubspec.yaml b/example/pubspec.yaml index a055d781..aa04d536 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -25,6 +25,8 @@ dependencies: cupertino_icons: ^1.0.2 easy_localization: path: ../ + easy_localization_storage: + path: ../packages/easy_localization_storage font_awesome_flutter: 9.0.0-nullsafety #custom loaders diff --git a/lib/src/asset_loader.dart b/lib/src/asset_loader.dart index 03cef967..67b43daa 100644 --- a/lib/src/asset_loader.dart +++ b/lib/src/asset_loader.dart @@ -2,6 +2,7 @@ import 'dart:convert'; import 'dart:ui'; import 'package:easy_localization/easy_localization.dart'; +import 'package:easy_localization_helper/easy_localization_helper.dart'; import 'package:flutter/services.dart'; /// abstract class used to building your Custom AssetLoader diff --git a/lib/src/easy_localization_app.dart b/lib/src/easy_localization_app.dart index e6acd2c1..63a71c61 100644 --- a/lib/src/easy_localization_app.dart +++ b/lib/src/easy_localization_app.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:easy_localization/easy_localization.dart'; import 'package:easy_localization/src/easy_localization_controller.dart'; +import 'package:easy_localization_storage/easy_localization_storage.dart'; import 'package:easy_logger/easy_logger.dart'; import 'package:flutter/material.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; @@ -159,8 +160,8 @@ class EasyLocalization extends StatefulWidget { /// ensureInitialized needs to be called in main /// so that savedLocale is loaded and used from the /// start. - static Future ensureInitialized() async => - await EasyLocalizationController.initEasyLocation(); + static Future ensureInitialized(EasyLocalizationStorage storage) async => + await EasyLocalizationController.initEasyLocation(storage); /// Customizable logger static EasyLogger logger = EasyLogger(name: '🌎 Easy Localization'); diff --git a/lib/src/easy_localization_controller.dart b/lib/src/easy_localization_controller.dart index 2e62e2f8..68c3041d 100644 --- a/lib/src/easy_localization_controller.dart +++ b/lib/src/easy_localization_controller.dart @@ -1,18 +1,31 @@ import 'package:easy_localization/easy_localization.dart'; +import 'package:easy_localization_helper/easy_localization_helper.dart'; +import 'package:easy_localization_storage/easy_localization_storage.dart'; import 'package:flutter/material.dart'; import 'package:intl/intl_standalone.dart' if (dart.library.html) 'package:intl/intl_browser.dart'; -import 'package:shared_preferences/shared_preferences.dart'; import 'translations.dart'; class EasyLocalizationController extends ChangeNotifier { static Locale? _savedLocale; static late Locale _deviceLocale; + static EasyLocalizationStorage? _storage; + static EasyLocalizationStorage get storage { + if (_storage == null) { + throw StateError( + 'EasyLocalizationController not initialized. Call initEasyLocation first.'); + } + return _storage!; + } + + static set storage(EasyLocalizationStorage storage) { + _storage = storage; + } late Locale _locale; Locale? _fallbackLocale; - List? _supportedLocales; + late List _supportedLocales; final Function(FlutterError e) onLoadError; final AssetLoader assetLoader; @@ -161,9 +174,11 @@ class EasyLocalizationController extends ChangeNotifier { final result = {}; final loaderFutures = ?>>[]; - // need scriptCode, it might be better to use ignoreCountryCode as the variable name of useOnlyLangCode - final Locale desiredLocale = - useOnlyLangCode ? Locale.fromSubtags(languageCode: locale.languageCode, scriptCode: locale.scriptCode) : locale; + // need scriptCode, it might be better to use ignoreCountryCode as the variable name of useOnlyLangCode + final Locale desiredLocale = useOnlyLangCode + ? Locale.fromSubtags( + languageCode: locale.languageCode, scriptCode: locale.scriptCode) + : locale; List loaders = [ assetLoader, @@ -197,15 +212,13 @@ class EasyLocalizationController extends ChangeNotifier { Future _saveLocale(Locale? locale) async { if (!saveLocale) return; - final preferences = await SharedPreferences.getInstance(); - await preferences.setString('locale', locale.toString()); + await storage.setLocale(value: locale); EasyLocalization.logger('Locale $locale saved'); } - static Future initEasyLocation() async { - final preferences = await SharedPreferences.getInstance(); - final strLocale = preferences.getString('locale'); - _savedLocale = strLocale?.toLocale(); + static Future initEasyLocation(EasyLocalizationStorage storage) async { + EasyLocalizationController.storage = storage; + _savedLocale = await storage.getLocale(); final foundPlatformLocale = await findSystemLocale(); _deviceLocale = foundPlatformLocale.toLocale(); EasyLocalization.logger.debug('Localization initialized'); @@ -213,8 +226,7 @@ class EasyLocalizationController extends ChangeNotifier { Future deleteSaveLocale() async { _savedLocale = null; - final preferences = await SharedPreferences.getInstance(); - await preferences.remove('locale'); + storage.setLocale(); EasyLocalization.logger('Saved locale deleted'); } @@ -222,9 +234,11 @@ class EasyLocalizationController extends ChangeNotifier { Locale? get savedLocale => _savedLocale; Future resetLocale() async { - final locale = selectLocaleFrom(_supportedLocales!, deviceLocale, fallbackLocale: _fallbackLocale); + final locale = selectLocaleFrom(_supportedLocales, deviceLocale, + fallbackLocale: _fallbackLocale); - EasyLocalization.logger('Reset locale to $locale while the platform locale is $_deviceLocale and the fallback locale is $_fallbackLocale'); + EasyLocalization.logger( + 'Reset locale to $locale while the platform locale is $_deviceLocale and the fallback locale is $_fallbackLocale'); await setLocale(locale); } } diff --git a/lib/src/utils.dart b/lib/src/utils.dart index b059b954..52b33fe9 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -29,39 +29,6 @@ String localeToString(Locale locale, {String separator = '_'}) { return locale.toString().split('_').join(separator); } -/// [Easy Localization] locale helper -extension LocaleToStringHelper on Locale { - /// Convert [locale] to String with custom separator - String toStringWithSeparator({String separator = '_'}) { - return toString().split('_').join(separator); - } -} - -/// [Easy Localization] string locale helper -extension StringToLocaleHelper on String { - /// Convert string to [Locale] object - Locale toLocale({String separator = '_'}) { - final localeList = split(separator); - switch (localeList.length) { - case 2: - return localeList.last.length == 4 // scriptCode length is 4 - ? Locale.fromSubtags( - languageCode: localeList.first, - scriptCode: localeList.last, - ) - : Locale(localeList.first, localeList.last); - case 3: - return Locale.fromSubtags( - languageCode: localeList.first, - scriptCode: localeList[1], - countryCode: localeList.last, - ); - default: - return Locale(localeList.first); - } - } -} - extension MapExtension on Map { void addAllRecursive(Map other) { for (final entry in other.entries) { diff --git a/packages/easy_localization_helper/.gitignore b/packages/easy_localization_helper/.gitignore new file mode 100644 index 00000000..ac5aa989 --- /dev/null +++ b/packages/easy_localization_helper/.gitignore @@ -0,0 +1,29 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ +migrate_working_dir/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +#.vscode/ + +# Flutter/Dart/Pub related +# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock. +/pubspec.lock +**/doc/api/ +.dart_tool/ +build/ diff --git a/packages/easy_localization_helper/.metadata b/packages/easy_localization_helper/.metadata new file mode 100644 index 00000000..a567cc46 --- /dev/null +++ b/packages/easy_localization_helper/.metadata @@ -0,0 +1,10 @@ +# This file tracks properties of this Flutter project. +# Used by Flutter tool to assess capabilities and perform upgrades etc. +# +# This file should be version controlled and should not be manually edited. + +version: + revision: "603104015dd692ea3403755b55d07813d5cf8965" + channel: "stable" + +project_type: package diff --git a/packages/easy_localization_helper/CHANGELOG.md b/packages/easy_localization_helper/CHANGELOG.md new file mode 100644 index 00000000..41cc7d81 --- /dev/null +++ b/packages/easy_localization_helper/CHANGELOG.md @@ -0,0 +1,3 @@ +## 0.0.1 + +* TODO: Describe initial release. diff --git a/packages/easy_localization_helper/LICENSE b/packages/easy_localization_helper/LICENSE new file mode 100644 index 00000000..ba75c69f --- /dev/null +++ b/packages/easy_localization_helper/LICENSE @@ -0,0 +1 @@ +TODO: Add your license here. diff --git a/packages/easy_localization_helper/README.md b/packages/easy_localization_helper/README.md new file mode 100644 index 00000000..0dafa321 --- /dev/null +++ b/packages/easy_localization_helper/README.md @@ -0,0 +1,39 @@ + + +A utility package providing shared extensions for the easy_localization ecosystem. This package +centralizes common functionality used by both easy_localization and its storage implementations. + +## Features + +TODO: List what your package can do. Maybe include images, gifs, or videos. + +## Getting started + +TODO: List prerequisites and provide or point to information on how to +start using the package. + +## Usage + +TODO: Include short and useful examples for package users. Add longer examples +to `/example` folder. + +```dart +const like = 'sample'; +``` + +## Additional information + +TODO: Tell users more about the package: where to find more information, how to +contribute to the package, how to file issues, what response they can expect +from the package authors, and more. diff --git a/packages/easy_localization_helper/analysis_options.yaml b/packages/easy_localization_helper/analysis_options.yaml new file mode 100644 index 00000000..a5744c1c --- /dev/null +++ b/packages/easy_localization_helper/analysis_options.yaml @@ -0,0 +1,4 @@ +include: package:flutter_lints/flutter.yaml + +# Additional information about this file can be found at +# https://dart.dev/guides/language/analysis-options diff --git a/packages/easy_localization_helper/lib/easy_localization_helper.dart b/packages/easy_localization_helper/lib/easy_localization_helper.dart new file mode 100644 index 00000000..7843ae75 --- /dev/null +++ b/packages/easy_localization_helper/lib/easy_localization_helper.dart @@ -0,0 +1,36 @@ +library easy_localization_helper; + +import 'dart:ui'; + +/// [Easy Localization] locale helper +extension LocaleToStringHelper on Locale { + /// Convert [locale] to String with custom separator + String toStringWithSeparator({String separator = '_'}) { + return toString().split('_').join(separator); + } +} + +/// [Easy Localization] string locale helper +extension StringToLocaleHelper on String { + /// Convert string to [Locale] object + Locale toLocale({String separator = '_'}) { + final localeList = split(separator); + switch (localeList.length) { + case 2: + return localeList.last.length == 4 // scriptCode length is 4 + ? Locale.fromSubtags( + languageCode: localeList.first, + scriptCode: localeList.last, + ) + : Locale(localeList.first, localeList.last); + case 3: + return Locale.fromSubtags( + languageCode: localeList.first, + scriptCode: localeList[1], + countryCode: localeList.last, + ); + default: + return Locale(localeList.first); + } + } +} diff --git a/packages/easy_localization_helper/pubspec.yaml b/packages/easy_localization_helper/pubspec.yaml new file mode 100644 index 00000000..bd1f1bd5 --- /dev/null +++ b/packages/easy_localization_helper/pubspec.yaml @@ -0,0 +1,55 @@ +name: easy_localization_helper +description: "A new Flutter package project." +version: 0.0.1 +homepage: +publish_to: none + +environment: + sdk: ">=3.0.0 <4.0.0" + flutter: ">=1.17.0" + +dependencies: + flutter: + sdk: flutter + +dev_dependencies: + flutter_test: + sdk: flutter + flutter_lints: ^4.0.0 + +# For information on the generic Dart part of this file, see the +# following page: https://dart.dev/tools/pub/pubspec + +# The following section is specific to Flutter packages. +flutter: + + # To add assets to your package, add an assets section, like this: + # assets: + # - images/a_dot_burr.jpeg + # - images/a_dot_ham.jpeg + # + # For details regarding assets in packages, see + # https://flutter.dev/to/asset-from-package + # + # An image asset can refer to one or more resolution-specific "variants", see + # https://flutter.dev/to/resolution-aware-images + + # To add custom fonts to your package, add a fonts section here, + # in this "flutter" section. Each entry in this list should have a + # "family" key with the font family name, and a "fonts" key with a + # list giving the asset and other descriptors for the font. For + # example: + # fonts: + # - family: Schyler + # fonts: + # - asset: fonts/Schyler-Regular.ttf + # - asset: fonts/Schyler-Italic.ttf + # style: italic + # - family: Trajan Pro + # fonts: + # - asset: fonts/TrajanPro.ttf + # - asset: fonts/TrajanPro_Bold.ttf + # weight: 700 + # + # For details regarding fonts in packages, see + # https://flutter.dev/to/font-from-package diff --git a/packages/easy_localization_helper/test/easy_localization_helper_test.dart b/packages/easy_localization_helper/test/easy_localization_helper_test.dart new file mode 100644 index 00000000..389e8cee --- /dev/null +++ b/packages/easy_localization_helper/test/easy_localization_helper_test.dart @@ -0,0 +1,12 @@ +// import 'package:flutter_test/flutter_test.dart'; + +// import 'package:easy_localization_helper/easy_localization_helper.dart'; + +// void main() { +// test('adds one to input values', () { +// final calculator = Calculator(); +// expect(calculator.addOne(2), 3); +// expect(calculator.addOne(-7), -6); +// expect(calculator.addOne(0), 1); +// }); +// } diff --git a/packages/easy_localization_shared_preferences_storage/.gitignore b/packages/easy_localization_shared_preferences_storage/.gitignore new file mode 100644 index 00000000..ac5aa989 --- /dev/null +++ b/packages/easy_localization_shared_preferences_storage/.gitignore @@ -0,0 +1,29 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ +migrate_working_dir/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +#.vscode/ + +# Flutter/Dart/Pub related +# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock. +/pubspec.lock +**/doc/api/ +.dart_tool/ +build/ diff --git a/packages/easy_localization_shared_preferences_storage/.metadata b/packages/easy_localization_shared_preferences_storage/.metadata new file mode 100644 index 00000000..a567cc46 --- /dev/null +++ b/packages/easy_localization_shared_preferences_storage/.metadata @@ -0,0 +1,10 @@ +# This file tracks properties of this Flutter project. +# Used by Flutter tool to assess capabilities and perform upgrades etc. +# +# This file should be version controlled and should not be manually edited. + +version: + revision: "603104015dd692ea3403755b55d07813d5cf8965" + channel: "stable" + +project_type: package diff --git a/packages/easy_localization_shared_preferences_storage/CHANGELOG.md b/packages/easy_localization_shared_preferences_storage/CHANGELOG.md new file mode 100644 index 00000000..41cc7d81 --- /dev/null +++ b/packages/easy_localization_shared_preferences_storage/CHANGELOG.md @@ -0,0 +1,3 @@ +## 0.0.1 + +* TODO: Describe initial release. diff --git a/packages/easy_localization_shared_preferences_storage/LICENSE b/packages/easy_localization_shared_preferences_storage/LICENSE new file mode 100644 index 00000000..ba75c69f --- /dev/null +++ b/packages/easy_localization_shared_preferences_storage/LICENSE @@ -0,0 +1 @@ +TODO: Add your license here. diff --git a/packages/easy_localization_shared_preferences_storage/README.md b/packages/easy_localization_shared_preferences_storage/README.md new file mode 100644 index 00000000..4a260d8d --- /dev/null +++ b/packages/easy_localization_shared_preferences_storage/README.md @@ -0,0 +1,39 @@ + + +TODO: Put a short description of the package here that helps potential users +know whether this package might be useful for them. + +## Features + +TODO: List what your package can do. Maybe include images, gifs, or videos. + +## Getting started + +TODO: List prerequisites and provide or point to information on how to +start using the package. + +## Usage + +TODO: Include short and useful examples for package users. Add longer examples +to `/example` folder. + +```dart +const like = 'sample'; +``` + +## Additional information + +TODO: Tell users more about the package: where to find more information, how to +contribute to the package, how to file issues, what response they can expect +from the package authors, and more. diff --git a/packages/easy_localization_shared_preferences_storage/analysis_options.yaml b/packages/easy_localization_shared_preferences_storage/analysis_options.yaml new file mode 100644 index 00000000..a5744c1c --- /dev/null +++ b/packages/easy_localization_shared_preferences_storage/analysis_options.yaml @@ -0,0 +1,4 @@ +include: package:flutter_lints/flutter.yaml + +# Additional information about this file can be found at +# https://dart.dev/guides/language/analysis-options diff --git a/packages/easy_localization_shared_preferences_storage/lib/easy_localization_shared_preferences_storage.dart b/packages/easy_localization_shared_preferences_storage/lib/easy_localization_shared_preferences_storage.dart new file mode 100644 index 00000000..ca4b35dd --- /dev/null +++ b/packages/easy_localization_shared_preferences_storage/lib/easy_localization_shared_preferences_storage.dart @@ -0,0 +1,3 @@ +library easy_localization_shared_preferences_storage; + +export 'src/easy_localization_shared_preferences_storage.dart'; diff --git a/packages/easy_localization_shared_preferences_storage/lib/src/easy_localization_shared_preferences_storage.dart b/packages/easy_localization_shared_preferences_storage/lib/src/easy_localization_shared_preferences_storage.dart new file mode 100644 index 00000000..5d7e440c --- /dev/null +++ b/packages/easy_localization_shared_preferences_storage/lib/src/easy_localization_shared_preferences_storage.dart @@ -0,0 +1,68 @@ +import 'dart:ui'; + +import 'package:easy_localization_storage/easy_localization_storage.dart'; +import 'package:easy_localization_helper/easy_localization_helper.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +class EasyLocalizationSharedPreferencesStorage extends EasyLocalizationStorage { + EasyLocalizationSharedPreferencesStorage( + SharedPreferences store, { + this.version = 1, + this.storagePrefix = 'easy_localization.locale', + }) { + _store = store; + versionKey = '$storagePrefix.__v'; + valueKey = '$storagePrefix._v'; + } + late SharedPreferences _store; + + // TODO: create migrate method by version + final int version; + final String storagePrefix; + late final String versionKey; + late final String valueKey; + + @override + Future getVersion() async { + int? value; + try { + value = _store.getInt(versionKey); + } catch (_) {} + return value; + } + + @override + Future setVersion({int? value}) async { + try { + if (value == null) { + _store.remove(versionKey); + } else { + _store.setInt(versionKey, value); + } + } catch (_) {} + } + + @override + Future getLocale() async { + Locale? value; + try { + final raw = _store.getString(valueKey); + if (raw == null) { + return null; + } + return raw.toLocale(); + } catch (_) {} + return value; + } + + @override + Future setLocale({Locale? value}) async { + try { + if (value == null) { + _store.remove(valueKey); + } else { + _store.setString(valueKey, value.toString()); + } + } catch (_) {} + } +} diff --git a/packages/easy_localization_shared_preferences_storage/pubspec.yaml b/packages/easy_localization_shared_preferences_storage/pubspec.yaml new file mode 100644 index 00000000..37c28f83 --- /dev/null +++ b/packages/easy_localization_shared_preferences_storage/pubspec.yaml @@ -0,0 +1,61 @@ +name: easy_localization_shared_preferences_storage +description: "A new Flutter package project." +version: 0.0.1 +homepage: +publish_to: none + +environment: + sdk: ">=3.0.0 <4.0.0" + flutter: ">=1.17.0" + +dependencies: + flutter: + sdk: flutter + + shared_preferences: '>=2.0.0 <3.0.0' + easy_localization_storage: + path: ../easy_localization_storage + easy_localization_helper: + path: ../easy_localization_helper + +dev_dependencies: + flutter_test: + sdk: flutter + flutter_lints: ^4.0.0 + +# For information on the generic Dart part of this file, see the +# following page: https://dart.dev/tools/pub/pubspec + +# The following section is specific to Flutter packages. +flutter: + + # To add assets to your package, add an assets section, like this: + # assets: + # - images/a_dot_burr.jpeg + # - images/a_dot_ham.jpeg + # + # For details regarding assets in packages, see + # https://flutter.dev/to/asset-from-package + # + # An image asset can refer to one or more resolution-specific "variants", see + # https://flutter.dev/to/resolution-aware-images + + # To add custom fonts to your package, add a fonts section here, + # in this "flutter" section. Each entry in this list should have a + # "family" key with the font family name, and a "fonts" key with a + # list giving the asset and other descriptors for the font. For + # example: + # fonts: + # - family: Schyler + # fonts: + # - asset: fonts/Schyler-Regular.ttf + # - asset: fonts/Schyler-Italic.ttf + # style: italic + # - family: Trajan Pro + # fonts: + # - asset: fonts/TrajanPro.ttf + # - asset: fonts/TrajanPro_Bold.ttf + # weight: 700 + # + # For details regarding fonts in packages, see + # https://flutter.dev/to/font-from-package diff --git a/packages/easy_localization_shared_preferences_storage/test/easy_localization_shared_preferences_storage_test.dart b/packages/easy_localization_shared_preferences_storage/test/easy_localization_shared_preferences_storage_test.dart new file mode 100644 index 00000000..af77ed10 --- /dev/null +++ b/packages/easy_localization_shared_preferences_storage/test/easy_localization_shared_preferences_storage_test.dart @@ -0,0 +1,12 @@ +// import 'package:flutter_test/flutter_test.dart'; + +// import 'package:easy_localization_shared_preferences_storage/easy_localization_shared_preferences_storage.dart'; + +// void main() { +// test('adds one to input values', () { +// final calculator = Calculator(); +// expect(calculator.addOne(2), 3); +// expect(calculator.addOne(-7), -6); +// expect(calculator.addOne(0), 1); +// }); +// } diff --git a/packages/easy_localization_storage/.gitignore b/packages/easy_localization_storage/.gitignore new file mode 100644 index 00000000..ac5aa989 --- /dev/null +++ b/packages/easy_localization_storage/.gitignore @@ -0,0 +1,29 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ +migrate_working_dir/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +#.vscode/ + +# Flutter/Dart/Pub related +# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock. +/pubspec.lock +**/doc/api/ +.dart_tool/ +build/ diff --git a/packages/easy_localization_storage/.metadata b/packages/easy_localization_storage/.metadata new file mode 100644 index 00000000..a567cc46 --- /dev/null +++ b/packages/easy_localization_storage/.metadata @@ -0,0 +1,10 @@ +# This file tracks properties of this Flutter project. +# Used by Flutter tool to assess capabilities and perform upgrades etc. +# +# This file should be version controlled and should not be manually edited. + +version: + revision: "603104015dd692ea3403755b55d07813d5cf8965" + channel: "stable" + +project_type: package diff --git a/packages/easy_localization_storage/CHANGELOG.md b/packages/easy_localization_storage/CHANGELOG.md new file mode 100644 index 00000000..41cc7d81 --- /dev/null +++ b/packages/easy_localization_storage/CHANGELOG.md @@ -0,0 +1,3 @@ +## 0.0.1 + +* TODO: Describe initial release. diff --git a/packages/easy_localization_storage/LICENSE b/packages/easy_localization_storage/LICENSE new file mode 100644 index 00000000..ba75c69f --- /dev/null +++ b/packages/easy_localization_storage/LICENSE @@ -0,0 +1 @@ +TODO: Add your license here. diff --git a/packages/easy_localization_storage/README.md b/packages/easy_localization_storage/README.md new file mode 100644 index 00000000..4a260d8d --- /dev/null +++ b/packages/easy_localization_storage/README.md @@ -0,0 +1,39 @@ + + +TODO: Put a short description of the package here that helps potential users +know whether this package might be useful for them. + +## Features + +TODO: List what your package can do. Maybe include images, gifs, or videos. + +## Getting started + +TODO: List prerequisites and provide or point to information on how to +start using the package. + +## Usage + +TODO: Include short and useful examples for package users. Add longer examples +to `/example` folder. + +```dart +const like = 'sample'; +``` + +## Additional information + +TODO: Tell users more about the package: where to find more information, how to +contribute to the package, how to file issues, what response they can expect +from the package authors, and more. diff --git a/packages/easy_localization_storage/analysis_options.yaml b/packages/easy_localization_storage/analysis_options.yaml new file mode 100644 index 00000000..a5744c1c --- /dev/null +++ b/packages/easy_localization_storage/analysis_options.yaml @@ -0,0 +1,4 @@ +include: package:flutter_lints/flutter.yaml + +# Additional information about this file can be found at +# https://dart.dev/guides/language/analysis-options diff --git a/packages/easy_localization_storage/lib/easy_localization_storage.dart b/packages/easy_localization_storage/lib/easy_localization_storage.dart new file mode 100644 index 00000000..e95c7bc1 --- /dev/null +++ b/packages/easy_localization_storage/lib/easy_localization_storage.dart @@ -0,0 +1,4 @@ +library easy_localization_storage; + +export 'src/easy_localization_storage.dart'; +export 'src/easy_localization_in_memory_storage.dart'; diff --git a/packages/easy_localization_storage/lib/src/easy_localization_in_memory_storage.dart b/packages/easy_localization_storage/lib/src/easy_localization_in_memory_storage.dart new file mode 100644 index 00000000..12e68681 --- /dev/null +++ b/packages/easy_localization_storage/lib/src/easy_localization_in_memory_storage.dart @@ -0,0 +1,27 @@ +import 'dart:ui'; + +import 'easy_localization_storage.dart'; + +/// An in-memory implementation of [EasyLocalizationStorage]. +/// Useful for testing. +class EasyLocalizationInMemoryStorage implements EasyLocalizationStorage { + int? _version = 1; + Locale? _locale; + + @override + Future getVersion() async => _version; + + @override + Future setVersion({int? value}) async { + if (value != null && value < 0) { + throw ArgumentError('Value must be null or non-negative'); + } + _version = value; + } + + @override + Future getLocale() async => _locale; + + @override + Future setLocale({Locale? value}) async => _locale = value; +} diff --git a/packages/easy_localization_storage/lib/src/easy_localization_storage.dart b/packages/easy_localization_storage/lib/src/easy_localization_storage.dart new file mode 100644 index 00000000..ff7a28d7 --- /dev/null +++ b/packages/easy_localization_storage/lib/src/easy_localization_storage.dart @@ -0,0 +1,18 @@ +import 'dart:ui'; + +abstract class EasyLocalizationStorage { + Future getVersion(); + Future setVersion({int? value}); + + Future getLocale(); + Future setLocale({Locale? value}); +} + +extension XEasyLocalizationStorage on EasyLocalizationStorage { + Future reset() { + return Future.value([ + setVersion(value: null), + setLocale(value: null), + ]); + } +} diff --git a/packages/easy_localization_storage/pubspec.yaml b/packages/easy_localization_storage/pubspec.yaml new file mode 100644 index 00000000..b2f3b35c --- /dev/null +++ b/packages/easy_localization_storage/pubspec.yaml @@ -0,0 +1,55 @@ +name: easy_localization_storage +description: "A new Flutter package project." +version: 0.0.1 +homepage: +publish_to: none + +environment: + sdk: ">=3.0.0 <4.0.0" + flutter: ">=1.17.0" + +dependencies: + flutter: + sdk: flutter + +dev_dependencies: + flutter_test: + sdk: flutter + flutter_lints: ^4.0.0 + +# For information on the generic Dart part of this file, see the +# following page: https://dart.dev/tools/pub/pubspec + +# The following section is specific to Flutter packages. +flutter: + + # To add assets to your package, add an assets section, like this: + # assets: + # - images/a_dot_burr.jpeg + # - images/a_dot_ham.jpeg + # + # For details regarding assets in packages, see + # https://flutter.dev/to/asset-from-package + # + # An image asset can refer to one or more resolution-specific "variants", see + # https://flutter.dev/to/resolution-aware-images + + # To add custom fonts to your package, add a fonts section here, + # in this "flutter" section. Each entry in this list should have a + # "family" key with the font family name, and a "fonts" key with a + # list giving the asset and other descriptors for the font. For + # example: + # fonts: + # - family: Schyler + # fonts: + # - asset: fonts/Schyler-Regular.ttf + # - asset: fonts/Schyler-Italic.ttf + # style: italic + # - family: Trajan Pro + # fonts: + # - asset: fonts/TrajanPro.ttf + # - asset: fonts/TrajanPro_Bold.ttf + # weight: 700 + # + # For details regarding fonts in packages, see + # https://flutter.dev/to/font-from-package diff --git a/packages/easy_localization_storage/test/easy_localization_storage_test.dart b/packages/easy_localization_storage/test/easy_localization_storage_test.dart new file mode 100644 index 00000000..d980529e --- /dev/null +++ b/packages/easy_localization_storage/test/easy_localization_storage_test.dart @@ -0,0 +1,12 @@ +// import 'package:flutter_test/flutter_test.dart'; + +// import 'package:easy_localization_storage/easy_localization_storage.dart'; + +// void main() { +// test('adds one to input values', () { +// final calculator = Calculator(); +// expect(calculator.addOne(2), 3); +// expect(calculator.addOne(-7), -6); +// expect(calculator.addOne(0), 1); +// }); +// } diff --git a/pubspec.yaml b/pubspec.yaml index f8ecaed3..579e3c23 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -3,7 +3,7 @@ description: Easy and Fast internationalizing and localization your Flutter Apps # author: AISSAT abdelwahab homepage: https://github.com/aissat/easy_localization issue_tracker: https://github.com/aissat/easy_localization/issues -# publish_to: none +publish_to: none version: 3.0.7 @@ -13,11 +13,14 @@ environment: dependencies: flutter: sdk: flutter - shared_preferences: '>=2.0.0 <3.0.0' intl: '>=0.17.0-0 <=0.19.0' args: ^2.3.1 path: ^1.8.1 easy_logger: ^0.0.2 + easy_localization_storage: + path: packages/easy_localization_storage + easy_localization_helper: + path: packages/easy_localization_helper flutter_localizations: sdk: flutter diff --git a/test/easy_localization_context_test.dart b/test/easy_localization_context_test.dart index c0348a97..0dafb7ae 100644 --- a/test/easy_localization_context_test.dart +++ b/test/easy_localization_context_test.dart @@ -1,11 +1,11 @@ import 'dart:io'; import 'package:easy_localization/easy_localization.dart'; +import 'package:easy_localization_storage/easy_localization_storage.dart'; import 'package:easy_logger/easy_logger.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:shared_preferences/shared_preferences.dart'; late BuildContext _context; @@ -46,13 +46,12 @@ void main() async { LevelMessages.warning, ]; - SharedPreferences.setMockInitialValues({}); EasyLocalization.logger.enableLevels = [ LevelMessages.error, LevelMessages.warning, ]; - await EasyLocalization.ensureInitialized(); + await EasyLocalization.ensureInitialized(EasyLocalizationInMemoryStorage()); group('BuildContext', () { testWidgets( @@ -105,10 +104,9 @@ void main() async { group('SharedPreferences deleteSaveLocale()', () { setUpAll(() async { - SharedPreferences.setMockInitialValues({ - 'locale': 'ar_DZ', - }); - await EasyLocalization.ensureInitialized(); + await EasyLocalization.ensureInitialized( + EasyLocalizationInMemoryStorage() + ..setLocale(value: const Locale('ar', 'DZ'))); }); testWidgets( '[EasyLocalization] deleteSaveLocale test', diff --git a/test/easy_localization_test.dart b/test/easy_localization_test.dart index 9078d9ef..9e3594ae 100644 --- a/test/easy_localization_test.dart +++ b/test/easy_localization_test.dart @@ -3,10 +3,11 @@ import 'dart:developer'; import 'package:easy_localization/easy_localization.dart'; import 'package:easy_localization/src/easy_localization_controller.dart'; import 'package:easy_localization/src/localization.dart'; +import 'package:easy_localization_helper/easy_localization_helper.dart'; +import 'package:easy_localization_storage/easy_localization_storage.dart'; import 'package:easy_logger/easy_logger.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:shared_preferences/shared_preferences.dart'; import 'easy_localization_utils_test.dart'; import 'utils/test_asset_loaders.dart'; @@ -132,10 +133,8 @@ void main() { }); test('controller loads saved locale', () async { - SharedPreferences.setMockInitialValues({ - 'locale': 'en', - }); - await EasyLocalization.ensureInitialized(); + await EasyLocalization.ensureInitialized(EasyLocalizationInMemoryStorage() + ..setLocale(value: const Locale('en'))); final controller = EasyLocalizationController( supportedLocales: const [Locale('en'), Locale('fb')], fallbackLocale: const Locale('fb'), @@ -149,17 +148,13 @@ void main() { assetLoader: const JsonAssetLoader(), ); expect(controller.locale, const Locale('en')); - - SharedPreferences.setMockInitialValues({}); }); /// E.g. if user saved a locale that was removed in a later version test('controller loads fallback if saved locale is not supported', () async { - SharedPreferences.setMockInitialValues({ - 'locale': 'de', - }); - await EasyLocalization.ensureInitialized(); + await EasyLocalization.ensureInitialized(EasyLocalizationInMemoryStorage() + ..setLocale(value: const Locale('de'))); final controller = EasyLocalizationController( supportedLocales: const [Locale('en'), Locale('fb')], fallbackLocale: const Locale('fb'), @@ -173,8 +168,6 @@ void main() { assetLoader: const JsonAssetLoader(), ); expect(controller.locale, const Locale('fb')); - - SharedPreferences.setMockInitialValues({}); }); group('locale', () { @@ -226,7 +219,8 @@ void main() { ); }); - test('select best lenguage match if no perfect match exists', () { // #674 + test('select best lenguage match if no perfect match exists', () { + // #674 const userDeviceLocale = Locale('en', 'FR'); const supportedLocale1 = Locale('en', 'US'); const supportedLocale2 = Locale('zh', 'CN'); @@ -241,7 +235,8 @@ void main() { ); }); - test('select perfect match if exists', () { // #674 + test('select perfect match if exists', () { + // #674 const userDeviceLocale = Locale('en', 'GB'); const supportedLocale1 = Locale('en', 'US'); const supportedLocale2 = userDeviceLocale; @@ -556,13 +551,13 @@ void main() { test('two as fallback and fallback translations priority', overridePrint(() { - printLog = []; - expect( - Localization.instance.plural('test_empty_fallback_plurals', 2), - '', - ); - expect(printLog, isEmpty); - })); + printLog = []; + expect( + Localization.instance.plural('test_empty_fallback_plurals', 2), + '', + ); + expect(printLog, isEmpty); + })); test('with number format', () { expect( @@ -643,7 +638,8 @@ void main() { ); }); - test('two as fallback for empty resource and fallback translations priority', + test( + 'two as fallback for empty resource and fallback translations priority', overridePrint(() { printLog = []; expect( @@ -653,8 +649,7 @@ void main() { expect(printLog, isEmpty); })); - test('reports empty plural resource with fallback', - overridePrint(() { + test('reports empty plural resource with fallback', overridePrint(() { printLog = []; expect( Localization.instance.plural('test_empty_fallback_plurals', -1), @@ -677,8 +672,10 @@ void main() { expect(logIterator.current, contains('Localization key [test_empty_plurals.other] not found')); logIterator.moveNext(); - expect(logIterator.current, - contains('Fallback localization key [test_empty_plurals.other] not found')); + expect( + logIterator.current, + contains( + 'Fallback localization key [test_empty_plurals.other] not found')); })); }); diff --git a/test/easy_localization_utils_test.dart b/test/easy_localization_utils_test.dart index cab0ee2a..b09c56bc 100644 --- a/test/easy_localization_utils_test.dart +++ b/test/easy_localization_utils_test.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'package:easy_localization/easy_localization.dart'; +import 'package:easy_localization_helper/easy_localization_helper.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; diff --git a/test/easy_localization_widget_test.dart b/test/easy_localization_widget_test.dart index 895ecbfa..49455fa1 100644 --- a/test/easy_localization_widget_test.dart +++ b/test/easy_localization_widget_test.dart @@ -3,10 +3,10 @@ import 'dart:io'; import 'package:easy_localization/easy_localization.dart'; import 'package:easy_localization/src/exceptions.dart'; import 'package:easy_localization/src/localization.dart'; +import 'package:easy_localization_storage/easy_localization_storage.dart'; import 'package:easy_logger/easy_logger.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:shared_preferences/shared_preferences.dart'; import 'utils/test_asset_loaders.dart'; @@ -71,12 +71,11 @@ class MyLocalizedWidget extends StatelessWidget { } void main() async { - SharedPreferences.setMockInitialValues({}); EasyLocalization.logger.enableLevels = [ LevelMessages.error, LevelMessages.warning, ]; - await EasyLocalization.ensureInitialized(); + await EasyLocalization.ensureInitialized(EasyLocalizationInMemoryStorage()); testWidgets( '[EasyLocalization with JsonAssetLoader] test', @@ -424,11 +423,11 @@ void main() async { ); group('SharedPreferences SavedLocale NULL', () { - setUp(() { - SharedPreferences.setMockInitialValues({ - 'locale': '', - }); - }); + // setUp(() { + // SharedPreferences.setMockInitialValues({ + // 'locale': '', + // }); + // }); testWidgets( '[EasyLocalization] SavedLocale() null locale without country code', @@ -503,10 +502,8 @@ void main() async { group('SharedPreferences saveLocale', () { setUpAll(() async { - SharedPreferences.setMockInitialValues({ - 'locale': 'ar', - }); - await EasyLocalization.ensureInitialized(); + await EasyLocalization.ensureInitialized(EasyLocalizationInMemoryStorage() + ..setLocale(value: const Locale('ar', 'DZ'))); }); testWidgets( @@ -536,10 +533,8 @@ void main() async { group('SharedPreferences saveLocale', () { setUpAll(() async { - SharedPreferences.setMockInitialValues({ - 'locale': 'ar_DZ', - }); - await EasyLocalization.ensureInitialized(); + await EasyLocalization.ensureInitialized(EasyLocalizationInMemoryStorage() + ..setLocale(value: const Locale('ar', 'DZ'))); }); testWidgets( @@ -594,10 +589,8 @@ void main() async { }); group('SharedPreferences deleteSaveLocale()', () { setUpAll(() async { - SharedPreferences.setMockInitialValues({ - 'locale': 'ar_DZ', - }); - await EasyLocalization.ensureInitialized(); + await EasyLocalization.ensureInitialized(EasyLocalizationInMemoryStorage() + ..setLocale(value: const Locale('ar', 'DZ'))); }); testWidgets( '[EasyLocalization] deleteSaveLocale test',