-
-
Notifications
You must be signed in to change notification settings - Fork 336
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
[V4]: Migrate to EasyLocalization v4 with improved initialization, asset loading, and storage #742
Open
aissat
wants to merge
11
commits into
develop
Choose a base branch
from
V4
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4ce6054
feat(v4): update example app
aissat bba4524
refactoring `AssetLoader` and `RootBundleAssetLoader`
aissat a784051
feat: create easy_localization_storage_interface.dart
aissat 7d30d13
update easy_localization_app.dart
aissat cb289d1
add export storage interface
aissat e8a7c54
update `AssetLoader` docs
aissat 4af688a
refactoring `EasyLocalizationController` update `initEasyLocation` su…
aissat e982c5a
update generate.dart
aissat 28de687
chore: update pubspec.yaml
aissat ea585d1
chore: create ar.json, ar.json and en.json
aissat b5a12c3
feat(test) :update test
aissat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"test": "اختبار", | ||
"day": { | ||
"zero": "{} يوم", | ||
"one": "{} يوم", | ||
"two": "{} أيام", | ||
"few": "{} أيام", | ||
"many": "{} يوم", | ||
"other": "{} يوم" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"test": "اختبار", | ||
"day": { | ||
"zero": "{} يوم", | ||
"one": "{} يوم", | ||
"two": "{} أيام", | ||
"few": "{} أيام", | ||
"many": "{} يوم", | ||
"other": "{} يوم" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"test": "test", | ||
"day": { | ||
"zero": "{} days", | ||
"one": "{} day", | ||
"two": "{} days", | ||
"few": "{} few days", | ||
"many": "{} many days", | ||
"other": "{} other days" | ||
}, | ||
"hat": { | ||
"zero": "no hats", | ||
"one": "one hat", | ||
"two": "two hats", | ||
"few": "few hats", | ||
"many": "many hats", | ||
"other": "other hats" | ||
}, | ||
"hat_other": { | ||
"other": "other hats" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,36 +4,64 @@ import 'dart:ui'; | |
import 'package:easy_localization/easy_localization.dart'; | ||
import 'package:flutter/services.dart'; | ||
|
||
/// abstract class used to building your Custom AssetLoader | ||
/// Example: | ||
/// ``` | ||
///class FileAssetLoader extends AssetLoader { | ||
/// @override | ||
/// Future<Map<String, dynamic>> load(String path, Locale locale) async { | ||
/// final file = File(path); | ||
/// return json.decode(await file.readAsString()); | ||
/// } | ||
///} | ||
/// ``` | ||
/// Abstract class for loading assets. | ||
abstract class AssetLoader { | ||
const AssetLoader(); | ||
Future<Map<String, dynamic>?> load(String path, Locale locale); | ||
/// Path to the assets directory. | ||
/// Example: | ||
/// ```dart | ||
/// path: 'assets/translations', | ||
/// path: 'assets/translations/lang.csv', | ||
/// ``` | ||
final String? path; | ||
|
||
/// List of supported locales. | ||
/// {@macro flutter.widgets.widgetsApp.supportedLocales} | ||
final List<Locale>? supportedLocales; | ||
|
||
/// Constructor for [AssetLoader]. | ||
/// | ||
/// [path] is the path to the assets directory. | ||
/// [supportedLocales] is a list of locales that the assets support. | ||
const AssetLoader({this.path, this.supportedLocales}) | ||
: assert(path != null || supportedLocales != null, | ||
'path or supportedLocales must not be null'); | ||
|
||
/// Loads the assets for the given [locale]. | ||
/// | ||
/// Returns a map of loaded assets. | ||
Future<Map<String, dynamic>> load({Locale? locale}); | ||
} | ||
|
||
/// | ||
/// default used is RootBundleAssetLoader which uses flutter's assetloader | ||
/// The `RootBundleAssetLoader` class is a subclass of `AssetLoader` that uses Flutter's asset loader | ||
/// to load localized JSON files. | ||
/// | ||
class RootBundleAssetLoader extends AssetLoader { | ||
const RootBundleAssetLoader(); | ||
// A custom asset loader that loads assets from the root bundle | ||
|
||
String getLocalePath(String basePath, Locale locale) { | ||
return '$basePath/${locale.toStringWithSeparator(separator: "-")}.json'; | ||
const RootBundleAssetLoader(String path) : super(path: path); | ||
|
||
/// Returns the path for the specified locale | ||
/// | ||
/// The [locale] parameter represents the desired locale. | ||
/// The returned path is based on the [path] of the asset loader | ||
/// and the [locale] with a separator ("-") between language and country. | ||
String getLocalePath(Locale locale) { | ||
return '$path/${locale.toStringWithSeparator(separator: "-")}.json'; | ||
} | ||
|
||
/// | ||
/// Loads the localized JSON file for the given `locale`. | ||
/// | ||
/// Throws an `ArgumentError` if the `locale` is `null`. | ||
/// | ||
@override | ||
Future<Map<String, dynamic>?> load(String path, Locale locale) async { | ||
var localePath = getLocalePath(path, locale); | ||
Future<Map<String, dynamic>> load({Locale? locale}) async { | ||
if (locale == null) throw ArgumentError.notNull('locale'); | ||
var localePath = getLocalePath(locale); | ||
EasyLocalization.logger.debug('Load asset from $path'); | ||
|
||
// Load the asset as a string and decode it as JSON | ||
return json.decode(await rootBundle.loadString(localePath)); | ||
} | ||
Comment on lines
+36
to
66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling for JSON parsing and file loading. The implementation should handle potential errors when loading and parsing JSON files. Consider adding try-catch blocks: @override
Future<Map<String, dynamic>> load({Locale? locale}) async {
if (locale == null) throw ArgumentError.notNull('locale');
var localePath = getLocalePath(locale);
EasyLocalization.logger.debug('Load asset from $path');
- // Load the asset as a string and decode it as JSON
- return json.decode(await rootBundle.loadString(localePath));
+ try {
+ final jsonString = await rootBundle.loadString(localePath);
+ return json.decode(jsonString) as Map<String, dynamic>;
+ } on FlutterError catch (e) {
+ throw AssetLoadException('Failed to load $localePath: ${e.message}');
+ } on FormatException catch (e) {
+ throw AssetLoadException('Invalid JSON in $localePath: ${e.message}');
+ }
}
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🛠️ Refactor suggestion
Consider making locale required in load() method.
While the class structure and documentation look good, making
locale
optional in theload()
method seems inconsistent since implementations likeRootBundleAssetLoader
require it and throw an error if it's null.Consider updating the method signature to:
📝 Committable suggestion