Jul
7
2012

asp.Net Easily make your web application multilingual – Part 2

In my previous article, asp.Net Easily make your web application multilingual – Part 1, I showed you some first steps to prepare your webforms to accept multilingual content.  In this article, we’ll bring it all together and create a functioning, multilingual web app.

The Code Behind

Copy the below to your default.vb

Imports System.Threading
Imports System.Globalization

Public Class _default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim culture As String = String.Empty

        culture = "fr-CA"

        If Not Page.IsPostBack Then
            SetCulture(culture, culture)
            Localise_Controls()
        End If

    End Sub

    Protected Sub Localise_Controls()

        Label1.Text = GetLocalResourceObject("WelcomeText")
        Label1.ToolTip = GetLocalResourceObject("WelcomeText")

    End Sub

    Protected Sub SetCulture(ByVal name As String, ByVal locale As String)

        Thread.CurrentThread.CurrentUICulture = New CultureInfo(name)
        Thread.CurrentThread.CurrentCulture = New CultureInfo(locale)

        HttpContext.Current.Session("MyUICulture") = Thread.CurrentThread.CurrentUICulture
        HttpContext.Current.Session("MyCulture") = Thread.CurrentThread.CurrentCulture

    End Sub

End Class

Whats Happening

When default.aspx is loaded, the Page_Load runs and we declare the Culture variable to be passed onto our subs.  In the case above, it is Nothing.

I run the SetCulture sub routine which sets the threading and saves the Culture and UICulture in a session variable.  As is the goal of this website, I’m not going to explain the the details of this subroutine, suffice to say that this will always work.  If you need more details, there’s an excellent article (here).

Next, we call the Localise_Controls sub routine where we actually dictate which field to pull from the Resource file and set to our control, in this example a Label.  The server will pull from the Resource file who’s Culture suffix matches that of the current Culture.  If no culture is declared, the Default Resource file (no suffix) is used.

To test this, run the application.  The Page should load and the label should read ‘Welcome to my website ‘ with a Tooltip of ‘Welcome to my website ‘.

Now, let’s load the French content, un-comment (delete the apostrophe) the below line and re-run the application.

'culture = "fr-CA"

Success, a multilingual website – Now What?

At this point, you already have everything you need to offer a multilingual web application.

Please consider that for every page, you’ll need a matching Resource file for every language you wish to offer to your users.  If you attempt to point your culture variable to a Resource file with a suffix that it cannot find, ie. fr-FR, the server will load the Default Resource file.

Ideally, you’ll want to offer your users a mechanism to choose their preferred language, either by dropdown, radio buttons or other.  Another considering would be to save that Culture to a cookie on the users PC, that way they will always view your content in their preferred language without being prompted to choose.

In a future post, I’ll assemble an example that does all the above, offering a multilingual (localised) experience using cookies and a database connection.

I’ve attached a zip file that contains the above code in a working example, give it a try and let me know what you think.

Happy coding!

If you enjoyed this post, please consider leaving a comment, or sharing it by Tweeting or using the FaceBook Like button.

You may also Subscribe to this blog and receive notifications of new posts by email using the feature at the top of the page.

Please remember to visit my Sponsors (right column) as it helps me get the bills paid.

Easily make your web application multilingual
Easily make your web application multilingual
localisation_Tutorial.zip
Version: 1.0
76.3 KiB
242 Downloads
Details...

Comments

comments

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Visit my Sponsors

Read previous post:
asp.Net Logo
Slugify your content to make it SEO (Search Engine Optimised)

SEO - Who Cares? SEO, it's what Google thinks about when you're not around! Ever come across a webpage where...

Close