-
-
Notifications
You must be signed in to change notification settings - Fork 327
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
Comments
This could be solved with #56 |
Quick follow-up, I don't have time to create a proper example & PR but here is the
It requires 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
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 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 |
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!
The text was updated successfully, but these errors were encountered: