3

I have a WinForms app in .Net4 where I'm having problems implementing multiple languages.

Disclaimer: I have searched and found many questions about globalization/localization, but the vast majority are trying to do something unusual, or custom. I'm having trouble with the basic out of the box stuff. I have read the documentation, but am still having problems.

I have a WinForms app, the default language is English. I need to display it in French on french PCs, it could be either fr-FR or fr-CA, so I when I set the form to localizable:=True, I changed the language to "French" and updated the labels, buttons, and other form controls. That part, I Think I've done correctly.

Form message boxes, I put all my strings into the resources file (by clicking my project, then the resources tab). I replaced the strings in code with references to my.ressource.{ressourcename}

I then added a new Resources.fr.resx in the myproject folder and translated all the strings it contained.

I tried to test this by adding the following line to the new() constructor of my startup form.

Thread.CurrentThread.CurrentUICulture = New CultureInfo("fr")

I also tried:

Thread.CurrentThread.CurrentUICulture = New CultureInfo("fr-CA")

The application still loads in English, even the form controls which are set to the neutral language 'French'.

In summary, there are 4 questions:

  1. Am I going about this correctly?
  2. Have I created the French resource file correctly?
  3. Should the Resources.fr.resx file be in the My Project folder (along side the default Resources.resx, or should it be in the root? Or alternatively should I be using a new file like MessageResources.resx and MessageResources.fr.resx and place both of those in the root?
  4. How do I force the entire application to use French instead of default language?
NapkinBob
  • 632
  • 7
  • 19
  • 1
    If the application should be automatically shown in French language, on French Windows, then it's enough to create French resources. You don't need to set `CurrentCulture` and `CurrentUICultuer`. If you have French resources, on a French Windows, those resources will be automatically used. – Reza Aghaei Aug 27 '18 at 17:26
  • 1
    If for any reason you need to set the language by code based on user preference, do it before `Application.Run` in main entry point. – Reza Aghaei Aug 27 '18 at 17:28
  • 1
    The resource files can be everywhere in the project. The files for different language, for a specific resource, should be at the same location. For example, StringResource.resx and StringResource.fr.resx should be at the same level. Also make sure the keys in the resource files are the same. – Reza Aghaei Aug 27 '18 at 17:30
  • Thanks @RezaAghaei, I was only trying to set the language as a test. My application is starting from a form. To change anything before `Application.Run` I *think* I would need to change the start up to `Sub Main()` and then call `System.Windows.Forms.Application.Run(New MyForm())` is that correct? – NapkinBob Aug 28 '18 at 16:12
  • 1
    You need to have `Application.EnableVisualStyles()` and `Application.SetCompatibleTextRenderingDefault(false)` and `Application.Run(new Form1())` in Sub Main. – Reza Aghaei Aug 28 '18 at 16:44
  • I created a `Sub Main()` and set it as my start up object, then added the three lines you mention above. The application STILL loads in English only despire adding `Thread.CurrentThread.CurrentUICulture = New CultureInfo("fr")` and `Thread.CurrentThread.CurrentCulture = New CultureInfo("fr")` at the very begining of my `Sub Main()` – NapkinBob Aug 28 '18 at 21:16
  • You definitely have a mistake in one of the steps. It's easy and you can arrange the environment (Form, localization, resources) in 2-3 Minutes. You may want to share [MCVE] and a really simple project on GitHub. – Reza Aghaei Aug 29 '18 at 01:56

1 Answers1

0

Thanks for all your help. As it turns out, my localization settings were all correct. The problem was that I was using MSBuild.ILMerge.Task (NuGet package) to merge my assemblies into a single EXE. It seems that was affecting the localized resources.

Disabling that merge solved the problem. I'll start a new question more specific to how to handle that particular scenario.

NapkinBob
  • 632
  • 7
  • 19