Home » Programming » C# » Multilingual applications with VB.NET

Multilingual applications with VB.NET

posted in: C#, Programming, VB.Net 2

In this simple tutorial you’ll learn how to manage different languages for a vb.NET application and the simplest way to access programmatically to the string values in the resource compiled files (*.resx) created by Visual Studio;

Adding resource files:

First of all, add a folder named “resources/” in your project.

Inside this folder, right click then from the context menu choose “add an element” -> “new element” -> select “Resource File (resx)” -> create a file named “Resources.en-US.resx”


In the “Resources.en-US.resx” file, You have to write all the strings used by your application.
It’s important that your language resource file is named as “Resources.{Language Code Name}.resx”, so VS can reach automatically this file.

The rsx file is just a list of Key-Value items, then You have to give a key-name for each string value; don’t use spaces in key-names, otherwise it’s best using “_” symbol.

Insert only two test lines in your new rsx file as the following…

KEY – VALUE:

TEST_STRING1 us1
TEST_STRING2 us2

Then, compile your project and if it’s all right, You will find a new folder in “binRelease” named “en-US” created by VS, with a file “{your project name}.resources.dll”; this file contains all the strings for the english-US language (“en-US”) and right now You can access them programmatically.

If You want add a new language to your application, just copy the rsx file and rename it in “Resources.{Language Code Name}.resx” where the language code name is one of those listed at http://msdn.microsoft.com/en-us/library/ee825488(v=cs.20).aspx.

Step 2, how to programmatically select the language and strings:

Add a vb class “Resources.vb” in the “resources” folder with the following code

[sourcecode language=”vb”] Public Class Resources

Public Shared Function GetValue(ByVal name As ResourceName) As String
Return My.Resources.ResourceManager.GetObject(name.ToString)
End Function

Public Shared Property Language() As CultureCodeName
Get
Dim c As CultureCodeName = Nothing
Dim cs As String = System.Threading.Thread.CurrentThread.CurrentUICulture.ToString().Replace(New Char() {"-"c}, New Char() {"_"c})
For Each cc As CultureCodeName In [Enum].GetValues(GetType(CultureCodeName))
If cs = cc.ToString Then
c = cc
Exit For
End If
Next
Return c
End Get
Set(ByVal cultureCodeName As CultureCodeName)
Dim c As String = cultureCodeName.ToString.Replace(New Char() {"_"c}, New Char() {"-"c})
System.Threading.Thread.CurrentThread.CurrentUICulture = _
System.Globalization.CultureInfo.GetCultureInfo(c)
End Set
End Property

End Class
[/sourcecode]

then, add a vb “code file” named “CultureCodeNames.vb” in “resources” folder with the following code

[sourcecode language=”vb”]

Public Enum CultureCodeName

en_US
‘if You want add a new language, after creating the rsx file,
‘also add here the code name, with "-" replaced in "_"

End Enum

[/sourcecode]

Finally, add the last vb codefile “ResourceNames.vb” in “resources” folder with this code

[sourcecode language=”vb”]

Public Enum ResourceName

‘add here all the key names in the RSX file
TEST_STRING1
TEST_STRING2

End Enum

[/sourcecode]

The configuration is finished.
In your project, add a button to a form, then add the following vb code in the codebehind to learn how to access to different language RSX files and their strings inside.

[sourcecode language=”vb”]

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

‘Select a language for your app
Resources.Language = CultureCodeName.en_US

‘show test string 1
MessageBox.Show(Resources.GetValue(ResourceName.TEST_STRING1))

‘show test string 2
MessageBox.Show(Resources.GetValue(ResourceName.TEST_STRING2))

‘show the current language code
MessageBox.Show(Resources.Language.ToString)

End Sub
[/sourcecode]

It’s very useful because when you will be writing the code, VS automatically suggests the strings that you can use, saving a lot of time and by selecting a language with “Resources.Language”, You can translate your application strings on the fly.

Have a nice day!

Max


2 Responses

  1. Edgar
    | Reply

    How to use this code for Pocket PC app

  2. Thor
    | Reply

    Hello, unfortunely the return of GetValue is always nothing. I followed exactly the guide but I can’t understand why it is not working.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.