搜索资源以获取多语言VB.NET的字符串 [英] Search resources for a string for Multiple Languages VB.NET

查看:114
本文介绍了搜索资源以获取多语言VB.NET的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。



我正在创建一个多语言应用程序。我已成功更改了CurrentCulture而没有关闭并重新打开我的应用程序,但我的问题是(状态标签和消息),因为我只能以编程方式更改它。无论如何,我需要通过创建语言的资源文件来更改(状态标签和消息),并通过编程方式从中获取文本,如下面的代码。

Hello.

I'm creating a multi language application. I have successfully changed the CurrentCulture without "close & reopening" my Application, But my problem is the (Status Labels & Messages) since i can only change it Programmatically. Anyway, I needed to change the (Status Labels & Messages) by creating a resource file of the language and get the "Text" from it programmatically like the code below.

If LangCB.Text = "English" Then
    MsgBox(My.Resources.EN.FirstMsg)
Else
    MsgBox(My.Resources.ES.FirstMsg)
End If
If LangCB.Text = "English" Then
    MsgBox(My.Resources.EN.SecondMsg)
Else
    MsgBox(My.Resources.ES.SecondMsg)
End If



代码剂量工作b我必须在每个地方做这件事(状态标签&消息)。但是我想知道是否有可能用更少的代码来做它,比如做一个循环左右(因为我不是那么擅长循环和每个人)。



我以为它会是这样的(有趣的是我猜)


And the code dose work but i have to do it in EVERY (Status Labels & Messages). But i was wondering if there is a possibility to do it with less code like doing a loop or so (since I'm not that good at "Loops" & "For Each").

I was thinking it will be something like this (funny i guess)

Private Sub ButtonClick
        StringInNewLang(FirstMsg)
End Sub

Private Sub StringInNewLang("string to search(FirstMsg)")
        If LangCB.Text = "English" Then
        For Each S As "string to search(FirstMsg)" In My.Resources.EN
        MsgBox(S)
        Next
        Else
        For Each S As "string to search(FirstMsg)" In My.Resources.ES
        MsgBox(S)
        Next
        End If
End Sub



在此先感谢:D


Thanks in Advance :D

推荐答案

我不明白为什么你要尝试重新发明轮子:)

本地化框架已经全部完成了你需要的。



第一个错误的做法,恕我直言,是通过比较ComboBox的文本属性直接处理本地化。理想情况下,您应该处理ComboBox的 SelectedIndexChanged 事件以设置当前的UI文化:

I don't exactly see why you are trying to reinvent the wheel :)
Localization framework already has got all what you need.

The first wrong thing to do, IMHO, is to handle localizations directly by comparing to a ComboBox' text property. Ideally, you should handle the SelectedIndexChanged event of the ComboBox to set the current UI culture:
Private Sub LangCB_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles LangCB_SelectedIndexChanged
   Thread.CurrentThread.CurrentCulture = new CultureInfo(LangCB.Text)
   '' or (I'm never sure about these two)
   Thread.CurrentThread.CurrentUICulture = new CultureInfo(LangCB.Text)
End Sub



然后你只需要调用r从其名称中获取资源,您不必指定文化,因为 ResourceManager 将为您处理,观察当前文化值以确定要搜索匹配的资源文件条目:


And then you just call the resource from its name, you do not have to specify the culture, as the ResourceManager will handle that for you, observing the current culture value to determine which resource file to search for matching entry:

MsgBox(My.Resources.FirstMsg)
MsgBox(My.Resources.SecondMsg)



如果没有提供给定文化的值,资源管理器将默认为默认文化(没有文化规范的.resx文件)。



或你的问题中有些东西我完全没有了。



增加本地化资源的例子



- Res.resx 文件(默认语言=英语)

资源名称:InvalidValue

资源值:无效价值



- Res.de.resx 档案(适用于德国文化)

资源名称:InvalidValue

资源价值:UngültigerWert(对不起翻译,最终;)) />


- Res.fr.resx 文件(法国文化)

资源名称: InvalidValue

资源价值:Valeur invalide



- 等等



你可以看到:

- 每个本地化文件都以默认的区域性文件名称加上文化标识符命名

- 在每个本地化文件中,资源名称必须相同



如果目前还不清楚,请告诉我。




If a value for a given culture is not provided, the resource manager will default to the default culture (the .resx file without culture specification).

Or, there is something in your issue I'm totally missing.

Added example of a localized resource

- Res.resx file (default language = english)
Resource name: InvalidValue
Resource value: Invalid value

- Res.de.resx file (for German culture)
Resource name: InvalidValue
Resource value: Ungültiger Wert (sorry for translation, eventually ;) )

- Res.fr.resx file (for french culture)
Resource name: InvalidValue
Resource value: Valeur invalide

- etc.

You can see that:
- Every localization file is named with the default culture file name plus the culture identifier
- In each localization file, resource names must be the same

Please let me know if it is still unclear.

除了解决方案1.



我个人认为重新发明轮子一直是技术史上最富有成效的活动之一,尤其是当谈到真正的汽车/自行车车轮。问题是潜在的发明者没有达到前任所达到的水平。一个业余爱好者的新面貌也可能是一件好事,但如果你长时间忽视现有的成就并且把你带回家的方法太严肃,那就不行了。你已经完成了尝试,这是非常幼稚和糟糕的,你可以通过学习现有的全球化和本地化技术找到它。



随着.NET,您永远不需要自己阅读资源。相反,您需要使用在添加资源时创建的自动生成的代码文件。这个想法是:你为一种文化创造所有资源,比如英国文化之一。然后创建单独的仅资源程序集,它们是主程序集的翻译副本,但只包含翻译成不同语言的资源;应根据文化名称命名这些程序集的输出目录。请注意,此机制允许外包翻译工作,而无需向本地化工作者或团队提供完整的源代码。这种组件称为附属组件。本地化后,主程序集可以通过更改线程的文化和UI文化自动切换到卫星资源,如解决方案1中所述。



有关详细信息,请看看我过去的答案:

如何使用3种语言的单个resx文件 [ ^ ],

winform .net 中的全球化/本地化问题[ ^ ],

< a href =http://www.codeproject.com/Answers/446585/globalization-in-winforms#answer1>全球化winforms [ ^ ]。



-SA
In addition to Solution 1.

I personally think that "reinventing the wheel" has been one of the most productive activity in the history of technology, especially when it comes to real car/bike wheels. The problem is when the potential inventor is not up to the level achieved by the predecessors. Fresh look of an amateur can also be a good thing, but not if you keep ignoring existing achievement for too long and take you home-baked approach too serious. You have done your attempt, and it's quite naive and bad, which you could find out by learning existing globalization and localization technology.

With .NET, you never need to read resources by yourself. Instead, you need to use auto-generated code file created when you add resources. The idea is: you create all resources for one culture, say, one of English cultures. Then you create separate resource-only assemblies which are translated copies of the main assembly, but contain only resources translated into different language; output directories of those assemblies should be named according to the names of the cultures. Note that this mechanism allows to outsource translation works without giving full source code to localization worker or a team. Such assemblies are called satellite assemblies. After localization, the main assembly can switch to the satellite resources automatically, by changing the thread's culture and UI culture, as explained in Solution 1.

For more detail, please see my past answers:
How to use a Single resx file for 3 languages[^],
globalization/localization problem in winform .net[^],
globalization in winforms[^].

—SA


这篇关于搜索资源以获取多语言VB.NET的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