Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[auth] Fix auto detection for default locale

Neeraj Guptade435ecf3356
diff --git a/auth/lib/app/view/app.dart b/auth/lib/app/view/app.dart
index 37967d75a6..b2abc6caa0 100644
--- a/auth/lib/app/view/app.dart
+++ b/auth/lib/app/view/app.dart
@@ -23,7 +23,7 @@ import 'package:tray_manager/tray_manager.dart';
import 'package:window_manager/window_manager.dart';
class App extends StatefulWidget {
- final Locale locale;
+ final Locale? locale;
const App({super.key, this.locale = const Locale("en")});
static void setLocale(BuildContext context, Locale newLocale) {
diff --git a/auth/lib/locale.dart b/auth/lib/locale.dart
index c3b33e2988..7b0a34d02a 100644
--- a/auth/lib/locale.dart
+++ b/auth/lib/locale.dart
@@ -26,6 +26,7 @@ const List<Locale> appSupportedLocales = <Locale>[
Locale("zh", "CN"),
];
+Locale? autoDetectedLocale;
Locale localResolutionCallBack(locales, supportedLocales) {
Locale? languageCodeMatch;
final Map<String, Locale> languageCodeToLocale = {
@@ -35,12 +36,14 @@ Locale localResolutionCallBack(locales, supportedLocales) {
for (Locale locale in locales) {
if (appSupportedLocales.contains(locale)) {
+ autoDetectedLocale = locale;
return locale;
}
if (languageCodeMatch == null &&
languageCodeToLocale.containsKey(locale.languageCode)) {
languageCodeMatch = languageCodeToLocale[locale.languageCode];
+ autoDetectedLocale = languageCodeMatch;
}
}
@@ -48,7 +51,9 @@ Locale localResolutionCallBack(locales, supportedLocales) {
return languageCodeMatch ?? const Locale('en');
}
-Future<Locale> getLocale() async {
+Future<Locale?> getLocale({
+ bool noFallback = false,
+}) async {
final String? savedValue =
(await SharedPreferences.getInstance()).getString('locale');
// if savedLocale is not null and is supported by the app, return it
@@ -64,6 +69,12 @@ Future<Locale> getLocale() async {
return savedLocale;
}
}
+ if (autoDetectedLocale != null) {
+ return autoDetectedLocale!;
+ }
+ if (noFallback) {
+ return null;
+ }
return const Locale('en');
}
diff --git a/auth/lib/main.dart b/auth/lib/main.dart
index 6d21f371d6..f812960a9a 100644
--- a/auth/lib/main.dart
+++ b/auth/lib/main.dart
@@ -102,7 +102,7 @@ Future<void> _runInForeground() async {
return await _runWithLogs(() async {
_logger.info("Starting app in foreground");
await _init(false, via: 'mainMethod');
- final Locale locale = await getLocale();
+ final Locale? locale = await getLocale(noFallback: true);
unawaited(UpdateService.instance.showUpdateNotification());
runApp(
AppLock(
diff --git a/auth/lib/onboarding/view/onboarding_page.dart b/auth/lib/onboarding/view/onboarding_page.dart
index dff1fbc179..0061dacf3a 100644
--- a/auth/lib/onboarding/view/onboarding_page.dart
+++ b/auth/lib/onboarding/view/onboarding_page.dart
@@ -108,7 +108,7 @@ class _OnboardingPageState extends State<OnboardingPage> {
child: Text("Lang"),
),
onTap: () async {
- final locale = await getLocale();
+ final locale = (await getLocale())!;
// ignore: unawaited_futures
routeToPage(
context,
diff --git a/auth/lib/ui/settings/general_section_widget.dart b/auth/lib/ui/settings/general_section_widget.dart
index 2089b9f3ca..cb29d8483a 100644
--- a/auth/lib/ui/settings/general_section_widget.dart
+++ b/auth/lib/ui/settings/general_section_widget.dart
@@ -49,7 +49,7 @@ class _AdvancedSectionWidgetState extends State<AdvancedSectionWidget> {
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
onTap: () async {
- final locale = await getLocale();
+ final locale = (await getLocale())!;
// ignore: unawaited_futures
routeToPage(
context,
diff --git a/auth/lib/ui/tools/app_lock.dart b/auth/lib/ui/tools/app_lock.dart
index 0d328218f1..1428c553c6 100644
--- a/auth/lib/ui/tools/app_lock.dart
+++ b/auth/lib/ui/tools/app_lock.dart
@@ -35,7 +35,7 @@ class AppLock extends StatefulWidget {
final ThemeData? darkTheme;
final ThemeData? lightTheme;
final ThemeMode savedThemeMode;
- final Locale locale;
+ final Locale? locale;
const AppLock({
super.key,
@@ -43,7 +43,7 @@ class AppLock extends StatefulWidget {
required this.lockScreen,
required this.savedThemeMode,
this.enabled = true,
- this.locale = const Locale('en', 'US'),
+ this.locale,
this.backgroundLockLatency = const Duration(seconds: 0),
this.darkTheme,
this.lightTheme,