WPF: add localization via resx files

Developing localizable feature in old application wasn’t painless. 🙂
In Windows environment, thanks to .Net framework this feature is very easy.
WPF doesn’t miss the point (ehm locbaml?? ;)) so realizing localizable applications is very easy indeed.

Since I’m still a WPF newbie developer, I found old resx files is still the simple solution.
I run through this very helpfull post WPF Localization – RESX Option by Toad.

Here is a step-by-step summary:

  1. Added “Resource” folder to wpf project
  2. Added resx files, based on languages to support (ie: Resource.en-US.resx, Resource.it-IT.resx, … and Resources.resx of couse)
  3. Changed resx files from Internal to Public
  4. Added resource namespace in Window1.xaml:
    xmlns:resx="clr-namespace:WpfApplication1.Resources"
  5. Setted property’s binding to desidered controls:
    <TextBlock Text="{x:Static resx:Resource.SampleMessage}" />
  6. So standard Window1.xaml can result like this

    <Window x:Class="WpfApplication1.Window1"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:resx="clr-namespace:WpfApplication1.Resources"
     Title="Window1" Height="100" Width="300">
    <Grid>
     <TextBlock Text="{x:Static resx:Resource.SampleMessage}" />
    </Grid>
    </Window>
  7. To test different languages, pre_setting language in App class constructor:
    public partial class App : Application
    {
        static App()
        {
            Resource.Culture = new System.Globalization.CultureInfo("it-IT");
        }
    }

XAML binding let resource available in designer window:

Ta-daaa! 🙂

source: WPF Localization – RESX Option

Technorati tags: WPFLocalization

Tags: ,

2 Responses to “WPF: add localization via resx files”

  1. WPF: localization in MVVM « a developer’s breadcrumb Says:

    […] a developer’s breadcrumb quelli che … le permiSSion, il l’ogIn, i crokii e l’acca_emme_ti_elle … ohhh yeah! « WPF: add localization via resx files […]

  2. Localizing WPF controls with ResX files « bernhardhagmann Says:

    […] https://robertoschiabel.wordpress.com/2010/01/31/wpf-add-localization-via-resx-files/ […]

Leave a comment