vb.net 多语言和资源文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1342624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:22:51  来源:igfitidea点击:

Multi-Language and Resource Files

vb.netresxmultilingual

提问by

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

我在 VB.Net 中执行此操作并使用 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).

创建新项目后,我在项目中添加了一个资源文件(Add -> New Item,Selected Resource File,命名为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

姓名 – lblFirstName, value – John 姓名 – lblLastName, value – Smith

On my form, I have 2 labels: FirstName, and LastName

在我的表单上,我有 2 个标签:FirstName 和 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

EnglishText.resx 西班牙语Text.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.

任何提示或建议将不胜感激。我尝试经常检查以查看是否有后续问题或是否需要提供更多信息。

回答by

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:

.NET 平台在构建时考虑了本地化。已经有一种基于当前文化的本地化程序集和资源的天生机制。在尝试推出自己的链接之前,您应该阅读以下一些入门链接:

http://msdn.microsoft.com/en-us/library/bb398937.aspx
http://msdn.microsoft.com/en-us/goglobal/bb688096.aspx

http://msdn.microsoft.com/en-us/library/bb398937.aspx
http://msdn.microsoft.com/en-us/goglobal/bb688096.aspx

回答by ahmet

Imports System.Globalization
Imports System.Resources

Public Class Form1
    Public rm As Resources.ResourceManager

    Private Property CultureInfo As CultureInfo
    Public Function getRMValue(ByVal strValue As String)
        Dim strLanguage As String

        If IsNothing(rm) Then
            strLanguage = CultureInfo.CurrentCulture.ToString.ToUpper.Substring(0, 2)
            If strLanguage = "EN" Then
                rm = My.Resources.English.ResourceManager
            Else
                rm = My.Resources.Turkce.ResourceManager
            End If
        End If
        getRMValue = rm.GetString(strValue)
    End Function
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    End Sub
    Private Sub btnBye_Click(sender As Object, e As EventArgs) Handles btnBye.Click
        MessageBox.Show(getRMValue("messagebox"))

    End Sub
End Class

Resource screenshot

资源截图

回答by Kenneth Rozendaal

Let's say, you have 3 languages you could do something like this:

比方说,你有 3 种语言可以做这样的事情:

If LanguageChanger<change this to the way you let people change languages> = "English" Then
Language = My.Resources.EnglishText 
else if LanguageChanger = "Spanish" Then 
Language = My.Resources.SpanishText
else if LanguageChanger = "German" Then 
Language = My.Resources.GermanText
End if

You could then use it by:

然后您可以通过以下方式使用它:

FirstName.Text = Language.lblFirstName

I've just done this from within this form, it's not tested so sorry if it doesn't work

我刚刚在这个表格中完成了这项工作,它没有经过测试,如果它不起作用很抱歉