2

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.

Djordje
  • 437
  • 1
  • 12
  • 24
  • Don't user controls come with their own .resx file? – Zohar Peled Dec 21 '17 at 21:30
  • 1
    @ZoharPeled I can add .resx file in User Control but i would need a large amount of strings in it, because I don't know on which form will be implemented – Djordje Dec 21 '17 at 21:36
  • Sorry, I missunderstood the question. I get it now. – Zohar Peled Dec 21 '17 at 22:35
  • 1
    If the columns of the `DataGridView`, are always the same, then a simple also good option is using [`Localizable` and `Language` property](https://stackoverflow.com/a/32990088/3110834) of the `UserControl` at design time. Best way is opinion based. It depends to different requirements. If columns are not the same and can be added by the user of the `UserControl`, then you don't need to provide localization in the control itself, the form should be Localizable and you need to just expose columns as a public property of your control. – Reza Aghaei Dec 22 '17 at 06:09
  • 1
    @RezaAghaei thank you for ssuggestion, columns are not the same so you are right about form localization, but I am thinking long terms, if perhaps new column is added or modified , how could I update `.resx` file automatically, i know that is not my job, but just started working and trying to prove myself, and perhaps I should edit the question – Djordje Dec 22 '17 at 11:13
  • 1
    The time that you add the new column, add localized strings to the resource as well. To change a language or to add a new language you can distribute satellite assemblies. – Reza Aghaei Dec 22 '17 at 17:05
  • 1
    @RezaAghaei thank you, nice suggestion but first time I've heard about satellite assemblies I think this is a good [example](https://www.codeproject.com/Articles/352105/Satellite-Assembly-Example-in-Csharp-Step-by-Step), If you have some extra time feel free to answer my question. – Djordje Dec 23 '17 at 11:31
  • Seems your question is similar to [Best practice to make a multi language application in C#/WinForms](https://stackoverflow.com/questions/119568/best-practice-to-make-a-multi-language-application-in-c-winforms). Good way is to move all localizable resources to separate satellite assemblies. – Didgeridoo Dec 25 '17 at 15:23
  • Does this answer your question? [How to localize UserControl](https://stackoverflow.com/questions/17693979/how-to-localize-usercontrol) – gcode May 18 '23 at 20:49

2 Answers2

4

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).

George Vovos
  • 7,563
  • 2
  • 22
  • 45
  • 1
    Thank you for suggestion, I was thinking something like writing some code in `UserControl` constructor but you are right about unmaintainable code, thank you for the tool, though i won't use it now, I will definitely use some tool later on, now I am Interested in that code , and I will edit my question now. – Djordje Dec 22 '17 at 12:35
  • 1
    @Yollo The code isn't anything complex,all you have to do is assign a property from a resource file.See my updated answer – George Vovos Dec 22 '17 at 12:52
  • but still I don't know what columns will `DataGridVIew` have, maybe I should use `loop`, checkout my updated [question](https://stackoverflow.com/q/47874467/7517846) – Djordje Dec 24 '17 at 16:15
  • 1
    @Yollo You will only localize whatever controls are on your usercontrol at "design" time.Whoever adds columns/gridviews on your control will be responsible for the localization of the new controls (again the best choice is by code) – George Vovos Dec 24 '17 at 16:26
  • thank you for answer, it was of help, if you find about some free localization tools, pleas let me know. – Djordje Dec 27 '17 at 09:12
  • 1
    @Yollo I'll check but I don't really think it's worth it,too much trouble (I've done it). Better stick with one (or more if you want) Resource file just like you would do on a Web application – George Vovos Dec 27 '17 at 09:16
  • @Yollo I found a plugin (see the answer) but I guess you've already seen it.Didn't find any free tools but it looks like the commercial ones are relatively cheap,around 100 euro/dollars – George Vovos Dec 27 '17 at 09:32
1

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

wecky
  • 754
  • 9
  • 17