关于多语言的一些问题

发布于 2024-10-17 12:14:41 字数 999 浏览 7 评论 0原文

最近,我决定向我的应用程序添加 4 种语言。 我读到了如何做到这一点并且我成功了。

但我想问两个问题。

第一个问题:是否有更好的方法来更改每个控件的文本

private System.Resources.ResourceManager rm;

System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr");
rm = new System.Resources.ResourceManager(typeof(MainForm));

,然后为每个控件编写此行:

aboutToolStripMenuItem.Text = rm.GetString("aboutToolStripMenuItem.Text");
addTaskToolStripMenuItem.Text = rm.GetString("addTaskToolStripMenuItem.Text");
addTaskToolStripMenuItem1.Text = rm.GetString("addTaskToolStripMenuItem1.Text");
...

第二个问题:假设 label1 的文本是“test”,在其他语言中是“testest”,然后是大小标签的内容会改变,这是可以的。但如果我得到 label2 他的位置靠近 label1,则 label1 可能位于 label2 的顶部。如何移动 label1 与 label2 进行比较,以便无论 label1 中的文本有多长,label2 的位置都将相对于 label1。我不想在程序中使用计算,我想知道是否有其他方式,例如控件之一中的属性。

编辑: 经过长时间的思考,我决定将 XML 用于我的多语言。这样我就可以让人们翻译它并为我上传它,而且我可以在运行时使用它或重新加载程序。

关于控件的相对位置,我将使用 FlowLayoutPanel 或 TableLayoutPanel 我将进一步检查哪个更好。

Recently, I decided to add 4 languages to my application.
I read about how to do it and I succeed.

But there are two problems/questions I would like to ask.

First question: Is there a better way to change the text of each control instead

private System.Resources.ResourceManager rm;

System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr");
rm = new System.Resources.ResourceManager(typeof(MainForm));

and then for each control to write this line:

aboutToolStripMenuItem.Text = rm.GetString("aboutToolStripMenuItem.Text");
addTaskToolStripMenuItem.Text = rm.GetString("addTaskToolStripMenuItem.Text");
addTaskToolStripMenuItem1.Text = rm.GetString("addTaskToolStripMenuItem1.Text");
...

Second Question: lets say the text of label1 is "test" and in other language its "testest" then the size of the label will change, which is ok. but If I got label2 that his location is near label1, label1 might be on the top of label2. How can I move the label1 compare with label2 so no matter how long the text in label1 will be, label2's location will be relative to label1. I dont want to use calculations in the program, I want to know if there's other way like property in one of the controls.

EDIT:
after long thinking, I decided to use XML for my multilanguage. this way I can let the people translate it and upload it for me plus I can use it on runtime instead or reload the programs.

About the relative pos of controls I will use FlowLayoutPanel or TableLayoutPanel I will check further which is better.

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

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

发布评论

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

评论(3

司马昭之心 2024-10-24 12:14:41

卫星组件就是您正在寻找的。

Q1:在 VS 中,将表单的 Localized 属性设置为 true。然后相应地选择语言属性并在设计器中输入您的翻译。这样,您只需在启动时设置线程区域性,.NET 就会为您加载正确的语言。无需添加额外的代码。

Q2:同样,在设计器中选择语言后,只需移动控件即可:它们的新位置/大小是翻译的一部分,将由 .NET 自动处理。

Satellite assemblies is what you are looking for.

Q1: In VS, set your form's Localizable property to true. Then select the language property accordingly and type your translations in the designer. That way, you simply need to set the thread culture at start-up and .NET will load the correct language for you. No need to add extra code.

Q2: Again, after you selected the language in the designer, just move the controls around: Their new location/size is part of the translation and will be handled automatically by .NET.

扮仙女 2024-10-24 12:14:41

在运行时将 GUI 更改为不同的区域性并不是一个好主意,最好说一些类似您需要重新启动应用程序才能看到更改的内容。

虽然如果你需要这样做,你需要从新的文化中重新加载所有资源(与InitializeComponen或多或少相同),不仅仅是文本,因为位置、大小等也可能会改变。此外,您还需要更改线程区域性,以便错误、消息和新控件也具有正确的区域性(也以正确的语言显示它们)。

您可以通过以下方式设置应用程序区域性:

CultureInfo appCulture = CultureInfo.CreateSpecificCulture("fr");
Thread.CurrentThread.CurrentCulture = appCulture; 
Thread.CurrentThread.CurrentUICulture = appCulture;

您需要特定的区域性才能在格式化和解析时使用它。

It's not a very good idea change the GUI to a different culture while it's running, it's better to say something like you need to restart the application to see changes.

Although if you need to do it, you need to reload all the resources from the new culture (more or less the same than the InitializeComponen does), not only the text, because the location, size and so on, may be changed too. Also you need to change the thread culture in order to the errors, message and the new controls have the correct culture too (to show them in the correct language too).

You can set your application culture with:

CultureInfo appCulture = CultureInfo.CreateSpecificCulture("fr");
Thread.CurrentThread.CurrentCulture = appCulture; 
Thread.CurrentThread.CurrentUICulture = appCulture;

You need an specific culture to use it on formating and parsing.

独守阴晴ぅ圆缺 2024-10-24 12:14:41

回答你的第一个问题:

如果你真的想遵循这个方案,也许使用反射或自动代码生成是更容易管理的替代方案。我通常编写自己的 GetString 方法,该方法采用默认的英语字符串作为参数(如果无法为当前语言动态加载资源则使用)。但我也不确定这是否是最好的解决方案......


回答你的第二个问题:

在Winforms中使用 TableLayoutPanelFlowLayoutPanel 或另一个布局组件来相对定位控件。例如,可以指定标签是否适合其内容(自动调整大小),或者是否应停靠,如果是,则采用何种对齐方式。几乎没有任何用例需要繁琐的自我管理或计算。

链接: http://msdn.microsoft.com/ en-us/library/z9w7ek2f(v=VS.100).aspx

In answer to your first question:

If you really want to follow that scheme maybe using Reflection or automatic code generation is an alternative for easier management. I usually write my own GetString method that takes a default english string as an argument (used if no resource can be loaded dynamically for the current language). But I am not sure if it is the best solution either...


In answer to your second question:

In Winforms use a TableLayoutPanel or FlowLayoutPanel or another layout component to relatively position the controls. It is possible to specify if the Label fits to its content (AutoSize) for example or if it shall Dock and if yes with what Alignment. There is nearly no use case that would require a tedious self management or computation.

Link: http://msdn.microsoft.com/en-us/library/z9w7ek2f(v=VS.100).aspx

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