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

Calendar selected in device settings not available in CultureInfo #109924

Open
ashrivax23 opened this issue Nov 11, 2024 · 9 comments
Open

Calendar selected in device settings not available in CultureInfo #109924

ashrivax23 opened this issue Nov 11, 2024 · 9 comments
Labels
area-System.Globalization os-android os-ios Apple iOS untriaged New issue has not been triaged by the area owner

Comments

@ashrivax23
Copy link

Description

<style> </style>

We are building a MAUI app with .NET 9 for iOS and Android and saw an issue in iOS and some Android device e.g. Samsung Galaxy Note 20 in which if the calendar is changed in the setting the CultureInfo doesn’t have the updated calendar information. Details are below along with a sample application.

  1.  To display a date, a DateTime object is constructed, and DateTime.ToString() invoked to format the date.  ToString() expects a CultureInfo object which represents the culture settings on the phone, including the user selected calendar.  and format the date using the DateTime.ToString() method which takes as input the CultureInfo.
  2. Expectation is that the Calendar returned by CultureInfo.DefaultThreadCurrentUICulture represents the actual platform calendar selected by the user (Gregorian, Buddhist, Japanese).  This calendar object is presumably used by the ToString() implementation to format a date using the correct calendar. oString() implementation.
  3. When CultureInfo.DefaultThreadCurrentUICulture properties are inspected, sometimes the associated Calendar object matches the actual calendar selected by the on the phone by the user and sometimes it does not match.
  4. When the user phone settings are set to Language= Thai, Country=Thailand, Calendar=Buddhist, CultureInfo returns the correct Calendar object representing Buddhist calendar on iOS or Android.
    1. When passed to the ToString() method to get a ShortDatePattern, the ToString method does not format the date as expected. It only correctly formats it when the app is restarted on iOS. This indicates a potential issue with the ToString() implementation.
    2. When passed to the ToString() method to get a LongDatePattern using ToString("D", CultureInfo.DefaultThreadCurrentUICulture), the ToString method does return the correct format.
  5. In all other language, country, calendar combinations tested, CultureInfo does not even return a correct calendar object and the Calendar represent Gregorian irrespective of what is selected by the user.  In this case since Calendar object is incorrect, there is no expectation ToString() would work correctly.

 

Below is the table that shows what date format is shown in the app when we set language, region and calendar (when available) on an iOS and Android devices

 

Language Country Calendar Expected Actual Platform Remarks
English US Gregorian Gregorian Gregorian iOS  
English US Buddhist Buddhist Gregorian iOS Even after app restart
English US Japanese Japanese Gregorian iOS Even after app restart
Thai Thailand Gregorian Gregorian Buddhist iOS Even after app restart
Thai Thailand Buddhist Buddhist Buddhist iOS  
Thai Thailand Japanese Japanese Buddhist iOS Even after app restart
Japanese Japan Gregorian Gregorian Gregorian iOS Even after app restart
Japanese Japan Buddhist Buddhist Gregorian iOS Even after app restart
Japanese Japan Japanese Japanese Gregorian iOS Even after app restart
English US Gregorian Gregorian Gregorian Android  
English US Hijri Hijri Gregorian Android  
English US Chinese Lunar Chinese Lunar Gregorian Android  
Thai Thailand Chinese Lunar Chinese Lunar Gregorian Android  
Thai Thailand Gregorian Gregorian Gregorian Android  
Thai Thailand Hijri Hijri Gregorian Android  
Japanese Japan Chinese Lunar Chinese Lunar Gregorian Android  
Japanese Japan Gregorian Gregorian Gregorian Android  
Japanese Japan Hijri Hijri Gregorian Android  
             

For iOS we can see option to select Gregorian, Japanese or Buddhist calendar from the device settings.

For Android on Samsung Note 20 we can see calendars like Chinese Lunar, Hijri, Korean Lunar, Shamsi etc. Not all Android devices have the ability to change the calendar.

 

Steps to Reproduce

Attached is a sample application that will show the dates and locale information.
Steps

  1. Install the application
  2. Check the locale, calendar and date information
  3. Go to device settings -> general -> language and region (On iOS)
  4. Change the language, region and calendar
  5. Relaunch the application and check if the language, date and calendar match as per the device setting.

