多语言和资源文件

发布于 2024-08-02 22:35:43 字数 886 浏览 7 评论 0原文

我有一个关于创建多语言应用程序和资源文件的使用的问题。我将解释我所做的事情以及我希望最终产品是什么样子。

我在 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 技术交流群。

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

发布评论

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

评论(3

离旧人 2024-08-09 22:35:43

.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

岁月蹉跎了容颜 2024-08-09 22:35:43
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

资源截图

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

吖咩 2024-08-09 22:35:43

比方说,你有 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

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

FirstName.Text = Language.lblFirstName

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

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

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

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