DmitryB
DmitryB

Reputation: 525

Why localization doesn't work

I try to write simple C# project to test how localication work. I did how here is written.

How to use localization in C#

I think I use the same like in the questions/1142802. But it doesn't work in my Demo project. I can't explain why. This is why I attached Demo project.

I have two resource files for FRA and RUS languages.

And try to switch language using next code

private void rbFra_Click(object sender, EventArgs e)
{
  System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("fr-Fr");
  Thread.CurrentThread.CurrentUICulture = cultureInfo;
  Thread.CurrentThread.CurrentCulture = cultureInfo;

  textBox1.Text = Properties.Resources.String1;
}

private void rgEng_Click(object sender, EventArgs e)
{
  System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("en-US");
  Thread.CurrentThread.CurrentUICulture = cultureInfo;
  Thread.CurrentThread.CurrentCulture = cultureInfo;

  textBox1.Text = Properties.Resources.String1;
}

private void rbRus_Click(object sender, EventArgs e)
{
  System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ru-RU");
  Thread.CurrentThread.CurrentUICulture = cultureInfo;
  Thread.CurrentThread.CurrentCulture = cultureInfo;

  textBox1.Text = Properties.Resources.String1;
}

But the result is always in English.

I think that Thread.CurrentThread.CurrentUICulture = ... should force to reload resource file and String1 return text from loaded resource.

Here is the DemoProject https://yadi.sk/d/4zEWVhso3Q4eqV

enter image description here

enter image description here

enter image description here

enter image description here

Upvotes: 1

Views: 1416

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125187

There are two problems with your settings:

  • You are putting your language resource files in a different folder than the neutral resource file. So the namespace would be different. Currently your neutral resource file is in Properties folder and the other resources are in root folder of the project.

  • The first part of the the file names of the language resources should be the same as the neutral resource file. So since the neutral resource file name is Resources.resx, those language files should be Resources.ru.resx and Resources.fr.resx. They lack s character at the end of file name.

After fixing those problems it will work well.

Apart from what you are trying to do, you can take a look at Windows Forms Localization feature:

Upvotes: 3

Related Questions