如何创建多语言项目
How to create a multilingual project
我是 VB.net 的新手,我正在尝试做一个简单的多语言项目。
到目前为止,我已经创建了 2 个资源文件:
en-US.resx
pt-PT.resx
在他们两个中我有相同的 ID 和不同的值(仅限字符串)
(这些字符串将用于多种形式)
当我更改语言时,我会这样做:
Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
或
Thread.CurrentThread.CurrentCulture = New CultureInfo("pt-PT")
根据我想看的语言。
但我不知道如何正确访问我的资源文件,正在做:
Dim assembly As System.Reflection.Assembly
assembly = Me.GetType.Assembly
Dim resourceManager As New System.Resources.ResourceManager("My.Resources", assembly)
MsgBox(resourceManager.GetString("TEST"))
给我一个例外System.Resources.MissingManifestResourceException' occurred in mscorlib.dll
我错过了什么?
第一次建议后编辑:
此示例需要以下 table 中列出的基于文本的资源文件。每个都有一个名为 DateStart
的字符串资源。
Culture | File name | Resource name | Resource value
en-US DateStrings.txt DateStart Today is
pt-PT DateStrings.pt-PT.txt DateStart hoje é
此代码使用 GetString(String, CultureInfo)
方法检索特定于文化的资源。该示例的默认区域性是英语 (en),它包括葡萄牙语(葡萄牙)(pt-PT) 区域性的附属程序集。
Module Example
Public Sub Main()
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("pt-PT")
Dim cultureNames() As String = { "en-US", "pt-PT" }
Dim rm As New ResourceManager("DateStrings",GetType(Example).Assembly)
'Access to resource file
For Each cultureName In cultureNames
Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture(cultureName)
Dim dateString As String = rm.GetString("DateStart", culture)
Console.WriteLine("{0}: {1} {2}.", culture.DisplayName, dateString,
Date.Now.ToString("M", culture))
Console.WriteLine()
Next
End Sub
End Module
好吧,我只是停止使用资源管理器。显然不需要资源管理器,现在它可以工作了。当教程似乎失败时,我希望这对其他人有所帮助:\
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US")
MsgBox(My.Resources.MStrings.TEST)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("pt-PT")
MsgBox(My.Resources.MStrings.TEST)
End Sub
End Class
我是 VB.net 的新手,我正在尝试做一个简单的多语言项目。
到目前为止,我已经创建了 2 个资源文件:
en-US.resx
pt-PT.resx
在他们两个中我有相同的 ID 和不同的值(仅限字符串) (这些字符串将用于多种形式)
当我更改语言时,我会这样做:
Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
或
Thread.CurrentThread.CurrentCulture = New CultureInfo("pt-PT")
根据我想看的语言。
但我不知道如何正确访问我的资源文件,正在做:
Dim assembly As System.Reflection.Assembly
assembly = Me.GetType.Assembly
Dim resourceManager As New System.Resources.ResourceManager("My.Resources", assembly)
MsgBox(resourceManager.GetString("TEST"))
给我一个例外System.Resources.MissingManifestResourceException' occurred in mscorlib.dll
我错过了什么?
第一次建议后编辑:
此示例需要以下 table 中列出的基于文本的资源文件。每个都有一个名为 DateStart
的字符串资源。
Culture | File name | Resource name | Resource value
en-US DateStrings.txt DateStart Today is
pt-PT DateStrings.pt-PT.txt DateStart hoje é
此代码使用 GetString(String, CultureInfo)
方法检索特定于文化的资源。该示例的默认区域性是英语 (en),它包括葡萄牙语(葡萄牙)(pt-PT) 区域性的附属程序集。
Module Example
Public Sub Main()
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("pt-PT")
Dim cultureNames() As String = { "en-US", "pt-PT" }
Dim rm As New ResourceManager("DateStrings",GetType(Example).Assembly)
'Access to resource file
For Each cultureName In cultureNames
Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture(cultureName)
Dim dateString As String = rm.GetString("DateStart", culture)
Console.WriteLine("{0}: {1} {2}.", culture.DisplayName, dateString,
Date.Now.ToString("M", culture))
Console.WriteLine()
Next
End Sub
End Module
好吧,我只是停止使用资源管理器。显然不需要资源管理器,现在它可以工作了。当教程似乎失败时,我希望这对其他人有所帮助:\
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US")
MsgBox(My.Resources.MStrings.TEST)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("pt-PT")
MsgBox(My.Resources.MStrings.TEST)
End Sub
End Class