如何创建一个多语言项目

时间:2015-05-12 11:23:19

标签: vb.net localization cultureinfo resourcemanager

我是VB.net的新手,我正在尝试做一个简单的多语言项目。

到目前为止,我已创建了2个资源文件:

config.assets.precompile += ['additional_manifests.css'] 

在他们两个中我都有相同的ID和不同的值(仅限字符串) (这些字符串将在多个表单中使用)

当我更改laguage时,我会这样做:

en-US.resx  
pt-PT.resx

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"))

我错过了什么?

在第一次消化后编辑: enter image description here

enter image description here

2 个答案:

答案 0 :(得分:1)

此示例需要下表中列出的基于文本的资源文件。每个都有一个名为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 

答案 1 :(得分:0)

好吧,我只是停止使用资源管理器...显然不需要,现在它的工作。当tuturials似乎失败时,我希望这可以帮助别人:\

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

结束班