0

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

Abdulsalam Elsharif
  • 4,773
  • 7
  • 32
  • 66

1 Answers1

1

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.

Berianidze Luka
  • 181
  • 3
  • 10
  • Thank you for the solution and explanation, It works now, but I do not understand you if I want to change the language while the program runing ! "you should save it somewhere,restart program,load saved language name from saved space and set it." What should I save? – Abdulsalam Elsharif Sep 19 '20 at 09:43
  • You can not change the language while the program is runnig this way. Now if you want to change language,you change it in ListBox and save if for example in file "language.conf" with content "ru-RU" and then restart program. On start,you read content from "language.conf" and set it to UICulture – Berianidze Luka Sep 19 '20 at 14:09
  • If you want to change language while program is runnig,you have to look for INotifyPropertyChanged implementation – Berianidze Luka Sep 19 '20 at 14:11
  • Thank you so much, I got your idea :) – Abdulsalam Elsharif Sep 19 '20 at 15:42