Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to localize WPF .net-core application with RESX

I want to localize a .Net Core application with .resx files. Can anyone give me a complete step-by-step solution? I'm new at WPF and .Net-Core. When I change the current culture in code nothing happens. Here is my code:

<ToolBar>
    <Button Content="{x:Static strings:Resource.NewCustomer}" Command="{Binding NewCustomerDelegateCommand}"/>
</ToolBar>
public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("de-DE");
        Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-DE");
        base.OnStartup(e);
    }
}
like image 463
McKenzy1971 Avatar asked Aug 30 '25 16:08

McKenzy1971


1 Answers

When localizing WPF application you have several choices.

  1. Using native WPF's approach (with using UID), see: MSDN. This approach has drawbacks explained more in detail in the article, but generally saying:

    • it's not using resx (which is very natural for most of the developers)
    • does not support changing language at runtime
  2. Using the resx's strongly-typed resource class generated by PublicResXFileCodeGenerator / ResXFileCodeGenerator.

    (see custom tool in the properties window in the VS for the resx file; PS: the second generator creates internal class, so keep in mind that it will be usable only in the same assembly where xaml being localized exists and only with WPF Binding, not with Static markup extension)

    This approach is similar to the one you’ve tried to apply, meaning in the xaml you use Binding (or x:Static) to the properties of the generated strongly-typed resource class. The drawback of this approach is that the generated strongly-typed resource class does not provide any notification whenever culture/language is changed. Other words, you can provide strings, labels etc., but they will be resolved at the time when xaml is parsed/loaded. After that it becomes fixed content:

    • does not support changing language at runtime
  3. You can find on the market already available solutions for localizing WPF application using resx files, e.g. have a look: https://www.qube7.com/guides/localization.html

    • it supports changing language at runtime (UI automatically refreshes when you set Culture.Current)
    • the solution provides way to localize xaml that is supported in VS designer
    • approach of localizing xaml is similar to localizing asp.net: you may have local resources (resx per xaml view) + you may have global resources (resx that holds shared resources)
    • solution gives you way to localize view model as well (which is also something not rare to see)

    The full source code of the Resource markup extension you’ll find here: Resource.cs

like image 94
gorskib Avatar answered Sep 02 '25 09:09

gorskib