by Steve NuttMicrosoft Visual Studio and ASP.net provide us developers with many ways to write multilingual web interfaces. Here I will explain how you can load languages from database tables, with each table containing words or phrases used in the web application.
The technique is to check the user's language setting at log on, connect to the database and return a dataset containing an English column and a "local" language column with the relevant translation. Transfer the data from the dataset into a hashtable and place the hashtable in the application cache for very fast access. A small section of the Spanish language table named tblES might be as follows:
user name - Nombre Usuario
password - Contraseña
login check failed - El Cheque De la Conexión Falló
created new record - Nuevo agregado
The first thing we need to do is find out which language the user requires. This can be as simple as using something like Request.UserLanguages to obtain the information from the user's browser, or you could store the language setting in the users database record and check it during the login process.
Either way, the end result is that you will probably end up with a two digit string like EN, ES or TH. For our example, we will store the string "ES" in Session("Language") - meaning the user wants to view the user interface in Spanish.
Here is the code to load the relevant language database table into a dataset and then traverse it to put each row into a hashtable. As this is not a tutorial about databases, we will assume that you have a class called DataAccess and a method within the class called ExecuteStringReturnDS to do the necessary work.
Public Function GetHashtable(ByVal Language As String) As Hashtable
Try
Dim objDataAccess As DataAccess.DataAccess
Dim objDataSet As DataSet
objDataSet = objDataAccess.ExecuteStringReturnDS("select * from tbl" & Left(Language, 2))
If Not objDataSet Is Nothing Then
Dim hashLanguage As New Hashtable
Dim CurrentTable As DataTable
Dim CurrentRow As DataRow
Dim CurrentColumn As DataColumn
Dim intFlag As Integer = 0
Dim strKeyValue As String = ""
'Traverse the dataset and put each row into the hashtable
For Each CurrentTable In objDataSet.Tables
For Each CurrentRow In CurrentTable.Rows
For Each CurrentColumn In CurrentTable.Columns
If Not (CurrentRow(CurrentColumn) Is Nothing) Then
If Not IsDBNull(CurrentRow(CurrentColumn)) Then
If intFlag = 0 Then
strKeyValue = CStr(CurrentRow(CurrentColumn))
intFlag = 1
Else
hashLanguage(strKeyValue) = CStr(CurrentRow(CurrentColumn))
intFlag = 0
End If
End If
End If
Next
Next
Next
'Return the hashtable to the UI
Return hashLanguage
'Destroy our objects
objDataSet = Nothing
hashLanguage = Nothing
CurrentTable = Nothing
CurrentRow = Nothing
CurrentColumn = Nothing
End If
Catch objException As Exception
'Handle Exception
End Try
End Function
In the following code block, you will see that the application supports 3 languages - English, Spanish and Thai and we use a SELECT statement to check the user's language setting before loading the relevant language into the Cache.
Try
Select Case UCase(Session("Language"))
Case "ES"
If Cache("es") Is Nothing Then 'Spanish language is not in Cache
Dim hashES As New Hashtable
hashES = GetHashtable("es")
If Not hashES Is Nothing Then
Cache.Insert("es", hashES, _
Nothing, _
Cache.NoAbsoluteExpiration, _
Cache.NoSlidingExpiration, _
Caching.CacheItemPriority.NotRemovable, _
Nothing)
End If
hashES = Nothing
End If
Case "TH"
If Cache("th") Is Nothing Then 'Thai language is not in Cache
Dim hashTH As New Hashtable
hashTH = GetHashtable("th")
If Not hashTH Is Nothing Then
Cache.Insert("th", hashTH, _
Nothing, _
Cache.NoAbsoluteExpiration, _
Cache.NoSlidingExpiration, _
Caching.CacheItemPriority.NotRemovable, _
Nothing)
End If
hashTH = Nothing
End If
Case Else
If Cache("en") Is Nothing Then 'English language is not in Cache
Dim hashEN As New Hashtable
hashEN = GetHashtable("en")
If Not hashEN Is Nothing Then
Cache.Insert("en", hashEN, _
Nothing, _
Cache.NoAbsoluteExpiration, _
Cache.NoSlidingExpiration, _
Caching.CacheItemPriority.NotRemovable, _
Nothing)
End If
hashEN = Nothing
End If
End Select
Catch ex As Exception
'Handle Exception
End Try
In our example where Session("Language") = "ES", all rows would be extracted from the tblES table and returned to our application as a dataset. That dataset would be loaded into a hashtable which in turn would be placed in the Cache. Here is the code our application would use to access a word or phrase from the Cache to populate a lable named lblMessage:
lblMessage.Text = Cache(Session("Language"))("created new record")
About the AuthorSteve Nutt is the founder of
Connelly Security and
Initial Monitoring companies that offer monitoring of security, fire and medical alarms.
.
Read more...