0

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: enter image description here

enter image description here

RagnaRock
  • 2,432
  • 7
  • 34
  • 56

2 Answers2

1

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 
ɐsɹǝʌ ǝɔıʌ
  • 4,440
  • 3
  • 35
  • 56
  • I tried your code with no sucess... My only diference is that I'm using 2 resx files. I named them DateStrings.pt-PT.resx and DateStrings.en.US.resx , shouldnt it work this way? P.s.: I'm also using a form, my test app doesnt have any module – RagnaRock May 12 '15 at 14:05
  • 1
    [**Here**](http://www.codeproject.com/Articles/59193/Localizing-a-Windows-Application-with-Satellite-As) you have a nice step-by-step article on how to localize a vb.net application with satellite assemblies by using resource files (.resx) – ɐsɹǝʌ ǝɔıʌ May 12 '15 at 14:12
  • 1
    @RagnaRock Don't worry about not using Modules. You can declare your ResourceManager passing the Form instead Module as a parameter `Dim rm As New ResourceManager("DateStrings",GetType(Form1).Assembly)` – ɐsɹǝʌ ǝɔıʌ May 12 '15 at 14:20
  • thats how Im doing it, but it keeps sending an exception (the one I tell im my first post) – RagnaRock May 12 '15 at 14:36
  • This problem may occur because the Form class is not the first class in the code.Try to move all of the other class definitions so that they appear after the form's class definition – ɐsɹǝʌ ǝɔıʌ May 12 '15 at 14:57
  • I've added an image... I might have something wrong with my files organization... I've tried to replicate your conditions the best I could, but I have the same error – RagnaRock May 12 '15 at 16:59
  • Your file organization seems to be ok. Try to do right-click the .resx files in the Solution Explorer and click `Run Custom Tool`. This re-generates the auto-generated Resources.Designer.cs file. – ɐsɹǝʌ ǝɔıʌ May 13 '15 at 07:03
0

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
RagnaRock
  • 2,432
  • 7
  • 34
  • 56