7

I want to use localization in my project so I'm using ".resx" files.

I have two files "StringRes.resx" and "StringRes.fr.resx". As you can guess, I want that all the messages of my app change according to the CultureInfo of the user.

But when I do this :

public MainWindow()
{
     Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("fr");
     InitializeComponent();
}

It doesn't change anything when I do :

Console.WriteLine(StringRes.FirstName);

Indeed, it's always the string in StringRes which is displayed and not the string from StringRes.fr

The both are in the same namespace.

Thank you for any help.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Kyoros
  • 173
  • 2
  • 2
  • 8
  • The resx files works off whatever window's culture is, I'm think there is an application culture somewhere [See this](http://stackoverflow.com/a/329041/1324033) – Sayse Aug 21 '13 at 07:58

1 Answers1

15

I have created my own test projected an I used "fr-FR" as a culture tag. The test project can be found on my skydrive and archived GitHub.

Works perfect.

Culture test

In short:

I set the culture in the Properties of the project to be en-US as that is what I would want my Resource.resx to be.

Then I created Resource.resx, Resource.fr-FR.resx and Resource.nl-NL.resx.

I filled them with the TestData string. And the created a window with 3 buttons with events hooked to them. Next to the buttons I created a textblock, the textblock is binded to a Text string field.

In the button events I change the culture and the Text field. And you can then see the change happen.


You first need to create your Resource.resx then in the same folder you need to create Resource.fr-FR.resx. The Resource.resx will get a codebehind file, all the cultured resource files should not. If your Resource.fr-FR.resx does have a culture file you did something wrong and it is best to delete that resources file and recreate it so it loses it's codebehind.

SynerCoder
  • 12,493
  • 4
  • 47
  • 78
  • 1
    I've done as you've suggested and look at your sample and it works ! My Resource.fr-FR.resx has a codebehind file so I've recreated as you said and i don't have problems anymore. Thanks a lot. – Kyoros Aug 21 '13 at 08:47
  • Well then, congratulations on your first answered question! Please accept the answer to let other people know this answer helped you. You also get +2 reputation for accepting an answer. For more information about reputation: http://stackoverflow.com/help/whats-reputation – SynerCoder Aug 21 '13 at 08:49
  • 1
    Thank you for the details about the order of adding the resource files. 2 hours into figuring out why mine were not working I found this post and it solved my problem too. – Adam Heeg May 19 '16 at 17:24
  • Wish you hadn't put all the relevant info in an external download link which could fail later. – IronSean May 05 '23 at 20:56
  • @IronSean what failed? I downloaded the ZIP again and it is working. Also, the answer contained all the explaination of what is happening. – SynerCoder May 08 '23 at 11:49
  • 1
    @IronSean Updated the project to .NET6 and uploaded it to github – SynerCoder May 08 '23 at 12:18