简体   繁体   English

在c#中本地化UserControl的最佳方法(winforms)

[英]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? 我正在使用DataGridView制作一个UserControl ,我希望尽可能地简化实现过程,所以我想知道我应该用本地化吗? 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. 例如,假设我的form/UserControl上有一个按钮,文本属性设置为“hello”,现在我想将我的form/UserControl本地化为意大利语。

  1. Set localizable property to true and choose language (in this case Italian) 将localizable属性设置为true并选择语言(在本例中为Italian)
  2. Google translate 谷歌翻译
  3. Set text property to "Ciao" 将文本属性设置为“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? 英语是默认的,所以我已经在我的表单中有.resx文件了,但是在此之后,VS将生成意大利语资源,其中button.Text属性为 ,“ciao”为 ,如果我理解正确,但如果有人来,会发生什么更改button.Text属性从hello到“hello world”,然后我的意大利语资源将不正确,除非他们手动更改,有没有办法以某种方式自动进行此更改?

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? 我想知道这一点,因为当我的UserControlDataGridView在某个form上实现时,我不知道我的DataGridView会有哪些列,所以我想知道我应该将本地化过程留给实现我的控件的人吗?

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 ) 尽量避免在每个表单/用户控件上使用resx,因为这可能导致无法维护的代码(重复的键/值),除非您使用第三方工具本地化整个应用程序,如( 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) 构造函数中的代码将如下所示(假设您有一个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 实际上,通常,步骤1不是必需的,因为密钥可能会在那里

Update: 更新:
There seems to be a popular VS plugin called ResXManager or ( here ). 似乎有一个流行的VS插件,名为ResXManager或( 这里 )。

The Localizable Property of your UserControl should be set to true. UserControl的Localizable属性应设置为true。

In addition any custom Properties of your UserControl you want translated should have the addition: 此外,您想要翻译的UserControl的任何自定义属性都应该添加:

[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: 您可能还必须从包含UserControl的Form的Designer.cs文件中删除下面的任何实例化,以使其正常工作:

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

All this was tested on MS VisualStudio 2017 Community 所有这些都在MS VisualStudio 2017社区进行了测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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