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
If Request.Form("chosenlang") <> "" Then
Session("chosenlang") = Request.Form("chosenlang")
Else
Session("chosenlang") = "nl"
End if
xmlfilename = "taal.xml"
Set doc = Server.CreateObject("MSXML2.DOMDocument")
doc.async = False
If Not Doc.Load(server.MapPath(xmlfilename)) Then
Response.Write "Couldn't load the XML-file.<br>"
Response.End
End If
doc.setProperty "SelectionLanguage", "XPath"
Set selectednode = doc.selectSingleNode("/talen/taal[@xml:lang='" & Session("chosenlang") & "']")
Set selectednodes = doc.documentElement.selectNodes("/talen/taal")
Function getstring(instring)
temp = selectednode._
selectSingleNode _
(instring).text
getstring = _
server.htmlencode(temp)
End Function
%>
<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">
<%
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.
|