easy_localization_loader
easy_localization_loader copied to clipboard
HttpAssetLoader()
I am having 3 Yaml files to load over Http
How the HttpAssetLoader() is working? What files does it expect?
Maybe this can help. The files will have to be put similar to the way RootBundleLoader expects. Here JSON is used, you could change it to use YAML.
Let's say one of you support two languages, then you can do something like this.
runApp(
ProviderScope(
child: EasyLocalization(
supportedLocales: const [
Locale('en', 'US'),
Locale('en', 'CA'),
],
path: 'http://localhost:8000/', // Base path is given, file expected to be at 'http://localhost:8000/en-US.json'
fallbackLocale: const Locale('en', 'US'),
assetLoader: NetworkAssetLoader(),
child: const App(),
),
),
);
import 'dart:convert';
import 'dart:developer';
import 'dart:ui';
import 'package:easy_localization/easy_localization.dart';
import 'package:http/http.dart' as http;
class NetworkAssetLoader extends AssetLoader {
@override
Future<Map<String, dynamic>> load(String path, Locale locale) async {
log('easy localization loader: load http $path');
print('Loading ${locale.toLanguageTag()}');
try {
var url = Uri.parse(
'$path${locale.toLanguageTag()}.json',
);
return http
.get(url)
.then((response) => json.decode(response.body.toString()));
} catch (e) {
// Catch network exceptions
return Future.value({});
}
}
}
Hi,
In version 1.0.0 the loader only uses the path without concatenating the locale, resulting in error:
Hello there. Could anyone describe it more? Does it affect launch screen time? What happened if fetch request would failed? Would it fallbacked to local file or what?