Skip to content

Localize a WPF Application

jbe2277 edited this page Jun 10, 2021 · 3 revisions

This article describes how a WPF Application can be localized. Please read the post CurrentCulture vs. CurrentUICulture first because it describes the basics of localization with the .NET Framework.

Display language (CurrentUICulture)

The official way to localize a WPF Application is by parsing the compiled XAML (=> BAML) file to extract the used string resources. Microsoft provides the LocBaml tool that can do the parsing but this tool is after about 10 years still not production-ready.

If you use this approach then please keep in mind that it can only localize string resources which are defined within a XAML source file. If you want to localize messages defined in C# code (e.g. validation messages) then it does not work.

An alternative method is to use the classic ResX files to localize a WPF Application. The advantages of this approach are:

  • Visual Studio comes with a Designer to edit the resource (ResX) files.
  • This approach is used by other technologies as well (e.g. ASP .NET, Windows Forms, Console Applications).
  • It can be used everywhere (e.g. in XAML, in C#).
  • It comes with an intelligent fallback mechanism if the user’s language is not supported.

How to use a ResX file in a WPF Application?

  • Create a Resource (ResX) file in your project or you can reuse the Properties/Resources.resx file.
  • In the Visual Studio Resource designer you have to change the Access Modifier from Internal to Public. This can be found in the top toolbar of the designer.
  • Add the namespace to your XAML View:
xmlns:p="clr-namespace:Waf.Writer.Presentation.Properties"
  • The Resources can be accessed via the x:Static markup extension of XAML:
Header="{x:Static p:Resources.NewMenu}"

Formatting settings (CurrentCulture)

The second part of this article describes how a WPF application can be configured to respect the Windows Format settings.

WPF uses always the culture setting “en-US” for formatting and parsing of values when Binding is used. It is possible to override this setting with the following statement:

FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), 
    new FrameworkPropertyMetadata(
        XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));

However, we can pass only the IETF Language Tag. This way all custom settings the user might have configured in the Windows Format settings are lost because only the language tag is passed.

If supporting the custom culture settings is required the following workaround can be used:

xmlns:glb="clr-namespace:System.Globalization;assembly=mscorlib"

<TextBox Text="{Binding Birthday, StringFormat=d, ConverterCulture={x:Static glb:CultureInfo.CurrentCulture}}"/>

Further readings

Clone this wiki locally