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

Remote translation file with local asset before load? #38

Closed
HugoHeneault opened this issue Jan 28, 2020 · 3 comments
Closed

Remote translation file with local asset before load? #38

HugoHeneault opened this issue Jan 28, 2020 · 3 comments

Comments

@HugoHeneault
Copy link

Been using your plugin for a few hours, works nicely for now. Thanks for your work! 👏

Our client will be updating by herself the translation files after app publishing, but we still need the app to load without an active internet connection.

Would it be possible to have both local asset loading and then remote locale updating?

Thanks!

@danilofuchs
Copy link

This could be solved with #56

@aissat aissat closed this as completed Feb 28, 2020
@HugoHeneault
Copy link
Author

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

@pishguy
Copy link

pishguy commented Nov 14, 2020

@HugoHeneault i get this error in your implemented class

The method 'AssetOrNetworkAssetLoader.load' has fewer positional arguments than those of overridden method 'AssetLoader.load'.
Future<Map<String, dynamic>> load(String localePath) async {
/F:/software/flutter/.pub-cache/hosted/pub.dartlang.org/easy_localization-2.3.3/lib/src/asset_loader.dart:21:32: Context: This is the overridden method ('load').
Future<Map<String, dynamic>> load(String path, Locale locale);

i think it should be:

Future<Map<String, dynamic>> load(String localePath, Locale locale) async {

now i have some problem in this class, after fixing error localeName in load function is empty

String string;
String localeName = localePath.replaceFirst(assetsPath, '').replaceFirst('.json', '');

implemented code:

final String assetsLocalePath = 'resources/langs/';
runApp(EasyLocalization(
  supportedLocales: [
    ...
  ],
  ...
  assetLoader: AssetOrNetworkAssetLoader(
      ...
      localeUrl: (String localeName) {
        print('>>> $localeName'); //<--- EMPTY
        return 'http://192.168.1.103/$localeName';
      }),
  child: StartupApplication(
    theme: initTheme,
  ),
));

and i get unable to load asset: "/resources/langs/" and when we getting SocketException: Failed host lookup error how can load from default asset?

This issue was closed.
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