Reputation: 2640
I'm new to VB.net and I'm trying to do a simple multilingual project.
So far I've created 2 resource files:
en-US.resx
pt-PT.resx
in both of them I have the same ID's and diferent values (strings only) (these strings will be used across multiple forms)
When I change the laguage I do:
Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
or
Thread.CurrentThread.CurrentCulture = New CultureInfo("pt-PT")
Based on the language I want to see.
But I dont know how to access my resource files properly, Doing:
Dim assembly As System.Reflection.Assembly
assembly = Me.GetType.Assembly
Dim resourceManager As New System.Resources.ResourceManager("My.Resources", assembly)
MsgBox(resourceManager.GetString("TEST"))
Gives me an exception System.Resources.MissingManifestResourceException' occurred in mscorlib.dll
What am I missing?
edit after the first sugestion:
Upvotes: 0
Views: 2067
Reputation: 2640
Well I just stop using the resource manager. Apparently the resource manager is not needed and now its working. I hope this helps someone else when the tutorials seem to fail :\
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
Upvotes: 1
Reputation: 4512
This example requires the text-based resource files listed in following table. Each has a single string resource named DateStart
.
Culture | File name | Resource name | Resource value
en-US DateStrings.txt DateStart Today is
pt-PT DateStrings.pt-PT.txt DateStart hoje é
This code uses the GetString(String, CultureInfo)
method to retrieve culture-specific resources. The example's default culture is English (en), and it includes satellite assemblies for the Portuguese (Portugal) (pt-PT) culture.
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
Upvotes: 1