2023-04-07 08:59:27 +00:00
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2023-02-07 07:23:04 +00:00
|
|
|
|
|
|
|
// list of locales which are enabled for auth app.
|
|
|
|
// Add more language to the list only when at least 90% of the strings are
|
|
|
|
// translated in the corresponding language.
|
2023-02-13 07:10:33 +00:00
|
|
|
const List<Locale> appSupportedLocales = <Locale>[
|
2023-02-07 07:23:04 +00:00
|
|
|
Locale('en'),
|
2023-04-07 08:59:27 +00:00
|
|
|
Locale('de'),
|
|
|
|
Locale('fr'),
|
2023-04-07 12:10:47 +00:00
|
|
|
Locale('it'),
|
2023-02-07 07:23:04 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
Locale localResolutionCallBack(locales, supportedLocales) {
|
2023-04-07 08:59:27 +00:00
|
|
|
// print call stacktrace to identify caller
|
2023-02-07 07:23:04 +00:00
|
|
|
for (Locale locale in locales) {
|
2023-02-13 07:10:33 +00:00
|
|
|
if (appSupportedLocales.contains(locale)) {
|
2023-02-07 07:23:04 +00:00
|
|
|
return locale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if device language is not supported by the app, use en as default
|
|
|
|
return const Locale('en');
|
|
|
|
}
|
2023-04-07 08:59:27 +00:00
|
|
|
|
|
|
|
Future<Locale> getLocale() async {
|
|
|
|
final String? savedLocale =
|
|
|
|
(await SharedPreferences.getInstance()).getString('locale');
|
2023-04-07 12:10:47 +00:00
|
|
|
if (savedLocale != null &&
|
|
|
|
appSupportedLocales.contains(Locale(savedLocale))) {
|
2023-04-07 08:59:27 +00:00
|
|
|
return Locale(savedLocale);
|
|
|
|
}
|
|
|
|
return const Locale('en');
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> setLocale(Locale locale) async {
|
2023-04-07 12:10:47 +00:00
|
|
|
if (!appSupportedLocales.contains(locale)) {
|
|
|
|
throw Exception('Locale $locale is not supported by the app');
|
|
|
|
}
|
2023-04-07 08:59:27 +00:00
|
|
|
await (await SharedPreferences.getInstance())
|
|
|
|
.setString('locale', locale.languageCode);
|
|
|
|
}
|