Click here to Skip to main content
15,885,032 members
Articles / Web Development / ASP.NET

An Easy Introduction to Localization in ASP.NET 2.0

Rate me:
Please Sign up or sign in to vote.
4.73/5 (18 votes)
16 Oct 20069 min read 165.7K   2.9K   91  
This article describes a quick and easy way to implement localization through the use of resource strings within an ASP.NET 2.0 application (Visual Basic 2005).
Imports System
Imports System.Globalization
Imports System.Threading
Imports System.Resources
Imports System.Reflection



Partial Class _Default
    Inherits System.Web.UI.Page

    Private rm As ResourceManager

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim ci As CultureInfo

        If Not Page.IsPostBack Then
            Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
            'get the culture info to set the language
            rm = New ResourceManager("Resources.Strings", System.Reflection.Assembly.Load("App_GlobalResources"))
            ci = Thread.CurrentThread.CurrentCulture
            LoadStrings(ci)
        Else
            'get the culture info to set the language
            rm = New ResourceManager("Resources.Strings", System.Reflection.Assembly.Load("App_GlobalResources"))
            ci = Thread.CurrentThread.CurrentCulture
            LoadStrings(ci)
        End If

    End Sub

    Private Sub LoadStrings(ByVal ci As CultureInfo)

        lbl1.Text = rm.GetString("BirdInfo", ci)
        lbl2.Text = rm.GetString("CatInfo", ci)
        lbl3.Text = rm.GetString("DogInfo", ci)
        lbl4.Text = rm.GetString("TravelInfo", ci)
        lbl5.Text = rm.GetString("WaterInfo", ci)
        lbl6.Text = rm.GetString("WeatherInfo", ci)

    End Sub


    Protected Sub ibtSpain_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibtSpain.Click
        Thread.CurrentThread.CurrentCulture = New CultureInfo("es-ES")
        LoadStrings(Thread.CurrentThread.CurrentCulture)
    End Sub


    Protected Sub ibtUSA_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibtUSA.Click
        Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
        LoadStrings(Thread.CurrentThread.CurrentCulture)
    End Sub


    Protected Sub ibtFrance_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibtFrance.Click
        Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-FR")
        LoadStrings(Thread.CurrentThread.CurrentCulture)
    End Sub


    Protected Sub ibtGermany_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibtGermany.Click
        Thread.CurrentThread.CurrentCulture = New CultureInfo("de-DE")
        LoadStrings(Thread.CurrentThread.CurrentCulture)
    End Sub
End Class


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions