asp.net | Using Multilanguage site

I prefer this method for 2 reasons, 1: I don’t need to create each page twice, 2: I don’t know any other method lol
PS: it’s all in VB


Step 0: Create BasePage.vb

Create BasePage.vb file in App_Codefolder

Imports Microsoft.VisualBasic
Imports System.Threading
Imports System.Globalization
Public Class BasePage
    Inherits System.Web.UI.Page
    Protected Overrides Sub InitializeCulture()
        If Session("MyCulture") = "" Then Session("MyCulture") = "ar"
        'Set the UICulture and the Culture with a value stored in a Session-object. I called mine “MyCulture"
        Thread.CurrentThread.CurrentUICulture = New CultureInfo(Session("MyCulture").ToString)
        ' **** Comment this line if you DO NOT want to use Hijri calander
        ' **** If you enabled this line you will use Hijri calander if you switch to Arabic
        'Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Session("MyCulture").ToString)
    End Sub
End Class

Step 1: Create Global.asax file

Add the following in Global.asax

<%@ Application Language="VB" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Threading" %>
<script runat="server">
    Private Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application startup                                                            
        Dim cookie As HttpCookie = HttpContext.Current.Request.Cookies("CultureInfo")
        If ((Not (cookie) Is Nothing) _
                    AndAlso (Not (cookie.Value) Is Nothing)) Then
            System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo(cookie.Value)
            System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo(cookie.Value)
        Else
            System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo("en")
            System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("en")
        End If
    End Sub
</script>

Step 2-1: Add the designated code to each page code behind (single page)

Add the following in the page you want to enable multi-language

Imports System.Globalization
Imports System.Threading
Imports System.Resources
Partial Class page_name_gose_here
    ' Inherits System.Web.UI.Page
    Inherits BasePage
    Private rm As ResourceManager

End Class
The code above is for a page dosen't parented to master page

Step 2-2: Add the designated code to master page

Imports System.Globalization
Imports System.Threading
Imports System.Resources
Partial Class master_page_name
    Inherits System.Web.UI.MasterPage

    Private rm As ResourceManager

End Class

And the following to the page

Imports System.Globalization
Imports System.Threading
Imports System.Resources
Partial Class page_name_goes_here
    'Inherits System.Web.UI.Page <==== remove this one
    Inherits BasePage
    Private rm As ResourceManager
End Class

Step 3: Button to switch language

On the button click event add the following

Select Case Session("MyCulture")

            Case "ar"
                Session("MyCulture") = "en"
                Dim aCookie As New HttpCookie("otmData")
                aCookie.Values("languagePref") = Session("MyCulture")
                aCookie.Expires = System.DateTime.Now.AddDays(21)
                Response.Cookies.Add(aCookie)
                Response.Redirect(Request.Url.ToString)
            Case "en"
                Session("MyCulture") = "ar"
                Dim aCookie As New HttpCookie("otmData")
                aCookie.Values("languagePref") = Session("MyCulture")
                aCookie.Expires = System.DateTime.Now.AddDays(21)
                Response.Cookies.Add(aCookie)
                Response.Redirect(Request.Url.ToString)
            Case Else
                Session("MyCulture") = "ar"
                Dim aCookie As New HttpCookie("otmData")
                aCookie.Values("languagePref") = Session("MyCulture")
                aCookie.Expires = System.DateTime.Now.AddDays(21)
                Response.Cookies.Add(aCookie)
                Response.Redirect(Request.Url.ToString)

        End Select

Step 4: Create a resource file

After finishing designing your page, in visual studio view the page in “Design mode”, go to “Tools” menu and click on “General Local Resource”

You will notice in the project explorer there are new files created, duplicate it and add in the end of its name “.en.” then you can take it from there by writing your translations.

You will notice each asp element has new attribute on it: meta:resourcekey=”element_name_Resource1″


Step 5: To prevent using other culture date formats

Add the following code to the master page (on page load)

System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat = New CultureInfo("en-GB").DateTimeFormat

Leave a Reply

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