Abdulsalam Elsharif
Abdulsalam Elsharif

Reputation: 5101

Multilingual WPF Project using .resx resource in XAML

I'm working on a multilingual WPF project using .resx files, I created two files Resources.resx for English language and Resources.ar-LY.resx for Arabic language, I added to both a string resource named SampleText that has different value.

In my XAML window, I assign the resource value to Text property of the TextBlock as following:

<TextBlock x:Name="txbSampleText" Text="{x:Static p:Resources.SampleText}"/>

Then .. I changed the application culture to Arabic using the following line:

Thread.CurrentThread.CurrentUICulture = new CultureInfo("ar-LY");

The Problem: The project culture changed but the Textblock always gets the value from Resources.resx,

I found THIS solution but the solution uses code behind instead of XAML, You can download a test application that contain the issue from HERE

Any help please

Upvotes: 0

Views: 1180

Answers (1)

Berianidze Luka
Berianidze Luka

Reputation: 181

You created Resources.lng.resx files right way and this should work. But now,let's discuss why it doesn't: What does InitializeComponent() method? This methods draws elements,sets their stylez...and also if text of elements is binded to resources,takes value by key and sets it. So what happens in your case? Window starts,all texts are set by InitializeComponent() method and after that,when 'drawing' is done you change culture and that is wrong. If you want your culture to be taken from any other language resources file,you should set UICulture before InitializeComponent() is called.

This also means that in this way you can not change language while program is runnig and if you want to change language,you should save it somewhere,restart program,load saved language name from saved space and set it.

Upvotes: 1

Related Questions