Question

This is my first time trying to localize, so bear with me. I've read up on it, but am still dealing with limited experience in the subject.

I've gone through a demo and have it working fine and I feel like I understand what happens behind the scenes. I moved the same concepts into my actual application, and it will not work. I've set the Localizable property to true and created an English and French resource file.

I've confirmed that my application is still pulling from the regular .resx file instead of en-US.resx or the French one.

I've tried setting these before the InitializeComponent call:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");

I've even tried to call a few particular ones explicitly:

resources.ApplyResources(this, "$this", new CultureInfo("en-US"));

resources gets declared as:

System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(myForm));

It all still pulls from the .resx file. One difference in this over my test project is that this form inherits from another, but I have made sure to set that form's Localizable property and resource files as well. Any ideas would be appreciated.

EDIT: I've still been working on this. I have a test project that works and my main application that does not. If I copy over the form that works in the the test over to the main, it does not work. If I add a reference to the main app in my test, the form from the main app also does not work even though it is straight copied from the test that works. Any ideas?

EDIT: It seems like the main difference/issue is that the basic project that is working is an executable by itself while my main application is calling an assembly with this localized form in it. Still no idea what is wrong. A lot of the tutorials I am seeing use System.Resources.ResourceManager, but when I change the form to localizable it auto-uses System.ComponentModel.ComponentResourceManager.

EDIT: I can literally say

resources.ApplyResources(this, "$this", new CultureInfo("fr-CA"));     

and it will ignore the $this.Text in the .fr-CA.resx file and use the default .resx file instead. I Don't Understand. And that is just me trying to FORCE it to work. I still need to just set the culture like normal. Does anyone have any ideas?

Was it helpful?

Solution 3

When you localize a form, it creates a folder per language in the build's output path that holds a resource file projectname.resources.dll.

I have a post build command that puts our .dlls elsewhere. I needed to change the command to also push out the resource files so that they were in the same directory. Once I did this, I was able to get it to work.

OTHER TIPS

Use the CreateSpecificCulture instead of new CultureInfo() for the threads CurrentCulture.

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(languageCode);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(languageCode);

As a side note, if your default culture is going to be English then you do not need to create a labels.en-US.rex. You can put that in the labels.resx and that will load up front and be the fallback. Gives you one less file you need to keep in sync.

If you add the relevant line below to the main method in Program.cs (other lines added for context)

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

// THIS LINE - Only need to set UI culture the other one is for currency etc
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");

// Or whatever your form is called.
Application.Run(new Form1());

This guarantees you are setting the culture before any InitialiseComponent method calls. I've eyeballed this working.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top