ResourceManager does not select the correct language

Using localization and language properties I translated my form1.

In the Form1_Load event, I want to set text for labels, buttons, etc.

private void Form1_Load(object sender, EventArgs e) { SetLanguage(); } 

SetLanguage Method:

 private void SetLanguage() { System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de"); System.Resources.ResourceManager rm = new System.Resources.ResourceManager(typeof(Form1)); button1.Text = rm.GetString("button1.Text"); linkLabel1.Text = rm.GetString("linkLabel1.Text"); checkBox1.Text = rm.GetString("checkBox1.Text"); } 

But it does not work, it always selects the "default / fallback" English lines (but the CultureInfo parameter is set). I donโ€™t know at all whatโ€™s wrong ... I used the same code in a new example application and worked in this small sample application. But in my real application this does not work.

There is also an explicit indication to ressourcemanager that Culture to use returns an English string instead of german:

 MessageBox.Show(rm.GetString("button1.Text", new System.Globalization.CultureInfo("de"))); 

Any ideas?

+4
source share
1 answer

This section is important. When we set the localizable form to true and create the application, Visual Studio .NET creates the resources specific to the culture specified in the Language property of the form. Visual Studio .NET creates a resource file using resgen.exe. We can also create resources using the following syntax:

 resgen mytext.en-us.txt mytext.en-us.resources resgen mytext.en-us.resources mytext.en-us.resx 

// This command reads the binary resource file mytext.en-us.resources and // writes an XML-based output file called mytext.en-us.resx Now the resources are created, but we need to link them to the application, so this is done with using Visual Studio.NET using AL.exe (link collector). We can also do this manually by following these steps:

  al /t:lib /embed:mytext.en-us.resx /culture:en-us /out:Myapplication.resources.dll 

You can find more about resgen.exe and al.exe files in the MSDN documentation.

0
source

Source: https://habr.com/ru/post/1441532/


All Articles