Skip to content
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

HttpAssetLoader() #29

Open
chrisvidal opened this issue Sep 1, 2021 · 3 comments
Open

HttpAssetLoader() #29

chrisvidal opened this issue Sep 1, 2021 · 3 comments

Comments

@chrisvidal
Copy link

I am having 3 Yaml files to load over Http

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

@htsv
Copy link

htsv commented Sep 17, 2021

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

@tigrenok00
Copy link

Hi,

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

image

@aminbenarieb
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants