[mob][auth] Fix detection of default locale (#3819)

## Description

## Tests
This commit is contained in:
Neeraj Gupta
2024-10-24 11:23:38 +05:30
committed by GitHub
14 changed files with 46 additions and 20 deletions

View File

@@ -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) {

View File

@@ -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');
}

View File

@@ -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(

View File

@@ -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,

View File

@@ -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,

View File

@@ -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,

View File

@@ -25,7 +25,7 @@ class EnteApp extends StatefulWidget {
final Future<void> Function(String) runBackgroundTask;
final Future<void> Function(String) killBackgroundTask;
final AdaptiveThemeMode? savedThemeMode;
final Locale locale;
final Locale? locale;
const EnteApp(
this.runBackgroundTask,
@@ -46,7 +46,7 @@ class EnteApp extends StatefulWidget {
class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
final _logger = Logger("EnteAppState");
late Locale locale;
late Locale? locale;
@override
void initState() {

View File

@@ -24,17 +24,26 @@ const List<Locale> appSupportedLocales = <Locale>[
Locale("zh", "CN"),
];
Locale? autoDetectedLocale;
Locale localResolutionCallBack(locales, supportedLocales) {
for (Locale locale in locales) {
if (appSupportedLocales.contains(locale)) {
return locale;
for (Locale supportedLocale in appSupportedLocales) {
if (supportedLocale == locale) {
autoDetectedLocale = supportedLocale;
return supportedLocale;
} else if (supportedLocale.languageCode == locale.languageCode) {
autoDetectedLocale = supportedLocale;
return supportedLocale;
}
}
}
// if device language is not supported by the app, use en as default
return 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
@@ -50,6 +59,12 @@ Future<Locale> getLocale() async {
return savedLocale;
}
}
if (autoDetectedLocale != null) {
return autoDetectedLocale!;
}
if (noFallback) {
return null;
}
return const Locale('en');
}

View File

@@ -95,7 +95,7 @@ Future<void> _runInForeground(AdaptiveThemeMode? savedThemeMode) 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);
runApp(
AppLock(
builder: (args) =>

View File

@@ -89,7 +89,7 @@ class _LandingPageWidgetState extends State<LandingPageWidget> {
child: Text("Lang"),
),
onTap: () async {
final locale = await getLocale();
final locale = (await getLocale())!;
// ignore: unawaited_futures
routeToPage(
context,

View File

@@ -70,7 +70,7 @@ class GeneralSectionWidget extends StatelessWidget {
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
onTap: () async {
final locale = await getLocale();
final locale = (await getLocale())!;
await routeToPage(
context,
LanguageSelectorPage(

View File

@@ -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({
Key? 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.darkTheme,
this.lightTheme,
}) : super(key: key);

View File

@@ -47,7 +47,7 @@ class _CreationTimeItemState extends State<CreationTimeItem> {
}
void _showDateTimePicker(EnteFile file) async {
final Locale locale = await getLocale();
final Locale locale = (await getLocale())!;
final localeType = getFromLocalString(locale);
final dateResult = await DatePickerBdaya.showDatePicker(
context,

View File

@@ -218,7 +218,7 @@ class AddPhotosPhotoWidget extends StatelessWidget {
// This custom method is required to enforce English as the default fallback
// instead of Chinese.
Future<AssetPickerTextDelegate> _getAssetPickerTextDelegate() async {
final Locale locale = await getLocale();
final Locale locale = (await getLocale())!;
switch (locale.languageCode.toLowerCase()) {
case "en":
return const EnglishAssetPickerTextDelegate();