easy_localization_loader icon indicating copy to clipboard operation
easy_localization_loader copied to clipboard

HttpAssetLoader()

Open chrisvidal opened this issue 3 years ago • 3 comments

I am having 3 Yaml files to load over Http

How the HttpAssetLoader() is working? What files does it expect?

chrisvidal avatar Sep 01 '21 08:09 chrisvidal

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({});
    }
  }
}

htsv avatar Sep 17 '21 17:09 htsv

Hi,

In version 1.0.0 the loader only uses the path without concatenating the locale, resulting in error:

image

tigrenok00 avatar Oct 04 '21 08:10 tigrenok00

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?

aminbenarieb avatar May 06 '22 21:05 aminbenarieb