多语言和资源文件
我有一个关于创建多语言应用程序和资源文件的使用的问题。我将解释我所做的事情以及我希望最终产品是什么样子。
我在 VB.Net 中执行此操作并使用 Visual Studio 2008
创建新项目后,我向该项目添加了一个资源文件(添加 -> 新项目,选定的资源文件,将其命名为 Resource1.resx)。
然后,我双击资源文件并能够添加一些名称和值。例如,
名称 – lblFirstName,值 – John 名称 – lblLastName,值 – Smith
在我的表单上,我有 2 个标签:名字和姓氏
在代码中,我添加了
FirstName.Text = My.Resources.Resource1.lblFirstName
LastName.Text = My.Resources.Resource1.lblLastName
如果我运行此代码,它工作正常。约翰和史密斯显示在标签上。
现在我的问题。假设标签(按钮、菜单项等)不是名字和姓氏,实际上是在不同语言中不同的单词。我想要的是像
EnglishText.resx 这样的东西 西班牙语文本.resx GermanText.resx
每个资源文件将包含相同的名称,只是不同的值。根据用户选择的语言(从菜单中)决定,我如何获取要使用的匹配资源文件。
基本上我想要的是
FirstName.Text = My.Resources.<Language Specific Resource File>.lblFirstName
这样的事情可能吗?这是可以接受的方法吗?有更好的方法吗?
任何提示或建议将不胜感激。我尝试经常检查是否有后续问题或是否需要提供更多信息。
I have a question about creating a multi-language application and the use of resource files. I will explain what I have done and what I would like the final product to be like.
I am doing this in VB.Net and using Visual Studio 2008
After creating a new project, I added a resource file to the project (Add -> New Item, Selected Resource File, named it Resource1.resx).
I then double clicked the resource file and was able to add some names and values. For example,
Name – lblFirstName, value – John
Name – lblLastName, value – Smith
On my form, I have 2 labels: FirstName, and LastName
In Code, I added
FirstName.Text = My.Resources.Resource1.lblFirstName
LastName.Text = My.Resources.Resource1.lblLastName
If I run this code, it works fine. John and Smith are displayed on the labels.
Now for my question. Say instead of first and last name the labels (buttons, menu items, etc.) were actually words that would be different in different languages. What I would like is to have something like
EnglishText.resx
SpanishText.resx
GermanText.resx
Each resource file would contain the same names, just different values. Depending on what language was selected, decided by the user (from a menu), how can I get the matching resource file to be used.
Basically what I want would be
FirstName.Text = My.Resources.<Language Specific Resource File>.lblFirstName
Is something like this possible? Is this an acceptable approach? Is there a better way of doing this?
Any tips or advice would be greatly appreciated. I try to check often to see if there are follow up questions or if more information needs to be provided.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
.NET 平台在构建时就考虑到了本地化。基于当前文化,已经存在一种天生的机制来本地化组件和资源。在尝试自己推出之前,您应该阅读以下一些入门链接:
http:// /msdn.microsoft.com/en-us/library/bb398937.aspx
http://msdn.microsoft.com/en-us/goglobal/bb688096。 ASPX
The .NET platform is built with localization in mind. There is already an inborn mechanism for localizing assemblies and resources based on the current culture. Here are some starter links you should read before trying to roll your own:
http://msdn.microsoft.com/en-us/library/bb398937.aspx
http://msdn.microsoft.com/en-us/goglobal/bb688096.aspx
资源截图
Resource screenshot
比方说,你有 3 种语言,你可以做这样的事情:
然后你可以通过以下方式使用它:
我刚刚在这个表单中完成了这个,它没有经过测试,所以很抱歉,如果它不起作用
Let's say, you have 3 languages you could do something like this:
You could then use it by:
I've just done this from within this form, it's not tested so sorry if it doesn't work