简体   繁体   中英

Best way to localize UserControl in c# (winforms)

Do to my lack of knowledge I've edited this question

I am making a UserControl with DataGridView in it and I want to simplify implementation process as much as possible, so I am wondering should I do that with localization? Do to my knowledge and research thus far, my approach for localization is this:

For example say I have one button on my form/UserControl with text property set to "hello" , now I want to localize my form/UserControl for Italian language.

  1. Set localizable property to true and choose language (in this case Italian)
  2. Google translate
  3. Set text property to "Ciao"

English is by default so i already have .resx file in my form, but after this VS will generate resources for Italian with button.Text property as key and "ciao" as value , if i understood correctly, but what happens if someone comes and change button.Text property from hello to "hello world", then my Italian resources won't be correct unless they are changed manually, is there a way to somehow do this change automatically?

I am wondering this, because when my UserControl with DataGridView is implemented on some form , I can't tell which columns will my DataGridView have, so I am wondering should I leave localization process to the person who implements my control?

Thank you, I really appreciate the help, and sorry for edit.

The best option is to set the (localized) text using code (use one Resource file).
You can do this in your form's/user control's constructor for example.
Try to avoid using a resx per form/user control because this will probably lead to unmaintainable code (duplicated key/values) unless you use a third party tool to localize the entire app like ( Infralution Globalizer )

The above tool is not free,but it's the only one I've used

The code in your constructor will look like this (Assuming you have a YourResourceFile.resx)

public MyUserControl()
{
        columnFirstName.Header = YourResourceFile.FirstName;
        columnLastName.Header = YourResourceFile.LastName;
}

If you want to add more columns on your grid,you'll add:

  1. a key in the Resource file
  2. a new line in the constructor

In fact,step 1,usually, will not be necessary since the key will probably be there

Update:
There seems to be a popular VS plugin called ResXManager or ( here ).

The Localizable Property of your UserControl should be set to true.

In addition any custom Properties of your UserControl you want translated should have the addition:

[Localizable(true)]
public string MyTranslatableLabel
{
    get;set;
}

You might also have to delete any instantiation like below from the Designer.cs file of the Form that contains the UserControl to get it to work properly:

this.myUserControl.MyTranslatableLabel = "Initial label of user control";

All this was tested on MS VisualStudio 2017 Community

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM