» » » Multilingual applications with VB.NET

Multilingual applications with VB.NET

posted in: C#, Programming, VB.Net | 0

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…



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

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
            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
            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 = _
        End Set
    End Property

End Class

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

Public Enum CultureCodeName

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

End Enum

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

Public Enum ResourceName

    'add here all the key names in the RSX file

End Enum

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.

    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

        'show test string 2

        'show the current language code

    End Sub

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!


Leave a Reply