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?
I have a NuGet package called WinUI3Localizer that helps you with localization.
GitHub: https://github.com/AndrewKeepCoding/WinUI3Localizer
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With