Link to public reproduction project repository

https://github.com/ashrivax23/DateTimeFormatMauiSample

Version with bug

9.0.0-rc.2.24503.2

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android

Affected platform versions

No response

Did you find any workaround?

For iOS I could write a method using NSCalendar to find which calendar is set on the device setting and based on that set the calendar in the app but that has other issues like characters from other language show with the date e.g. Thai characters shown when language is English and calendar is Buddhist.
For Android found no work arounds.

Relevant log output

Copy link

We've found some similar issues:

If any of the above are duplicates, please consider closing this issue out and adding additional context in the original issue.

Note: You can give me feedback by 👍 or 👎 this comment.

@ashrivax23
Copy link
Author

We've found some similar issues:

If any of the above are duplicates, please consider closing this issue out and adding additional context in the original issue.

Note: You can give me feedback by 👍 or 👎 this comment.

Not relevant to the issue that is reported

@drasticactions
Copy link

@ashrivax23 Can you try running this code inside an .NET iOS (dotnet new ios) or Android (dotnet new android) application that doesn't implement that MAUI UI Framework?

https://github.com/ashrivax23/DateTimeFormatMauiSample/blob/main/MainPage.xaml.cs#L60-L106

I can't speak for Android (@jonathanpeppers would you know where to look for that?), but for iOS you're most likely facing what this user did:

dotnet/maui#15744 (comment)

https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/localization/

If CFBundleDevelopmentRegion has not been specified in the Info.plist file, the Xamarin.iOS build tools currently use a default value of en_US. While this may change in a future release, it means that the default language is English.

https://github.com/ashrivax23/DateTimeFormatMauiSample/blob/main/Platforms/iOS/Info.plist
In short, you also need to declare the supported languages within your iOS/Catalyst app as well to get the supported controls. Relying on CultureInfo alone isn't enough.

These docs are for Xamarin.iOS, but also apply to MAUI iOS and Catalyst.

@jaosnz-rep
Copy link

jaosnz-rep commented Nov 12, 2024

I can repro this issue at iOS & Android platform on the latest 17.12 Preview 5(9.0.0-rc.1.24453.9 & 9.0.0-rc.2.24503.2).

@ashrivax23
Copy link
Author

ashrivax23 commented Nov 15, 2024

@ashrivax23 Can you try running this code inside an .NET iOS (dotnet new ios) or Android (dotnet new android) application that doesn't implement that MAUI UI Framework?

https://github.com/ashrivax23/DateTimeFormatMauiSample/blob/main/MainPage.xaml.cs#L60-L106

I can't speak for Android (@jonathanpeppers would you know where to look for that?), but for iOS you're most likely facing what this user did:

#15744 (comment)

https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/localization/

If CFBundleDevelopmentRegion has not been specified in the Info.plist file, the Xamarin.iOS build tools currently use a default value of en_US. While this may change in a future release, it means that the default language is English.

https://github.com/ashrivax23/DateTimeFormatMauiSample/blob/main/Platforms/iOS/Info.plist In short, you also need to declare the supported languages within your iOS/Catalyst app as well to get the supported controls. Relying on CultureInfo alone isn't enough.

These docs are for Xamarin.iOS, but also apply to MAUI iOS and Catalyst.

I tried the above mentioned approach.

Created this application: https://github.com/ashrivax23/DateTimeIOSSample

In the above application I can see the calendar change in the date picker. Please check the attached screenshots of the simulator with the locale, date format and calendar shown in the UI

Image
Image
Image

@jonathanpeppers jonathanpeppers transferred this issue from dotnet/maui Nov 18, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Nov 18, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Nov 18, 2024
@vcsjones vcsjones added area-System.Globalization and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Nov 20, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-globalization
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Tagging subscribers to 'arch-android': @vitek-karas, @simonrozsival, @steveisok, @akoeplinger
See info in area-owners.md if you want to be subscribed.

@tarekgh
Copy link
Member

tarekgh commented Nov 21, 2024

CC @matouskozak

Copy link
Contributor

Tagging subscribers to 'os-ios': @vitek-karas, @kotlarmilos, @ivanpovazan, @steveisok, @akoeplinger
See info in area-owners.md if you want to be subscribed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Globalization os-android os-ios Apple iOS untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

5 participants