-
-
Notifications
You must be signed in to change notification settings - Fork 329
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
Decouple asset loading logic from Localization #56
Conversation
- Helps developer in integration tests - Allows developer to use custom loading logic ( network http ( get/post/put etc ), file, rootbundle, string etcc
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.
I really like the approach you took.
It enables many more integration possibilities. Currently, the library assumes way too much responsibility when it comes to data fetching, with not much benefits.
Also fixes #38
@danilofuchs We could also just expose |
@aissat @danilofuchs Loosing the dependency on rootBundle allows us to unit test |
hi guys this great work, I agree with all code but I have a suggestion to make this code more simple and easy
class StringAssetLoader extends AssetLoader {
@override
Future<String> load(String localePath) {
return Future.value('''{"title" : "Hello"}''');
}
}
class FileAssetLoader extends AssetLoader {
@override
Future<String> load(String localePath) {
File file = File(localePath);
return file.readAsString();
}
}
class NetworkAssetLoader extends AssetLoader {
@override
Future<String> load(String localePath) async {
String url =
"https://raw.githubusercontent.com/aissat/easy_localization/master/example/$localePath";
return http.get(url).then((response) => response.body.toString());
}
}
// asset loader to be used when doing integration tests
// default AssetLoader suffers from this issue
// https://github.com/flutter/flutter/issues/44182
class TestsAssetLoader extends AssetLoader {
@override
Future<String> load(String localePath) async {
final ByteData byteData = await rootBundle.load(localePath);
return utf8.decode(byteData.buffer.asUint8List());
}
} i mean i'll make this in import 'package:path_provider/path_provider.dart';
class FileProvidertLoade extends AssetLoader {
@override
Future<String> load(String localePath) {
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path+"localePath";
.......
}
} |
@aissat agree on this. |
@spiritinlife look last update |
thanks, @spiritinlife that's great work 👍 💯 |
@aissat Thank you for your feedback and your awesome work. I checked your latest commits and added some comments. Example AssetLoaders should be provided in Readme so that the user knows how to proceed. |
@spiritinlife I agree with u about |
Library shouldn't make assumptions about asset loading. For example the recent
useDocumentStorage
shouldn't be added as an option to EasyLocalizationDelegate since its very specific. Allowing the developer to bring their own asset loading logic is the only way to proceed safely.We provide a default asset loader RootBundleAssetLoader and allow the developer to override this default. By doing this we also remove http dependency and the
loadPath
if/else logic.In the example, I have provided asset loaders for Network, File, String, Testing and more can be added.
This also allows developers to do integration testing in their apps since this bug flutter/flutter#44182 right now is an issue for flutter_driver.