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

Quick follow-up, I don't have time to create a proper example & PR but here is the AssetOrNetworkAssetLoader I used to allow: #276

Closed
pishguy opened this issue Nov 14, 2020 · 0 comments

Comments

@pishguy
Copy link

pishguy commented Nov 14, 2020

Quick follow-up, I don't have time to create a proper example & PR but here is the AssetOrNetworkAssetLoader I used to allow:

  • fetching localization from remote access if internet is available
  • saving the files locally so they are not downloaded very time the app starts up
  • fallbacking to assets if internet connection isn't available

It requires connectivity, http and path_provider packages.

How to use it:

final String assetsLocalePath = 'assets/langs/';
    runApp(EasyLocalization(
      path: assetsLocalePath,
      useOnlyLangCode: true,
      child: MyApp(),
      assetLoader: AssetOrNetworkAssetLoader(
          assetsPath: assetsLocalePath,
          localCacheDuration: Duration(days: 1), // how long the localizations file should be kept
          localeUrl: (String localeName) =>  'http://example.com/$localeName'), // the URL for the remote locale file
      supportedLocales:
          supportedLocales.map((locale) => Locale(locale)).toList(),

The loader:

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:connectivity/connectivity.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart';

const defaultTimeout = Duration(seconds: 1);

class AssetOrNetworkAssetLoader extends AssetLoader {
  /// The local URL method which accepts a [localeName] and returns the full distant
  /// localUrl
  final Function localeUrl;

  /// The timeout for downloading the distant localization files
  final Duration timeout;

  /// The default path for assets json files
  final String assetsPath;

  /// The duration before refreshing local localizations files with distant ones
  final Duration localCacheDuration;

  AssetOrNetworkAssetLoader(
      {@required this.localeUrl,
      this.timeout,
      this.assetsPath,
      this.localCacheDuration});

  @override
  Future<Map<String, dynamic>> load(String localePath) async {
    String string;
    String localeName =
        localePath.replaceFirst(assetsPath, '').replaceFirst('.json', '');

    // try loading local previously-saved localization file
    if (await localTranslationExists(localeName)) {
      string = await loadFromLocalFile(localeName);
    }

    // no local or failed, check if internet and download the file
    if (string == null && await isInternetConnectionAvailable()) {
      string = await loadFromNetwork(localeName);
    }

    // local cache duration was reached or no internet access but prefer local file to assets
    if (string == null &&
        await localTranslationExists(localeName, ignoreCacheDuration: true)) {
      string = await loadFromLocalFile(localeName);
    }

    // still nothing? Load from assets
    if (string == null) {
      string = await rootBundle.loadString(localePath);
    }

    // then returns the json file
    return json.decode(string);
  }

  @override
  Future<bool> localeExists(String localePath) => Future.value(true);

  Future<bool> isInternetConnectionAvailable() async {
    ConnectivityResult connectivityResult =
        await (Connectivity().checkConnectivity());
    if (connectivityResult == ConnectivityResult.none) return false;

    final result = await Future.any([
      InternetAddress.lookup('example.com'),
      Future.delayed(timeout ?? defaultTimeout)
    ]);

    if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
      return true;
    }

    return false;
  }

  Future<String> loadFromNetwork(String localeName) async {
    String url = localeUrl(localeName);

    try {
      final response = await Future.any(
          [http.get(url), Future.delayed(timeout ?? defaultTimeout)]);

      if (response != null && response.statusCode == 200) {
        String content = response.body.toString();

        // check valid json before saving it
        if (json.decode(content) != null) {
          await saveTranslation(localeName, content);
          return content;
        }
      }
    } catch (e) {
      print(e.toString());
    }

    return null;
  }

  Future<bool> localTranslationExists(String localeName,
      {bool ignoreCacheDuration: false}) async {
    File translationFile = await getFileForLocale(localeName);

    if (!await translationFile.exists()) {
      return false;
    }

    // don't check file's age
    if (!ignoreCacheDuration) {
      Duration difference =
          DateTime.now().difference(await translationFile.lastModified());

      if (difference > (localCacheDuration ?? Duration(days: 1))) {
        return false;
      }
    }

    return true;
  }

  Future<String> loadFromLocalFile(String localeName) async {
    return await (await getFileForLocale(localeName)).readAsString();
  }

  Future<void> saveTranslation(String localeName, String content) async {
    File file = await new File(await getFilenameForLocale(localeName))
        .create(recursive: true);
    return file.writeAsString(content);
  }

  Future<String> get _localPath async {
    final directory = await getTemporaryDirectory();

    return directory.path;
  }

  Future<String> getFilenameForLocale(String localeName) async {
    return "${await _localPath}/langs$localeName.json";
  }

  Future<File> getFileForLocale(String localeName) async {
    return File(await getFilenameForLocale(localeName));
  }
}

Originally posted by @HugoHeneault in #38 (comment)

@pishguy pishguy closed this as completed Nov 14, 2020
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

1 participant