Aug 27, 2010

Windows Form Localization in .NET

This article explains the steps to implement localization for windows app using .NET

1. Design the interface by using table layout panel.

2. Change the localizable property of form to true.

3. Select the language by using the language property of form. It will create new resource file for that language.

4. Change the text property of each and every control into selected language.

5. Create the main resource for each and every language and add that into myproject folder of an application. By using this files we use to handle the message box and runtime string localization.
For example Resources.resx = file is use for default language( english-US) = this is for Spanish(Mexico).

To create this resources file: Add new item select the resource files add it to my project folder and Change its couple of property. OR you can use following tools
Custom tool :- VbMyResourcesResXFileCodeGenerator.
Custom tool namespace :- My.Resources.

6. Now access resource file to localize message in messagebox and runtime strings.

MessageBox.Show(My.Resources.NameOfString, My.Resources.Title, MessageBoxButtons.YesNo)

7. Our goal is to localize app on changing combobox (cboLanguage). For this we use settings file and add/update following block of code and update with your project namespace

     Public Shared ReadOnly Property [Default]() As MySettings
#If _MyType = "WindowsForms" Then
                If Not addedHandler Then
                    SyncLock addedHandlerLockObject
                        If Not addedHandler Then
                            AddHandler My.Application.Shutdown, AddressOf AutoSaveSettings
                            addedHandler = True
                        End If
                    End SyncLock
                End If
#End If
                Return defaultInstance
            End Get
        End Property
        <Global.System.Configuration.UserScopedSettingAttribute(), _
                 Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), _
                 Global.System.Configuration.DefaultSettingValueAttribute("en-US")> _
                Public Property UserLanguagePreference() As String
                Return CType(Me("UserLanguagePreference"), String)
            End Get
            Set(ByVal value As String)
                Me("UserLanguagePreference") = value
            End Set
        End Property

    End Class
End Namespace

Namespace My

    <Global.Microsoft.VisualBasic.HideModuleNameAttribute(), _
     Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), _
     Global.System.Runtime.CompilerServices.CompilerGeneratedAttribute()> _
    Friend Module MySettingsProperty

        <Global.System.ComponentModel.Design.HelpKeywordAttribute("My.Settings")> _
        Friend ReadOnly Property Settings() As Global.<namespace>.My.MySettings
                Return Global.<namespace>.My.MySettings.Default
            End Get
        End Property
    End Module
End Namespace

On selection change event of combobox add following code:

 Dim lang As String = ""
'*** refresh current form(say Mainform) with localized strings

Note: in combobox display member= Language Name (English)
Value Member = Language Code (en-US)

Add this method to refresh current form:

Private Sub LoadFormResources()
        Dim crm As ComponentResourceManager
        crm = New ComponentResourceManager(GetType(MainForm))

        Dim controlCollobj As New ControlsCollection(Me)

        For Each aControl As Control In controlCollobj.Controls
            crm.ApplyResources(aControl, aControl.Name)
        Next aControl
End Sub

Hope, It helps.

One comment

  1. Hello, came cacross this tutorial while trying to solve exactly the described problem. As I am beginner (I did only VBAprogramming before), I am not sure where to put the code sniplets to. Into which form? New Class?, I never came caross the “Namespace” instance. Where to put this?
    Additionaly, there is a part of the code missing in the first sniplet. It ends with:
    End Class
    End Namespace
    but it does not start with
    Which Definition Needs to be put before?
    Can anybody help me out of my Trouble?
    Thank you in andvance

Leave a Reply

Your email address will not be published. Required fields are marked *