ASPNL logo (1 kb)
Thursday, November 20, 2008

powered by


Microsoft ASP.NET Connections
Member of ASP Guild
<< previous | index | next >>
Supporting multiple languages with XML and ASP

Supporting multiple languages with XML and ASP

30 March 2001

Another article I wrote was about the use of XML in PHP to make it possible to work with multiple languages. This isn't just possible in PHP, but also in other languages, like ASP. In this article I will talk about the connection between XML and ASP. For this I took a very usefull example. Knowledge of ASP and XML is needed to understand this article.

On a normal website you can have different versions, one in Dutch, one in English, one in Spanish, etc. Usually this means you'll have ten pages with the same content, in different languages. What we are going to do here is create a standard-page (a template) with ASP. We'll create a dictionary (a kind of database) in XML. ASP calls XML and you'll have multiple languages on your site.

The XML-dictionairy

We're not going to make an extensive dictionairy, because it's only about understanding it. In this case we only have a title and a content in Dutch, English and Spanish:

<?xml version="1.0" encoding="UTF-8"?>
<talen>
   <taal xml:lang="nl" engname="Dutch" langname="Nederlands" charset="Windows-1252">
      <titel>Welkom op mijn homepage</titel>
      <inhoud>Dit is een homepage, waar ASP en XML gebruikt worden.</inhoud>
      <lang>nl</lang>
      <langname>Dutch</langname>
   </taal>
   <taal xml:lang="en" engname="English" langname="English" charset="Windows-1252">
      <titel>Welcome on my homepage</titel>
      <inhoud>This is a homepage, where ASP and XML is being used.</inhoud>
      <lang>en</lang>
      <langname>English</langname>
   </taal>
   <taal xml:lang="sp" engname="Spanish" langname="Espanol" charset="iso-8859-1">
      <titel>Recepcion en mi homepage</titel>
      <inhoud>Esto es un homepage, donde se esta utilizando el ASP y XML.</inhoud>
      <lang>sp</lang>
      <langname>Spanish</langname>
   </taal>
</talen>

This is obvious: you can see which language it concerns, the English name and the local name that has been given to it, along with the charset to be used. After that the title, content and the language are given. In this article this file is saved as "taal.xml".

The ASP-engine

Now ASP has to make a website out of it that can deliver three languages. This is very simple. Make contact with the XML-file, retrieve the data from it and build the website. Then follows the file. The explanation is given in the file itself as comment, so you can copy it. In this article this file is saved as "homepage.asp".

<%@ Language=VBScript %>
<%
Option Explicit
Dim scriptname, xmlfilename, doc, temp
Dim node, selectednode, optionnodelist, errobj
Dim chosenlang, selectednodes, i

'Checks to see which language is being used, if there isn't one, Dutch will be used
If Request.Form("chosenlang") <> "" Then
   Session("chosenlang") = Request.Form("chosenlang")
Else
   Session("chosenlang") = "nl"
End if

'Here the XML-file is given
xmlfilename = "taal.xml"

'Here an object is created that uses the XML
Set doc = Server.CreateObject("MSXML2.DOMDocument")

'Here is stated that ASP has to wait until XML is finished
doc.async = False

'Here XML is loaded, if not we'll get an error messagege
If Not Doc.Load(server.MapPath(xmlfilename)) Then
   Response.Write "Couldn't load the XML-file.<br>"
   Response.End
End If

'Here is stated we use Xpath
doc.setProperty "SelectionLanguage", "XPath"

'Here the right tags are chosen from the XML-file
Set selectednode = doc.selectSingleNode("/talen/taal[@xml:lang='" & Session("chosenlang") & "']")

'Here all the tags are chosen
Set selectednodes = doc.documentElement.selectNodes("/talen/taal")

Function getstring(instring)
   'This function makes sure the value of the tags will be in instring and is HTML-ready
   temp = selectednode._
   selectSingleNode _
   (instring).text
   getstring = _
   server.htmlencode(temp)
End Function
'Here the file is made in HTML
%>
<html>
<head>
<title>
<%=getstring("titel")%></title>
</head>
<body>
<h1><font face="Arial" size="7">
<%=getstring("titel")%></font></h1>
<form action="homepage.asp" method="post">
<p><font size="1" face="Arial">
Choose a language:
<select size="1" name="chosenlang">
<%
'Here the language choise is put together
'So you can extend the XML-file with languagues, without touching ASP
For i = 0 To selectednodes.length - 1
   Response.Write "<option value=" & _
   Chr(34) & _
   selectednodes.item(i)._
   selectSingleNode("lang").text & Chr(34)
   If (selectednodes.item(i)._
   selectSingleNode("lang").text = _
   Session("chosenlang")) Then
      Response.Write " selected>"
   Else
      Response.Write ">"
   End If
   Response.Write selectednodes.item(i)._
   selectSingleNode("langname").text & _
   "</option>" & vbCrLf
Next
%>
</select>
</font>
<input type=submit value="OK" name=submit></p>
<hr>
<p><font face="Arial"><%=getstring("content")%></font></p>
</body>
</html>

Read this file thoroughly and you'll get it.

<< previous | ^ to top | index | next >>
copyright 2000-2002 ASPNL