From b721a848891cdf097de1a3192dd80801093b7d05 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Tue, 13 May 2025 12:28:34 +0530 Subject: [PATCH 1/4] Fix date format in birthday date-picker --- mobile/lib/l10n/l10n.dart | 35 ++++++++++++++++++++++++---- mobile/lib/ui/common/date_input.dart | 3 +++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/mobile/lib/l10n/l10n.dart b/mobile/lib/l10n/l10n.dart index 5154f8840c..ab8f9e2cc4 100644 --- a/mobile/lib/l10n/l10n.dart +++ b/mobile/lib/l10n/l10n.dart @@ -29,21 +29,46 @@ const List appSupportedLocales = [ Locale("zh", "CN"), ]; +List _onDeviceLocales = []; Locale? autoDetectedLocale; -Locale localResolutionCallBack(locales, supportedLocales) { - for (Locale locale in locales) { +Locale localResolutionCallBack(deviceLocales, supportedLocales) { + _onDeviceLocales = deviceLocales; + debugPrint("onDeviceLocales: ${_onDeviceLocales.toString()}"); + for (Locale deviceLocale in deviceLocales) { for (Locale supportedLocale in appSupportedLocales) { - if (supportedLocale == locale) { + if (supportedLocale == deviceLocale) { autoDetectedLocale = supportedLocale; return supportedLocale; - } else if (supportedLocale.languageCode == locale.languageCode) { + } else if (supportedLocale.languageCode == deviceLocale.languageCode) { autoDetectedLocale = supportedLocale; return supportedLocale; } } } - return const Locale('en'); + return autoDetectedLocale ?? const Locale('en'); +} + +// This is used to get locale that should be used for various formatting +// operations like date, time, number etc. For common languages like english, different +// locale might have different formats. For example, en_US and en_GB have different +// formats for date and time. Use this method to find the best locale for formatting +// operations. This is not used for displaying text in the app. +Future getFormatLocale() async { + final Locale locale = (await getLocale())!; + Locale? firstLanguageMatch; + // see if exact matche is present in the device locales + for (Locale deviceLocale in _onDeviceLocales) { + if (deviceLocale.languageCode == locale.languageCode && + deviceLocale.countryCode == locale.countryCode) { + return deviceLocale; + } + if (firstLanguageMatch == null && + deviceLocale.languageCode == locale.languageCode) { + firstLanguageMatch = deviceLocale; + } + } + return firstLanguageMatch ?? locale; } Future getLocale({ diff --git a/mobile/lib/ui/common/date_input.dart b/mobile/lib/ui/common/date_input.dart index 41fbccb76a..f7b1fbae0b 100644 --- a/mobile/lib/ui/common/date_input.dart +++ b/mobile/lib/ui/common/date_input.dart @@ -121,8 +121,11 @@ class _DatePickerFieldState extends State { } Future _showDatePicker() async { + final Locale locale = await getFormatLocale(); + debugPrint("DatePicker: $locale"); final DateTime? picked = await showDatePicker( context: context, + locale: locale, initialDate: _selectedDate ?? DateTime.now(), firstDate: widget.firstDate ?? DateTime(1900), lastDate: widget.lastDate ?? DateTime(2100), From b08d8de1c8eb31684b878f3f0188d859db87fa60 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Tue, 13 May 2025 13:25:46 +0530 Subject: [PATCH 2/4] Fix date formatting in edit time --- mobile/lib/ui/viewer/date/edit_date_sheet.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mobile/lib/ui/viewer/date/edit_date_sheet.dart b/mobile/lib/ui/viewer/date/edit_date_sheet.dart index 1ae6a12eb2..39c5381909 100644 --- a/mobile/lib/ui/viewer/date/edit_date_sheet.dart +++ b/mobile/lib/ui/viewer/date/edit_date_sheet.dart @@ -264,7 +264,7 @@ class DateAndTimeWidget extends StatelessWidget { Widget build(BuildContext context) { final colorScheme = getEnteColorScheme(context); final locale = Localizations.localeOf(context); - final String date = DateFormat.yMMMd(locale.languageCode).format(dateTime); + final String date = DateFormat.yMMMd(locale.toString()).format(dateTime); final String time = DateFormat( MediaQuery.of(context).alwaysUse24HourFormat ? 'HH:mm' : 'h:mm a', ).format(dateTime); @@ -628,7 +628,7 @@ class PhotoDateHeaderWidget extends StatelessWidget { ), const SizedBox(height: 4), Text( - "${DateFormat.yMEd(locale.languageCode).format(startDate)} · ${DateFormat( + "${DateFormat.yMEd(locale.toString()).format(startDate)} · ${DateFormat( MediaQuery.of(context).alwaysUse24HourFormat ? 'HH:mm' : 'h:mm a', @@ -648,7 +648,7 @@ class PhotoDateHeaderWidget extends StatelessWidget { } String _formatDate(DateTime date, Locale locale, BuildContext context) { - return "${DateFormat.yMEd(locale.languageCode).format(date)}\n${DateFormat( + return "${DateFormat.yMEd(locale.toString()).format(date)}\n${DateFormat( MediaQuery.of(context).alwaysUse24HourFormat ? 'HH:mm' : 'h:mm a', ).format(date)}"; } From c0fd71a66838da84ad06628889495794ef44ce67 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Tue, 13 May 2025 14:25:20 +0530 Subject: [PATCH 3/4] Fix: fallback to device locale on language only match --- mobile/lib/l10n/l10n.dart | 12 ++++++++---- mobile/lib/ui/settings/language_picker.dart | 10 ++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/mobile/lib/l10n/l10n.dart b/mobile/lib/l10n/l10n.dart index ab8f9e2cc4..09ad1d5d06 100644 --- a/mobile/lib/l10n/l10n.dart +++ b/mobile/lib/l10n/l10n.dart @@ -34,18 +34,22 @@ Locale? autoDetectedLocale; Locale localResolutionCallBack(deviceLocales, supportedLocales) { _onDeviceLocales = deviceLocales; - debugPrint("onDeviceLocales: ${_onDeviceLocales.toString()}"); + Locale? firstLangeuageMatch; for (Locale deviceLocale in deviceLocales) { for (Locale supportedLocale in appSupportedLocales) { if (supportedLocale == deviceLocale) { autoDetectedLocale = supportedLocale; return supportedLocale; - } else if (supportedLocale.languageCode == deviceLocale.languageCode) { - autoDetectedLocale = supportedLocale; - return supportedLocale; + } + if (firstLangeuageMatch == null && + supportedLocale.languageCode == deviceLocale.languageCode) { + firstLangeuageMatch = deviceLocale; } } } + if (firstLangeuageMatch != null) { + autoDetectedLocale = firstLangeuageMatch; + } return autoDetectedLocale ?? const Locale('en'); } diff --git a/mobile/lib/ui/settings/language_picker.dart b/mobile/lib/ui/settings/language_picker.dart index 86d9a9a674..009150afb9 100644 --- a/mobile/lib/ui/settings/language_picker.dart +++ b/mobile/lib/ui/settings/language_picker.dart @@ -98,11 +98,21 @@ class _ItemsWidgetState extends State { @override Widget build(BuildContext context) { items.clear(); + bool foundMatch = false; for (Locale locale in widget.supportedLocales) { + if (currentLocale == locale) { + foundMatch = true; + } items.add( _menuItemForPicker(locale), ); } + if (!foundMatch && kDebugMode) { + items.insert( + 0, + Text("(i) Locale : ${currentLocale.toString()}"), + ); + } items = addSeparators( items, DividerWidget( From f9cce787f7b994b2882cc8c05cf47df75e2c2cd4 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Tue, 13 May 2025 14:26:54 +0530 Subject: [PATCH 4/4] Remove log --- mobile/lib/ui/common/date_input.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/mobile/lib/ui/common/date_input.dart b/mobile/lib/ui/common/date_input.dart index f7b1fbae0b..f9624d5167 100644 --- a/mobile/lib/ui/common/date_input.dart +++ b/mobile/lib/ui/common/date_input.dart @@ -122,7 +122,6 @@ class _DatePickerFieldState extends State { Future _showDatePicker() async { final Locale locale = await getFormatLocale(); - debugPrint("DatePicker: $locale"); final DateTime? picked = await showDatePicker( context: context, locale: locale,