Easy_localization: Remote translation file with local asset before load?

Created on 28 Jan 2020  路  3Comments  路  Source: aissat/easy_localization

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!

Most helpful comment

All 3 comments

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


@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> 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> 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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

necipk picture necipk  路  5Comments

EmmanuelMess picture EmmanuelMess  路  7Comments

omidraha picture omidraha  路  4Comments

Wilson13 picture Wilson13  路  7Comments

AdelMahmoudHussein picture AdelMahmoudHussein  路  7Comments