download method
Download the correct file based on locale and flavor
It will get the localazy CDN config file and then search the right file based on getFileName
or customFileSearch.
Returns null if nothing have been found on localazy
Returns ARB content from cache or from localazy CDN depending if file have changed since last download
Can throw StateError if no file match the given name of getFileName and locale
/flavor
overrideFileName
can be used if you need multiple localization delegate on your flutter app that use one localazy manager but for multiple filek
Implementation
@override
Future<String?> download(String locale, String flavor,
{String? overrideFileName}) async {
if (_cacheConfig.isEmpty) {
await _loadCacheConfig();
}
final remoteConfigFile =
File(path.join(cacheFolder, _remoteConfigFileName));
final now =
DateTime.now().subtract(configCacheDuration).millisecondsSinceEpoch;
String? content;
if (now > (_cacheConfig[_remoteConfigFileName] ?? 0)) {
final response = await http.get(Uri.parse(_configUrl(cdnId)));
if (response.statusCode == 200) {
content = utf8.decode(response.bodyBytes);
await remoteConfigFile.writeAsString(content);
_cacheConfig[_remoteConfigFileName] =
DateTime.now().millisecondsSinceEpoch;
await _saveCacheConfig();
}
} else {
content = await remoteConfigFile.readAsString();
}
if (content == null) {
return null;
}
// then parse the JSON.
final fileToSearch = overrideFileName ?? getFileName(locale, flavor);
//print('$fileToSearch for $locale and $flavor');
final configData = LocalazyConfig.fromJSON(jsonDecode(content));
LocalazyLocale? wantedLocale;
if (customFileSearch == null) {
final wantedFile = configData.files.firstWhereOrNull((element) {
final nameOk = element.file.toLowerCase() == fileToSearch.toLowerCase();
if (flavor.isEmpty || flavor == IntlDelegate.defaultFlavorName) {
return nameOk;
} else {
final flavorOk = element.productFlavors.firstWhereOrNull(
(element) => element.contains(':${flavor}')) !=
null;
final libOk = element.library == flavor;
final moduleOk = element.module == flavor;
final buildOk = element.buildType == flavor;
return nameOk && (flavorOk || libOk || moduleOk || buildOk);
}
});
if (wantedFile == null) {
throw StateError(
'"$fileToSearch" is not found on Localazy for flavor "$flavor"');
}
wantedLocale = wantedFile.locales.firstWhereOrNull((element) {
final fileLocale = element.region.isEmpty
? element.language
: '${element.language}_${element.region}';
return fileLocale.toLowerCase() == locale.toLowerCase();
});
} else {
wantedLocale = customFileSearch!(configData);
}
if (wantedLocale == null) {
throw StateError(
'"$locale" is not found on Localazy for file $fileToSearch and flavor $flavor');
} else {
final timestamp = await _getTimestamp(wantedLocale, fileToSearch);
if (timestamp < wantedLocale.timestamp || timestamp == 0) {
final localeResponse =
await http.get(Uri.parse('$_host${wantedLocale.uri}'));
if (localeResponse.statusCode == 200) {
final content = utf8.decode(localeResponse.bodyBytes);
await _saveInCache(content, wantedLocale, fileToSearch);
await _setTimestamp(
wantedLocale.timestamp, wantedLocale, fileToSearch);
return content;
}
} else {
return _readFromCache(wantedLocale, fileToSearch);
}
}
}