本地化 Windows 窗体

发布于 2024-12-09 16:32:35 字数 664 浏览 0 评论 0原文

我正在从事一个涉及本地化大量 Windows 窗体的项目。

我们使用 TableLayoutPanel 控件来处理布局,该控件效果很好。

我们遇到的一个问题是,当我们将 Form.Localized 属性设置为 True 时,我们最终必须为每种语言的每个表单管理一个 .resx 文件。如果 .resx 文件仅包含本地化文本,但它还包含大量表单布局数据,那就没问题了。

有没有办法将可本地化的文本元素与控件布局信息分开,并在 Visual Studio IDE 中继续工作?

我注意到我可以修改表单的设计器文件以查看另一个资源文件,但是当我使用表单设计器时,这些更改将被删除:

'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Dock = System.Windows.Forms.DockStyle.Top
Me.Label1.Location = New System.Drawing.Point(3, 0)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(61, 13)
Me.Label1.TabIndex = 1
Me.Label1.Text = My.Resources.ResourceManager.GetString("Form1_Label1_Text")

I'm working on a project that involves localizing a large number of Windows Forms.

We're dealing with layout using the TableLayoutPanel control, which works nicely.

One area we're striking problems with is when we set the Form.Localizable property to True, we then end up having to manage one .resx file per form per language. That'd be fine if the .resx files only contained the Localized text, but it also contains a vast amount of layout data for the form.

Is there a way to separate the localizable text elements from the control layout information, that continues to work in the visual studio IDE?

I've noticed that I can modify my the form's designer file to look at another resource file, but when I use the form designer, these changes are deleted:

'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Dock = System.Windows.Forms.DockStyle.Top
Me.Label1.Location = New System.Drawing.Point(3, 0)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(61, 13)
Me.Label1.TabIndex = 1
Me.Label1.Text = My.Resources.ResourceManager.GetString("Form1_Label1_Text")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

糖粟与秋泊 2024-12-16 16:32:35

我从来没有找到一个让我完全满意的答案。但这是我最终实现的解决方案:

  1. 我最终设置了 Form.Localizes = false 因为我不想维护所有这些相同的 .resx 文件

  2. 所有 UI 元素都获得一个事实上的值(如“O_K_”),但显然不是本地化。

  3. 我对待表单/控件的方式与所有其他可本地化内容(错误消息、日志消息等)完全相同,只是为它们提供了唯一的键,例如“cmdOkTextOK”,以便它们可以在多个表单之间共享

  4. 我创建了LocalizeComponent () 函数在每个表单上,并在 InitializeComponent() 函数之后立即从构造函数中调用它,如下所示:

Public Class Form1
    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        LocalizeComponent()
    End Sub

    Private Sub LocalizeComponent()
        ' Localizes all the ui elements from a common Resource
        Me.SuspendLayout()
        Me.cmdOK.Text = My.Resources.ResourceManager.GetString("cmdOKTextOK")
        Me.cmdCancel.Text = My.Resources.ResourceManager.GetString("cmdCancelTextCancel")
        Me.cmdApply.Text = My.Resources.ResourceManager.GetString("cmdApplyTextApply")
        Me.ResumeLayout(False)
    End Sub
End Class

这会将可见控制元素的本地化移出设计器文件(每次进行表单编辑时都会将其销毁)并将其放入回到我的控制之下。

我仍然不是 100% 满意,因为控件是在运行时使用非本地化字符串创建的,而这些字符串稍后会更新。但这将使我摆脱我真的不想要的维护噩梦!

感谢所有不辞辛劳回答的人。我很感激!我不确定在回答自己的问题时你应该做什么来将其标记为“已回答”,所以如果有人能指出这一点而不会让我太难受,我将不胜感激。

I never found an answer to this I was completely happy with. But this is the solution I ended up implementing:

  1. I ended up setting Form.Localizable = false because I didn't want to maintain all those identical .resx files

  2. All the UI elements get a de-facto value (like "O_K_") that is visibly not localized.

  3. I treated the form / controls exactly the same as all my other localizable content (error messages, log messages etc.) and just gave them unique keys like "cmdOkTextOK" so they could be shared across multiple forms

  4. I created LocalizeComponent() functions on every form and called it from the constructor immediately after the InitializeComponent() function, as follows:

Public Class Form1
    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        LocalizeComponent()
    End Sub

    Private Sub LocalizeComponent()
        ' Localizes all the ui elements from a common Resource
        Me.SuspendLayout()
        Me.cmdOK.Text = My.Resources.ResourceManager.GetString("cmdOKTextOK")
        Me.cmdCancel.Text = My.Resources.ResourceManager.GetString("cmdCancelTextCancel")
        Me.cmdApply.Text = My.Resources.ResourceManager.GetString("cmdApplyTextApply")
        Me.ResumeLayout(False)
    End Sub
End Class

This moves the localization of visible control elements out of the designer file (where it was destroyed each time I did a form edit) and puts it back under my control.

I'm still not 100% happy, because the controls are being created with non-localized strings at run time that are latter being updated. But it will save me from a maintenance nightmare I really don't want!

THANKS to everyone who took the trouble to answer. I appreciate it! I'm not sure what you're meant to do when answering your own question to mark it 'answered' so if someone could point me at that without burning me too bad that'd be appreciated.

蓝天白云 2024-12-16 16:32:35

您可以将文件作为资源,所以这是您应该能够完成的事情,因为它在运行时不会更改,只能从​​您所说的内容进行访问。例如,可能是包含语言条目和翻译文本的 xml 文件。 <输入键=“btnOK”>确定。在运行时,您可以决定在

<Languages>
    <Language name="Spanish">
         <Entry key="Yes">Si</Entry>

我工作的最后一个地方加载哪种语言节点,我们使用 xml 文件,每种语言一个,我们将其与安装捆绑在一起。加载解决方案后,我们将所需的文件加载到全局哈希表中,并以这种方式设置每一位文本。可能有一个更优化的解决方案,但也许这些方面的东西可能会对您有所帮助。例如,hashTable("successfulSaveMsg") 将返回“您已成功保存您的小部件”。

You can have files as resources, so that's something you should be able to accomplish since it's not changed during the run-time, only accessed from what you've said. Maybe an xml file with language entries and the translated text, for example. <entry key = "btnOK">Ok</entry>. At run-time you can make the decision what language node to load in

<Languages>
    <Language name="Spanish">
         <Entry key="Yes">Si</Entry>

The last place I worked, we used xml files, one for each language, that we bundled with the install. When the solution was loaded in, we loaded the desired file into a global HashTable and set each bit of text that way. There may be a more optimal solution, but maybe something along these lines may help you. hashTable("successfulSaveMsg") would return "You have successfully saved your widget", for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文