1

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

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
DmitryB
  • 455
  • 1
  • 5
  • 18
  • I would add the Strings inside a single resource and index them that way Resource.String1...String3 try that.. – MethodMan Nov 26 '17 at 19:44
  • It's not completely clear why. If there is a standard mechanism for working with the localization of resources, then why not use it. 2. One file is not convenient for developers who want to add a new language. – DmitryB Nov 26 '17 at 19:49
  • then you need to figure out how to access 1 of 3 different resource files.. do a simple google search then – MethodMan Nov 26 '17 at 19:51
  • Does not the code < Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-Fr"); > do it automatically? – DmitryB Nov 26 '17 at 19:59
  • how would it know what Resource file you are trying to reference...? ask yourself that.. – MethodMan Nov 26 '17 at 20:08
  • At compile time VS create "fr" and "ru" folder in "bin\Debug". So when the Thread.CurrentThread.CurrentUICulture is changed it uses these folders to load TestLocalization.resources.dll with the necessary resources. – DmitryB Nov 26 '17 at 20:19
  • https://stackoverflow.com/questions/1142802/how-to-use-localization-in-c-sharp – MethodMan Nov 26 '17 at 20:24
  • Possible duplicate of [How to use localization in C#](https://stackoverflow.com/questions/1142802/how-to-use-localization-in-c-sharp) – Camilo Terevinto Nov 26 '17 at 20:29
  • You should restart your test application and assign the desired culture on program startup BEFORE opening the first form, e.g. in the `Main()` method. From my knowledge there is no easy way in WinForms to dynamically switch the culture during runtime. – KBO Nov 27 '17 at 07:57
  • @KBO No need to restart. Reopening the form is just for cases which the user is using Windows Forms Localization using `Localizable` and `Language` property. Also even in that case, the problem can be solved without reopening the form. – Reza Aghaei Nov 27 '17 at 09:52
  • @Reza Thanks for the comment, but in non-trivial apps you normally have a bunch of thread pool and worker threads. If you want to change the language of the entire app, the easiest way is to restart from scratch and set the `CultureInfo.DefaultThreadCurrentCulture` and `CultureInfo.DefaultThreadCurrentUICulture` additionally. So all threads, created in the app, have the right language. – KBO Nov 27 '17 at 10:08

1 Answers1

3

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:

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Thank you so much. It works. I did not know that the filename is part of the namespace for resources. – DmitryB Nov 27 '17 at 10:25