initialize static method
Initialize localization.
You can specify the asset path of the Localization file in path
.
Implementation
static Future<void> initialize({
String path = "assets/Localization.csv",
Duration timeout = const Duration(seconds: 5),
String? locale,
}) async {
try {
if (isInitialized) {
return;
}
if (path.isEmpty) {
debugPrint("CSV File path is empty.");
return Future.error("CSV File path is empty.");
}
String csv = await rootBundle.loadString(path).timeout(timeout);
if (csv.isEmpty) {
debugPrint("CSV data is empty.");
return Future.error("CSV data is empty.");
}
final num2lang = <int, String>{};
csv = csv.replaceAll("\r\n", "\n").replaceAll("\r", "\n");
final converted = const CsvToListConverter().convert(csv, eol: "\n");
for (int y = 1; y < converted.length; y++) {
final line = converted[y];
if (line.isEmpty) {
continue;
}
for (int x = 1; x < line.length; x++) {
final cell = line[x];
if (cell.isEmpty) {
continue;
}
if (y == 1) {
final langs = cell.split(":");
_collection[langs.first] = {};
num2lang[x - 1] = langs.first;
} else {
final key = line.first;
final val = line[x];
if (val.isEmpty || key.isEmpty || key.startsWith("#")) {
continue;
}
final doc = _collection[num2lang[x - 1]] ?? {};
doc[key] = val;
}
}
}
} catch (e) {
print("[$path] was not found.");
}
final deviceLocale = Config.locale;
if (deviceLocale.isEmpty) {
return;
}
if (locale.isEmpty) {
_locale = Config.isMobile
? deviceLocale
: Prefs.getString("locale://".toSHA1(), deviceLocale);
if (_locale.isEmpty) {
_locale = "en_US";
}
initializeDateFormatting(_locale);
_language = _locale.split("_").first;
} else {
_locale = locale!;
Prefs.set("locale://".toSHA1(), _locale);
initializeDateFormatting(locale);
_language = _locale.split("_").first;
}
if (_collection.containsKey(_language)) {
__document = _collection[_language];
}
_isInitialized = true;
}