uno.extensions icon indicating copy to clipboard operation
uno.extensions copied to clipboard

LocalizationService does not set a culture in Windows (Unpackaged) Head

Open Seekuhnetz opened this issue 1 year ago • 21 comments

Current behavior

I created a new App with the UnoApp Template and Localization and Configuration enabled. I added the ToggleLocalizationAsync() of this page.

The Current Culture gets updated in the (Packaged) but not in the (Unpackaged) Windows Head.

I also had to add .Section<LocalizationConfiguration>() under configBuilder in my App.cs else the SupportedCultures would be empty. I guess the problem could be fixed if i was able to also add .Section<LocalizationSettings>(). But i can not because it is internal.

Expected behavior

The Current Culture should be updated in the (Unpackaged) Windows Head like in the (Packaged) Head.

How to reproduce it (as minimally and precisely as possible)

  • Create a new Uno App with the UnoApp Template and Localization and Configuration enabled.
  • Add the ToggleLocalizationAsync() of this page.
  • Run the Windows (Unpackaged) Head

Workaround

No response

Works on UWP/WinUI

None

Environment

No response

NuGet package version(s)

No response

Affected platforms

Windows

IDE

Visual Studio 2022

IDE version

No response

Relevant plugins

No response

Anything else we need to know?

No response

Seekuhnetz avatar Jan 03 '24 19:01 Seekuhnetz

@nickrandolph I just tested this and it looks like that changing the language with ILocalizationService still does not work in the Unpackaged Head of WinUI. Packaged Head works fine. SetLanguageTest.zip

Seekuhnetz avatar May 17 '24 02:05 Seekuhnetz

@nickrandolph I just tested this and it looks like that changing the language with ILocalizationService still does not work in the Unpackaged Head of WinUI. Packaged Head works fine. SetLanguageTest.zip

Thanks for following up on this. I've confirmed that:

  • ILocalizationService does appear to be saving the change to the current culture
  • For some reason the saved culture isn't being applied correctly when run as unpackaged. This is evident both in a new application created with latest template but also in the Playground application in this repository

nickrandolph avatar May 20 '24 18:05 nickrandolph

Looks like we might be stalled on this https://github.com/microsoft/WindowsAppSDK/pull/4181

nickrandolph avatar May 20 '24 18:05 nickrandolph

I also noticed that DateTimes are in the wrong format for the Unpackaged Head. The culture does not get set to the system one and is instead set to "en". Again this is not an issue in the Packaged Head.

I fixed the issue with following workaround:

App.Dispatcher.TryEnqueue(() =>
{
    CultureInfo ci = new CultureInfo("de-DE");
    Thread.CurrentThread.CurrentCulture = ci;
    Thread.CurrentThread.CurrentUICulture = ci;
});

Seekuhnetz avatar May 31 '24 12:05 Seekuhnetz

@nickrandolph it looks like https://github.com/microsoft/WindowsAppSDK/pull/4181 has been closed.

Seekuhnetz avatar Jul 04 '24 10:07 Seekuhnetz

@nickrandolph it looks like microsoft/WindowsAppSDK#4181 has been closed.

@rajamatt now that https://github.com/microsoft/WindowsAppSDK/pull/4181 is closed, can you re-test with latest version please You can start to test with 1.6.240701003-experimental2 as mentioned here normally.

agneszitte avatar Aug 29 '24 22:08 agneszitte

I created a new app using Uno.WinUI 5.3.123 and the culture localization was working well in both Packaged and Unpackaged Windows. The Microsoft.WindowsAppSDK version (1.5.240627000) I used is the same that comes with Uno.WinUI 5.3.123. In the uno.extensions Playground, updating the Uno.WinUI to 5.3.123 does not solve the issue though.

I did not test with 1.6.240701003-experimental2 yet as I am having trouble overriding the version that it takes from Uno.WinUI in Playground. This should be the next step.

rajamatt avatar Aug 29 '24 23:08 rajamatt

I found a way to substitute the Microsoft.WindowsAppSDK package with the 1.6.240701003-experimental2 version. It is still working in Packaged and Unpackaged with Uno.WinUI 5.3.123 (which is used with Uno.Sdk 5.3.99).

However, if I override the Microsoft.WindowsAppSDK in Playground it still doesn't work, so something might be wrong with Playground itself.

It seems that this 1.6.240701003-experimental2 version doesn't help. I also tried the latest version 1.6.240821007-preview2 which didn't help either.

@agneszitte Could you also quickly test if a new unoapp with Uno.Sdk 5.3.99 handles the localization change correctly? You can:

  1. Create a new unoapp with the recommended preset.
  2. In the MainModel add the method below, along with the localizationService, like done on this doc page. And bind it to a button on the MainPage.
public Task ToggleLocalizationAsync()
{
    var currentCulture = localizationService.CurrentCulture;
    var culture = localizationService.SupportedCultures.First(culture => culture.Name != currentCulture.Name);
    
    return localizationService.SetCurrentCultureAsync(culture);
}
  1. Open the app, observe the title of the application (with the recommended template it should be Main - [name of app] - [language])
  2. Click the button you binded to the localization toggle method.
  3. Close the app, then start it again.
  4. Notice the language in the title of the application should have changed (localization worked).

rajamatt avatar Sep 04 '24 00:09 rajamatt

@Kunal22shah can you help do a test with latest 1.6.240829007 please

agneszitte avatar Sep 17 '24 20:09 agneszitte

@agneszitte I have tested with Uno.Sdk 5.3.108 and latest 1.6.240829007 WinAppSdk and can confirm that localization works fine.

Kunal22shah avatar Sep 19 '24 13:09 Kunal22shah

@Kunal22shah Thanks for the help 🚀

@agneszitte I want to re-test pure Xaml culture/localization as well as I don't remember clearly if it was working or not. I will report the result here.

rajamatt avatar Sep 19 '24 13:09 rajamatt

@Kunal22shah Thanks for the help 🚀

@agneszitte I want to re-test pure Xaml culture/localization as well as I don't remember clearly if it was working or not. I will report the result here.

Thanks a lot @Kunal22shah for the previous test and thanks in advance for the result of the next test @rajamatt

agneszitte avatar Sep 19 '24 14:09 agneszitte

Tested again with latest stable Uno.Sdk and both: Code behind & Xaml X:Uid's are working correctly on the Unpackaged Windows head.

Launching the app, setting the culture, then restarting the app displays the correct selected culture.

rajamatt avatar Sep 19 '24 20:09 rajamatt

Tested again with latest stable Uno.Sdk and both: Code behind & Xaml X:Uid's are working correctly on the Unpackaged Windows head.

Launching the app, setting the culture, then restarting the app displays the correct selected culture.

Thanks a lot for the feedback @rajamatt !

agneszitte avatar Sep 19 '24 22:09 agneszitte

I retested and am not able to make localization work on the Unpackaged head. I am not sure what happened but I am guessing I overrode the WinAppSdk to the preview version and then Extensions always kept that version. It is weird though, because that specific preview version adds to the API with Microsoft.Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride, and I never changed code in Extensions when I was testing, so it really should not have worked at all.

More importantly, this new Microsoft.Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride is only available in the WindowsAppSdk 1.6.240821007-preview2 version, and not the latest stable 1.6.240829007.

rajamatt avatar Oct 01 '24 20:10 rajamatt

WinAppSdk seems to just have been updated and now includes this change (on a stable version!) I will test further to see if this new version 1.6.240923002 helps.

rajamatt avatar Oct 01 '24 20:10 rajamatt

WinAppSdk seems to just have been updated and now includes this change (on a stable version!) I will test further to see if this new version 1.6.240923002 helps.

thank you. how can i reference 1.6.240923002 in my project? in the NuGet Manager it is said that i can not upgrade the NuGet as it is referenced by the Uno Sdk

Seekuhnetz avatar Oct 01 '24 20:10 Seekuhnetz

thank you. how can i reference 1.6.240923002 in my project? in the NuGet Manager it is said that i can not upgrade the NuGet as it is referenced by the Uno Sdk

@Seekuhnetz if you want to give it a try on your side while @rajamatt is testing it on his side, you can add in your Name_Of_Your_Project.csproj file <WinAppSdkVersion>1.6.240923002</WinAppSdkVersion>

For more info about implicit packages: https://platform.uno/docs/articles/features/using-the-uno-sdk.html?tabs=3rd-party-packages#implicit-packages

agneszitte avatar Oct 01 '24 21:10 agneszitte

<WinAppSdkVersion>1.6.240923002</WinAppSdkVersion>

thank you, but sadly this did not fix the bug for me.

From @agneszitte: @Seekuhnetz Thank you a lot for the feedback, I will see if @rajamatt confirm the same on his side as well.

Seekuhnetz avatar Oct 01 '24 22:10 Seekuhnetz

Thanks for the test @Seekuhnetz. I believe this will require a fix in the Uno.Extensions.Localization project as over there we are currently setting the PrimaryLanguage property which isn't compatible with Unpackaged apps.

I will investigate more to prepare the details and share them here.

rajamatt avatar Oct 02 '24 00:10 rajamatt

Thanks for the test @Seekuhnetz. I believe this will require a fix in the Uno.Extensions.Localization project as over there we are currently setting the PrimaryLanguage property which isn't compatible with Unpackaged apps.

I will investigate more to prepare the details and share them here.

cc @kazo0, @eriklimakc for info

agneszitte avatar Oct 07 '24 14:10 agneszitte

@agneszitte Is there any update for this ticket?

Seekuhnetz avatar Jun 16 '25 08:06 Seekuhnetz