Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion auth/lib/app/view/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 12 additions & 1 deletion auth/lib/locale.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -35,20 +36,24 @@ 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;
}
}

// Return the first language code match or default to 'en'
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
Expand All @@ -64,6 +69,12 @@ Future<Locale> getLocale() async {
return savedLocale;
}
}
if (autoDetectedLocale != null) {
return autoDetectedLocale!;
}
if (noFallback) {
return null;
}
return const Locale('en');
}

Expand Down
2 changes: 1 addition & 1 deletion auth/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion auth/lib/onboarding/view/onboarding_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion auth/lib/ui/settings/general_section_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions auth/lib/ui/tools/app_lock.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ class AppLock extends StatefulWidget {
final ThemeData? darkTheme;
final ThemeData? lightTheme;
final ThemeMode savedThemeMode;
final Locale locale;
final Locale? locale;

const AppLock({
super.key,
required this.builder,
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,
Expand Down
4 changes: 2 additions & 2 deletions mobile/lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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() {
Expand Down
23 changes: 19 additions & 4 deletions mobile/lib/l10n/l10n.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -50,6 +59,12 @@ Future<Locale> getLocale() async {
return savedLocale;
}
}
if (autoDetectedLocale != null) {
return autoDetectedLocale!;
}
if (noFallback) {
return null;
}
return const Locale('en');
}

Expand Down
2 changes: 1 addition & 1 deletion mobile/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
2 changes: 1 addition & 1 deletion mobile/lib/ui/home/landing_page_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion mobile/lib/ui/settings/general_section_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions mobile/lib/ui/tools/app_lock.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ class AppLock extends StatefulWidget {
final ThemeData? darkTheme;
final ThemeData? lightTheme;
final ThemeMode savedThemeMode;
final Locale locale;
final Locale? locale;

const AppLock({
Key? key,
required this.builder,
required this.lockScreen,
required this.savedThemeMode,
this.enabled = true,
this.locale = const Locale("en", "US"),
this.locale,
this.darkTheme,
this.lightTheme,
}) : super(key: key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion mobile/lib/ui/viewer/gallery/hooks/add_photos_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down