[iOS] Localisation not working
See original GitHub issueDescription
Running an iOS app I want to be able to get the localisation information from the user settings.
On startup the values for the following are all set to “Invariant” Thread.CurrentThread.CurrentUICulture Thread.CurrentThread.CurrentCulture CultureInfo.CurrentUICulture CultureInfo.DefaultThreadCurrentUICulture CultureInfo.DefaultThreadCurrentCulture CultureInfo.InstalledUICulture CultureInfo.InvariantCulture
Note: On windows and andriod these values are set correctly.
I have tried to get the localisation information to create a cultureinfo and set the values for the above properties in the AppDelegate class, I have tried to get the localization information from NSLocale.CurrentLocale or NSLocale.AutoUpdatingCurrentLocale.CollationIdentifier but it always return null. I can see the values in the debug window but on running the code to get the LocaleIdentifier throw a NullReferenceException. I have tried the following code in the overrides for CreateMauiApp, WillFinishLaunching and OnActivated.
`
var culture = CultureInfo.CreateSpecificCulture(NSLocale.CurrentLocale.CollationIdentifier); CultureInfo.DefaultThreadCurrentCulture = culture; CultureInfo.DefaultThreadCurrentUICulture = culture;
`
Firstly I would have expected the CultureInfo to have been which would be consistent with Windows and Android targets, failing that I would have expected I should have been able to access the NSLocale.CurrentLocale properties.
Sample repo available here https://github.com/PedroCigaw/MauiIssueSamplesRepo.git
Steps to Reproduce
- Run sample from github
- Run on windows and android you you will see the cultureinfo is loaded.
- Run on ios see cultureinfo is Invariant.
- Open AppDelegate.cs and uncomment code.
- Run on ios and the app will crash
Version with bug
Release Candidate 3 (current)
Last version that worked well
Unknown/Other
Affected platforms
iOS
Affected platform versions
iOS 15
Did you find any workaround?
No work round
Relevant log output
No response
Issue Analytics
- State:
- Created a year ago
- Comments:10 (2 by maintainers)
Top GitHub Comments
Hello, we are developing DevExpress .NET MAUI controls and experienced the same issue. This issue has a Normal priority for us. Thank you for your help.
As Eilon pointed out, this is a bug in the runtime. The bug is fixed and released for the 6.0.3xx band but not released for the 6.0.4xx band yet, hopefully that will happen soon. If you need this right now, you can create a
global.json
file that you set to a version in the 3xx with a minimum of 303 range.dotnet --list-sdks
dotnet new --globaljson --sdk-version=6.0.303
dotnet workload update
NOTE: this will update your entire MAUI workload for all projectsYou can see if it worked by inspecting
CultureInfo.CurrentUICulture.DisplayName
which should not contain “invariant”More is described here: https://github.com/dotnet/runtime/issues/73512#issuecomment-1213464568
If you do not need this today, wait until the next .NET 6 update and it should be released as part of that. There is nothing we can do at this time on our end.