2020-03-28 16:11:11 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:flutter/services.dart';
|
2021-03-13 18:02:08 +00:00
|
|
|
import 'package:intl/date_symbol_data_local.dart';
|
2020-03-28 16:11:11 +00:00
|
|
|
import 'package:intl/intl_standalone.dart';
|
|
|
|
|
2019-04-21 09:44:07 +00:00
|
|
|
/// Internationalization utilities
|
|
|
|
///
|
|
|
|
/// @author Pierre HUBERT
|
|
|
|
|
2020-03-28 16:11:11 +00:00
|
|
|
String _currLang;
|
|
|
|
Map<String, Map<String, String>> translations;
|
|
|
|
|
|
|
|
/// Initialize translations system
|
|
|
|
///
|
|
|
|
/// This method currently fetch the current user language
|
|
|
|
Future<void> initTranslations() async {
|
|
|
|
_currLang = await findSystemLocale();
|
2021-03-13 18:02:08 +00:00
|
|
|
await initializeDateFormatting(_currLang, null);
|
2020-03-28 16:11:11 +00:00
|
|
|
|
|
|
|
// Load the list of translations
|
|
|
|
final list = Map<String, String>.from(
|
|
|
|
jsonDecode(await rootBundle.loadString("assets/langs.json")));
|
|
|
|
|
|
|
|
translations = new Map();
|
|
|
|
for (final entry in list.entries) {
|
|
|
|
translations[entry.key] = Map<String, String>.from(jsonDecode(
|
|
|
|
await rootBundle.loadString("assets/langs/${entry.value}.json")));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-21 09:44:07 +00:00
|
|
|
/// Translate a string
|
|
|
|
///
|
|
|
|
/// Translate a given [string] into the current language, if available
|
2019-04-23 12:35:41 +00:00
|
|
|
///
|
|
|
|
/// Then apply the list of [args] to the string, each argument name is
|
|
|
|
/// surrounded by '%'
|
|
|
|
String tr(String string, {Map<String, String> args}) {
|
2020-03-28 16:11:11 +00:00
|
|
|
// Check if a translation is available
|
|
|
|
if (_currLang != null &&
|
|
|
|
translations.containsKey(_currLang) &&
|
|
|
|
translations[_currLang].containsKey(string))
|
|
|
|
string = translations[_currLang][string];
|
2019-04-23 12:35:41 +00:00
|
|
|
|
|
|
|
//Apply arguments
|
2020-03-28 16:11:11 +00:00
|
|
|
if (args != null)
|
2019-04-23 12:35:41 +00:00
|
|
|
args.forEach((key, value) => string = string.replaceAll("%$key%", value));
|
|
|
|
|
2019-04-21 09:44:07 +00:00
|
|
|
return string;
|
2019-04-23 12:35:41 +00:00
|
|
|
}
|