1

I am working on a C# WinForms application. With the idea of moving the application to ASP.NET in the future, the solutions has 2 projects;

  1. Class Library project with a data access class and a folder "Model" with a file per class/model.
  2. Windows Forms App project with all the forms, user controls and their logic.

New with localization, I read this article by Microsoft. According to their first step of "Localization Best Practices", it is advised to move all localizable resources to separate resource-only DLLs.

Because I already have a Class Library project (project 1) in my solution, I added a new folder "Localization" and created a two resource files "Strings.resx" and "Strings.en.resx". (The default language is in Dutch.)

To test this recommended setup, I cleared the text property of one of the buttons on the main form to replace it with a value from the resource file I've created. But I can't figure out how to connect the value of the resource file to the text property.

I found this post where is all has been done programmatically, but I am not sure if this is the only/best approach. And how do I notify the ResourceManager about the resource files in an other project (in the same solution). And do you still need to set the Localizable property of the form to True, when not using resource files per form, as per default?

Any help to send me in the right direction is welcome!

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Robert
  • 118
  • 1
  • 10

1 Answers1

1

You can work with those resource files like normal classes. You just need to make your resources accessible from outside of a class library by making them public. To do so, you can use either of the following options:

  • You can double click on resource file and open it in design mode and in its toolbar, set Access Modifier drop-down to to Public.

  • You can right click on resource file and click properties and then in properties window set Custom Tool to PublicResXFileCodeGenerator.

Then the resource class will be generated as a public class in your assembly.

Then to access to those resources, you can add the assembly as a reference to your windows application and use them like any other classes, something like:

saveButton.Text = MyResourceAssembly.Resources.StringResources.Save;

Note:

  • Usually Windows Forms application localization is different from ASP.NET application. In windows forms, you rely on Localizable and Language properties of form like what is described here.

  • If for any reason you wanted to use those resource files, you can take a look at this resource extender example which allows you to set text of controls using resource files at design time.

  • If there is a probability of having an RTL language in your supported languages, you may want to use RTL form or instead, an RTL Panel.

  • It's good idea to decorate your model classes with data annotation attributes and try to utilize those attributes in Windows Forms as well. For example you can implement IDataErrorInfo for your models and use data annotation validation attributes in Windows Forms as well as ASP.NET MVC.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Thanks! Works great! Some questions about the notes: 1. Are you telling that the resource files can't be used for an ASP.NET project to localize? 2. Does it matter if the text property is set at the constructor of the form, or at the _load event? I've seen examples where the load event is used for this. I just call a method LocalizeForm() from the constructor. 3. Do you please have a page with an example of your fourth note? – Robert May 31 '18 at 17:51
  • 1
    **1.** In fact they are more useful for ASP.NET project. For Windows Forms project, usually localization will be done in a different way. But if you are not going to use the common way of Windows Forms localization, it's OK to use those resource files in Windows Forms as well. **2.** In general, it doesn't matter to put them in Load or constructor. **3.** At the moment I don't have any example, but I have implemented similar approach in a project without any problem. I probably will share an article/blog post about it. – Reza Aghaei May 31 '18 at 17:58
  • 1
    @Robert I published a post about [DataAnnotations Validation Attributes in Windows Forms](http://www.reza-aghaei.com/dataannotations-validation-attributes-in-windows-forms/) – Reza Aghaei Jun 01 '18 at 03:16
  • Wow, you are the best! – Robert Jun 01 '18 at 06:08
  • @Robert You will like it: Need to support [DataAnnotations attributes for DataGridView in Windows Forms](https://stackoverflow.com/a/59885956/3110834) to control Column header texts, Order of columns, Tooltip, Format of columns? – Reza Aghaei Feb 05 '20 at 20:54