getLocalizations method
Get localization mapping as json for the specified locale
.
Locale json is named according to the locale
languageCode.
For example, the Danish translation is named da
If there is no language resource, null
is returned.
Implementation
@override
Future<Map<String, String>?> getLocalizations(
Locale locale, {
bool refresh = false,
bool cache = true,
}) async {
Map<String, dynamic>? result;
// first try to get local cache
if (!refresh) {
try {
String filename = await _cacheLocalizationFilename(locale);
info('Getting language locale from cache : $filename');
String jsonString = File(filename).readAsStringSync();
result = json.decode(jsonString) as Map<String, dynamic>;
} catch (exception) {
warning(
"Failed to read localization from cache of type '$locale' - $exception");
}
}
// if no local cache (or refresh is true)
if (result == null) {
_assertCarpService();
info('Getting language locale from server. '
'study_id: ${CarpService().app.studyId}, '
'path: ${_getLocalizationsPath(locale)}');
DocumentSnapshot? document =
await CarpService().document(_getLocalizationsPath(locale)).get();
info('Localization downloaded : $document');
result = document?.data;
if (cache && result != null) {
info("Saving localization for '$locale' to local cache.");
try {
final json = jsonEncode(result);
File(await _cacheLocalizationFilename(locale))
.writeAsStringSync(json);
} catch (exception) {
warning("Failed to save local cache for '$locale' - $exception");
}
}
}
return (result != null)
? result.map((key, value) => MapEntry(key, value.toString()))
: null;
}