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!
Comments
Related Posts
Visit my Sponsors
Recent Comments
- Using the Ajax Modal Popup as a Login Control - A Beginner`s Guide - The Everyman Programmer The Everyman Programmer on Using the Ajax Modal Popup as a Login Control – A Beginner`s Guide – Part 2
- Using the Ajax Modal Popup as a Login Control – A Beginner`s Guide - Part 2 - The Everyman Programmer The Everyman Programmer on Passing Parameters to a QueryString – The right way to do it, Encryption
- Using the Ajax Modal Popup as a Login Control – A Beginner`s Guide - Part 2 - The Everyman Programmer The Everyman Programmer on Re-Usable Database Connection Class – A Beginner’s Tutorial Part 1
- Passing Parameters to a QueryString - The right way to do it, Encryption - The Everyman Programmer The Everyman Programmer on asp.Net 4.0 Routing, a simple Tutorial that works
- Nested Repeaters, do it clean and simple – A Beginner's Tutorial – Part 1 - The Everyman Programmer The Everyman Programmer on Re-Usable Database Connection Class – A Beginner’s Tutorial Part 1






