ResourceManager not picking right language
Asked Answered
K

1

6

Using Localization and Language Properties I have translated my Form1.

Within Form1_Load event I want to set the text for labels, buttons and so on...

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 is not working, it is always picking "default/fallback" english strings (but de CultureInfo is set). I have absolutely no idea whats wrong... I used the same code in a new sample application and it worked within this small sample application. But within my real application it does not work.

Also explicit telling ressourcemanager what Culture to use does return english string instead of german:

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

Any ideas?

Kurtiskurtosis answered 23/10, 2012 at 11:22 Comment(4)
Try to invoke SetLanguage() in constructor.Saliva
I tried SetLanguage() in constructor. Same issue - it's returning english string.Kurtiskurtosis
According to MSDN msdn.microsoft.com/en-us/library/y99d1cd3(v=vs.110).aspx it should go in Form1() and also before calling the InitializeComponent functionSaliva
I used this tutorial: codeproject.com/Articles/13125/…Kurtiskurtosis
P
0

This section is the important one. When we set the form's localizable to true and build the application, Visual Studio .NET creates resources specific to the culture specified in the Language property of the form. Visual Studio .NET creates the resource file using resgen.exe. We can also create resources by 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 a binary resources file mytext.en-us.resources and //writes an XML-based output file named mytext.en-us.resx Now the resources are created, but we need to link them to the application, so this is done by Visual Studio .NET using AL.exe (Assembly linker). We can also do it manually by doing this:

 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 in MSDN documentation.

Predicament answered 20/10, 2013 at 19:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.