Robert
Robert

Reputation: 118

Localize form controls from one file in Class Library

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!

Upvotes: 1

Views: 999

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125277

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.

Upvotes: 1

Related Questions