You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
this library can be support getting locale from network when its available?
it seems this class don't work correctly and i get some error. reference
finalString 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);
classAssetOrNetworkAssetLoaderextendsAssetLoader {
/// The local URL method which accepts a [localeName] and returns the full distant /// localUrlfinalFunction localeUrl;
/// The timeout for downloading the distant localization filesfinalDuration timeout;
/// The default path for assets json filesfinalString assetsPath;
/// The duration before refreshing local localizations files with distant onesfinalDuration localCacheDuration;
AssetOrNetworkAssetLoader(
{@requiredthis.localeUrl,
this.timeout,
this.assetsPath,
this.localCacheDuration});
@overrideFuture<Map<String, dynamic>> load(String localePath) async {
String string;
String localeName =
localePath.replaceFirst(assetsPath, '').replaceFirst('.json', '');
// try loading local previously-saved localization fileif (awaitlocalTranslationExists(localeName)) {
string =awaitloadFromLocalFile(localeName);
}
// no local or failed, check if internet and download the fileif (string ==null&&awaitisInternetConnectionAvailable()) {
string =awaitloadFromNetwork(localeName);
}
// local cache duration was reached or no internet access but prefer local file to assetsif (string ==null&&awaitlocalTranslationExists(localeName, ignoreCacheDuration:true)) {
string =awaitloadFromLocalFile(localeName);
}
// still nothing? Load from assetsif (string ==null) {
string =await rootBundle.loadString(localePath);
}
// then returns the json filereturn json.decode(string);
}
@overrideFuture<bool> localeExists(String localePath) =>Future.value(true);
Future<bool> isInternetConnectionAvailable() async {
ConnectivityResult connectivityResult =await (Connectivity().checkConnectivity());
if (connectivityResult ==ConnectivityResult.none) returnfalse;
final result =awaitFuture.any([
InternetAddress.lookup('example.com'),
Future.delayed(timeout ?? defaultTimeout)
]);
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
returntrue;
}
returnfalse;
}
Future<String> loadFromNetwork(String localeName) async {
String url =localeUrl(localeName);
try {
final response =awaitFuture.any(
[http.get(url), Future.delayed(timeout ?? defaultTimeout)]);
if (response !=null&& response.statusCode ==200) {
String content = response.body.toString();
// check valid json before saving itif (json.decode(content) !=null) {
awaitsaveTranslation(localeName, content);
return content;
}
}
} catch (e) {
print(e.toString());
}
returnnull;
}
Future<bool> localTranslationExists(String localeName,
{bool ignoreCacheDuration:false}) async {
File translationFile =awaitgetFileForLocale(localeName);
if (!await translationFile.exists()) {
returnfalse;
}
// don't check file's ageif (!ignoreCacheDuration) {
Duration difference =DateTime.now().difference(await translationFile.lastModified());
if (difference > (localCacheDuration ??Duration(days:1))) {
returnfalse;
}
}
returntrue;
}
Future<String> loadFromLocalFile(String localeName) async {
returnawait (awaitgetFileForLocale(localeName)).readAsString();
}
Future<void> saveTranslation(String localeName, String content) async {
File file =awaitnewFile(awaitgetFilenameForLocale(localeName))
.create(recursive:true);
return file.writeAsString(content);
}
Future<String> get _localPath async {
final directory =awaitgetTemporaryDirectory();
return directory.path;
}
Future<String> getFilenameForLocale(String localeName) async {
return"${await_localPath}/langs$localeName.json";
}
Future<File> getFileForLocale(String localeName) async {
returnFile(awaitgetFilenameForLocale(localeName));
}
}
The text was updated successfully, but these errors were encountered:
this library can be support getting locale from network when its available?
it seems this class don't work correctly and i get some error. reference
The loader:
The text was updated successfully, but these errors were encountered: