Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinUI 3 runtime localization

I am developing WinUI 3 app, currently stuck with localization. Although I wrote separate resw resource files for different cultures and localized with x:Uid I cannot find a way to change language in app runtime.

Setting Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride only works fine for settings startup language.

Is runtime localization in WinUI 3 even possible?

like image 249
Pawsh Avatar asked Sep 12 '25 21:09

Pawsh


2 Answers

I have a NuGet package called WinUI3Localizer that helps you with localization.

GitHub: https://github.com/AndrewKeepCoding/WinUI3Localizer

  • No app restart
  • You/users can edit localized strings even after deployment
  • You/users can add new languages even after deployment
  • Uses standard "Resources.resw"
like image 187
Andrew KeepCoding Avatar answered Sep 14 '25 10:09

Andrew KeepCoding


I've spent many hours on this subject. Here's my sincere advice: DO NOT TRY TO LOCALIZE YOUR VIEWS. Also, don't try to handle changes to the language at RunTime. No one does this, you'll never see an ROI in the investment.

Make all your localization in the view models and use the StringLocalizer class that's built into .NET's DI. Add the string localizer to the DI first:

this.serviceProvider = new ServiceCollection()
            .AddLogging()
            .AddLocalization();

Then, in your .NET or .Net Standard library, in your view model, add the following initialization:

private readonly IStringLocalizer<LandingViewModel> stringLocalizer;

public MyViewModel(IStringLocalizer<MyViewModel> stringLocalizer)
{
    this.stringLocalizer = stringLocalizer;
}

Now, if you have some text you want to display, say on a button, you'd have:

public string SignUpText => this.stringLocalizer["SignUp"];

And in XAML, you'd have:

        <Button Content="{x:Bind ViewModel.SignUpText}"/>

Finally, give the RESX file the same name as your view model. It should be an Embedded Resource with, and this is important, no custom tool:

enter image description here

like image 31
Quarkly Avatar answered Sep 14 '25 09:09

Quarkly