ente/lib/l10n/l10n.dart

63 lines
1.8 KiB
Dart
Raw Normal View History

2023-04-04 14:03:23 +00:00
import "package:flutter/widgets.dart";
import "package:photos/generated/l10n.dart";
2023-04-10 11:26:44 +00:00
import "package:shared_preferences/shared_preferences.dart";
2023-04-04 14:03:23 +00:00
extension AppLocalizationsX on BuildContext {
S get l10n => S.of(this);
}
// 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-05-08 02:51:16 +00:00
const List<Locale> appSupportedLocales = <Locale>[
Locale('en'),
Locale('es'),
2023-06-29 07:36:09 +00:00
Locale('de'),
2023-08-31 04:31:47 +00:00
Locale('fr'),
2023-05-17 05:53:33 +00:00
Locale('it'),
2023-05-08 02:51:16 +00:00
Locale("nl"),
Locale("zh", "CN"),
];
Locale localResolutionCallBack(locales, supportedLocales) {
for (Locale locale in locales) {
if (appSupportedLocales.contains(locale)) {
return locale;
}
}
// if device language is not supported by the app, use en as default
return const Locale('en');
}
2023-04-10 11:26:44 +00:00
Future<Locale> getLocale() async {
2023-05-02 07:01:07 +00:00
final String? savedValue =
2023-04-10 11:26:44 +00:00
(await SharedPreferences.getInstance()).getString('locale');
2023-05-02 07:01:07 +00:00
// if savedLocale is not null and is supported by the app, return it
if (savedValue != null) {
late Locale savedLocale;
if (savedValue.contains('_')) {
final List<String> parts = savedValue.split('_');
savedLocale = Locale(parts[0], parts[1]);
} else {
savedLocale = Locale(savedValue);
}
if (appSupportedLocales.contains(savedLocale)) {
return savedLocale;
}
2023-04-10 11:26:44 +00:00
}
return const Locale('en');
}
Future<void> setLocale(Locale locale) async {
if (!appSupportedLocales.contains(locale)) {
throw Exception('Locale $locale is not supported by the app');
}
2023-05-02 07:01:07 +00:00
final StringBuffer out = StringBuffer(locale.languageCode);
if (locale.countryCode != null && locale.countryCode!.isNotEmpty) {
out.write('_');
out.write(locale.countryCode);
}
2023-04-10 11:26:44 +00:00
await (await SharedPreferences.getInstance())
2023-05-02 07:01:07 +00:00
.setString('locale', out.toString());
2023-04-10 11:26:44 +00:00
}