Want your site to be seen by surfers from multiple countries? Undoubtedly, this requires providing page content in multiple languages, which is the so-called
"Localization" function. The easiest way to think of is to write page content in multiple languages separately, then place it in different directories, and then add navigation on the homepage
Links to their respective language locations. Obviously, such a way of processing will waste a lot of space that contains common elements and design frameworks, and modification and maintenance are also annoying.
petty. Is there any good way? Of course, this article introduces how to use XML and ASP technology to achieve this goal.
This article will discuss the following aspects: creating XML documents containing language resources, creating web page templates using ASP, using XPath syntax and MSXML3
The analyzer locates the target content in the XML document and dynamically inserts language strings into the HTML stream.
Preparation conditions
The technologies discussed in this article involve the following aspects: IIS4 or PWS (including ASP functions), MSXML version 3.
Function display
The routine to be discussed in this article is a simple HTML form to submit name and address information. Below is the display of Spanish in Netscape Navigator
Illustration of words:
Using the techniques discussed in this article, you can add multiple languages to your site by updating an XML file.
Create an XML document containing language resources
First, use your favorite page editor to create a web page file called. I found using static placeholder literals (such as "First
name ") Designing a page is easiest. When the XML is ready, you can replace these static text with variables. To download the routine file, click here.
After completing the functional design of the basic page, start creating a well-format XML document containing language strings. Here I use a plain text editor
Notepad writes the initial XML language set-English version. Notepad makes me feel closer to the source code. The XML file is named. Listed below
XML file code content containing English, French and Spanish strings:
<?xml version="1.0" encoding="UTF-8"?>
<languages>
<language xml:lang="en" engname="English" langname="English" charset="Windows-1252">
<title>Localize ASP with XML</title>
<firstname>First name</firstname>
<lastname>Last name</lastname>
<prefix>Prefix</prefix>
<suffix>Suffix</suffix>
<address1>Address</address1>
<address2>Address 2</address2>
<address3>Address 3</address3>
<city>City</city>
<region>State/Province</region>
<postalcode>Postal code</postalcode>
<areacode>Area code</areacode>
<telephone>Telephone number</telephone>
<submit>OK</submit>
<lang>en</lang>
<charset>Windows-1252</charset>
<langname>English</langname>
</language>
<language xml:lang="fr" engname="French" langname="franζais" charset="Windows-1252">
<title>Localize ASP with XML - French</title>
<firstname>Prénom</firstname>
<lastname>Nom</lastname>
<prefix>Préfixe (M., Mme, Mlle)</prefix>
<suffix>Suffixe/Titre</suffix>
<address1>Rue</address1>
<address2/>
<address3/>
<city>Ville</city>
<region>Région/Province</region>
<postalcode>Code postal</postalcode>
<areacode>Indicatif régional</areacode>
<telephone>Numéro de téléphone</telephone>
<submit>OK</submit>
<lang>fr</lang>
<charset>Windows-1252</charset>
<langname>French</langname>
</language>
<language xml:lang="sp" engname="Spanish" langname="Espanol" charset="Windows-1252">
<title>Localize ASP with XML - Spanish</title>
<firstname>Nombre</firstname>
<lastname>Apellido paterno</lastname>
<prefix>Prefijo (Sr., Sra., Srta.)</prefix>
<suffix>Sufijo o título</suffix>
<address1>Línea 1 de dirección</address1>
<address2/>
<address3/>
<city>Ciudad</city>
<region>Región, estado o provincia</region>
<postalcode>Código postal</postalcode>
<areacode>Código de área</areacode>
<telephone>Número de teléfono </telephone>
<submit>OK</submit>
<lang>sp</lang>
<charset>iso-8859-1</charset>
<langname>Spanish</langname>
</language>
</languages>
The first line of the file is the XML declaration. The version attribute tells readers that the document complies with the XML 1.0 standard, and the encoding attribute indicates parsing
The compressed version of Unicode is used:
<?xml version="1.0" encoding="UTF-8"?>
An XML document requires a root element that contains other elements. Because the routine here contains some languages, the root element is named languages:
<languages>
As a collection type, the languages element contains one or more language elements:
<language xml:lang="en" engname="English" langname="English" charset="Windows-1252">
The above language tag contains 4 attributes. xml:lang attribute is the most important one. In the ASP program, you will search through the 2-letter language code of this attribute value.
String group. Other attributes, such as charset, can be used for future functional expansion.
In each language node, I added an element as a variable that contains the text displayed on the HTML page. XML allows the use of meaningful names as
Custom tags, for example, I use the <title> element to contain the title of the HTML page. Similarly, the <firstname> element contains the string "First name".
<title>Localize ASP with XML</title>
<firstname>First name</firstname>
Finally, a well-formatted XML document contains a complete set of strings corresponding to HTML pages. The following figure shows tags and English language characters in XML files
string. Note that only the English version is discussed here, and more languages can be added in the future.
ASP file code analysis
Let's take a look at the ASP files now. In it we use XML Path Language (XPath), which can be found in Microsoft's XML parser (MSXML)
implement. You can think of XPath as a tool to reach a predetermined location for part of the XML document, which is somewhat similar to entering a path to execute a file in the command line.
For example, c:\winnt\, or a URL access page is typed into the browser. Let's study this ASP file to see how to retrieve English language words
String. First, we instantiate an XML parser working object called:
set doc=server._
CreateObject _
("")
Since ASP programs cannot handle events like Win32 programs, we turn off the asynchronous operation option here. This ensures that you are turning to other tasks
Before, the script program will wait for the completion of the current event:
= False
Then use the Load method of the DOMDocument object to load the XML document containing the string. If an error occurs during the reprinting process, a warning message will be issued and stopped
Stop running:
if not (server._
mappath("")) then
msg="Failed to load "
msg=msg & "the XML file"
msg
end if
We can query XML documents using two methods: XSL and XPath. Let the parser use the latter here, passing the attribute name and correct value to
setProperty method:
_
"SelectionLanguage", "XPath"
Then, create a path to select part of the XML document. Obviously, the relevant string is located somewhere in the languages element, so languages become
The beginning part of the path. At the same time, we know that strings are contained in a language element, but which one is it? Please don't forget the XML document in the previous
In, we embed a property called xml:lang and give a unique value "en". This is what we want to choose. Here is the syntax for operating it:
sel="/languages/language"
sel=sel & "[@xml:lang='en']"
This is a bit difficult to understand, but you can imagine this XPath code as an SQL statement, similar to the command to retrieve a record set:
SELECT * FROM WHERE xml:lang='en'
Going back to the actual XPath query, use the following code to return the node object containing the first matching node:
set selectednode = _
(sel)
The last step is to pass the element name ("title") to the selectSingleNode method and ask it to retrieve the text attribute value of the "title" node. That is to say, take
Must contain text in <title> and</title>. Here, the retrieved text is "Localize ASP with XML":
txt=selectednode._
selectSingleNode("title").text
Comparing with SQL commands, it is like retrieving a field value from within an ADO record set, the statement is:
txt=rs("title")
Insert language strings in ASP page
Once you know how to select text from XML elements and assign values to variables, you can insert the variable value into the HTML stream. To make the code concise, create a
The function of getstring() is as follows:
function getstring(instring)
temp=selectednode._
selectSingleNode _
(instring).text
getstring= _
(temp)
end function
The input value of the getstring() function is the element name, and the element value must be retrieved from the XML document. For example, pass "firstname" to getstring(),
The getstring function selects the firstname element and returns its text value. To be on the safe side, we use the ASP's
Methods convert text to legitimate HTML code. In the ASP page, the call code is similar to the following:
<td>
<%=getstring("firstname")%>:
</td>
If the English part of the XML document is selected, the HTML output result is as follows:
<td>First name:v/td>
If French is selected, the HTML output result is as follows:
<td>Prénom:</td>
The following figure shows the French version form:
Select the available language
One of the big advantages of XML is that it is saved in a clear text format, and we can update XML files on the web server at any time. And, we can
Open the English version of the file in XML NotHPad, copy a copy, and translate the text into a new language. The following picture
The routine file in XML Notepad is displayed:
In the above figure, you can perform operations such as adding, deleting and selecting elements and attributes on the left, and you can edit relevant content on the right. When the site requires multiple languages
In this case, you only need to perform the paste operation in the content and then upload the latest XML document.
In order for the added language to take effect immediately, add a program to use it to determine the number of different languages in the file, and return the language code and language name.
As shown in the following code snippet, we can format the data to create an HTML list box. When a user submits a form, the language type code represented by 2 letters
Stored in the ASP session variable chosenang.
<select name="chosenlang">
<%
for i=0 to - 1
"<option value=" & _
chr(34) & _
(i)._
selectSingleNode("lang").text & chr(34)
if ((i)._
selectSingleNode("lang").text = _
session("chosenlang")) then
" selected>"
else
">"
end if
(i)._
selectSingleNode("langname").text & _
"</option>" & vbcrlf
next
%></select>
Add prompt information and character set data
The above uses XML to provide variable language and text display of HTML forms, and then some more rich uses should be considered. For example, add a
For prompt information, you only need to embed the HTML tag <LABEL> before and after the text, and provide variable content from the XML as the title attribute value. Similarly, to help the browser
Identify the language type of HTML page and save character set information into XML files, such as:
<charset>x-sjis</charset>
Then, you can use ASP to insert the character set type into the HTML stream:
<meta HTTP-EQUIV="Content-Type" content="text/html; charset=
<%=getstring("charset")%>" />
The final HTML code includes the Japanese character set reference:
<meta HTTP-EQUIV="Content-Type" content="text/html; charset=x-sjis" />
The following image shows the page using the Japanese character set. Remember, to see the correct content, the operating system and browser must support Unicode and have the
Require characters. Otherwise, you will see some strange text, maybe some question marks, maybe some square symbols, etc.
Summarize
This article describes how to use XML files to save language strings for web pages. From this we learned to use ASP scripting programs and use Microsoft XML parser and W3C
The XPath language implements the function of querying XML documents. ASP code inserts variable literals containing character set types into HTML streams and forms a list box for users to choose
Language available. This article covers a lot, but I guess it's just a starting point. While these functions can be simply implemented in XML-enabled browsers, we have
It has been seen that using server-side scripts can convert XML data into ordinary HTML content, and even earlier versions of browsers can see these wonderful contents.
Allow.
"Localization" function. The easiest way to think of is to write page content in multiple languages separately, then place it in different directories, and then add navigation on the homepage
Links to their respective language locations. Obviously, such a way of processing will waste a lot of space that contains common elements and design frameworks, and modification and maintenance are also annoying.
petty. Is there any good way? Of course, this article introduces how to use XML and ASP technology to achieve this goal.
This article will discuss the following aspects: creating XML documents containing language resources, creating web page templates using ASP, using XPath syntax and MSXML3
The analyzer locates the target content in the XML document and dynamically inserts language strings into the HTML stream.
Preparation conditions
The technologies discussed in this article involve the following aspects: IIS4 or PWS (including ASP functions), MSXML version 3.
Function display
The routine to be discussed in this article is a simple HTML form to submit name and address information. Below is the display of Spanish in Netscape Navigator
Illustration of words:
Using the techniques discussed in this article, you can add multiple languages to your site by updating an XML file.
Create an XML document containing language resources
First, use your favorite page editor to create a web page file called. I found using static placeholder literals (such as "First
name ") Designing a page is easiest. When the XML is ready, you can replace these static text with variables. To download the routine file, click here.
After completing the functional design of the basic page, start creating a well-format XML document containing language strings. Here I use a plain text editor
Notepad writes the initial XML language set-English version. Notepad makes me feel closer to the source code. The XML file is named. Listed below
XML file code content containing English, French and Spanish strings:
<?xml version="1.0" encoding="UTF-8"?>
<languages>
<language xml:lang="en" engname="English" langname="English" charset="Windows-1252">
<title>Localize ASP with XML</title>
<firstname>First name</firstname>
<lastname>Last name</lastname>
<prefix>Prefix</prefix>
<suffix>Suffix</suffix>
<address1>Address</address1>
<address2>Address 2</address2>
<address3>Address 3</address3>
<city>City</city>
<region>State/Province</region>
<postalcode>Postal code</postalcode>
<areacode>Area code</areacode>
<telephone>Telephone number</telephone>
<submit>OK</submit>
<lang>en</lang>
<charset>Windows-1252</charset>
<langname>English</langname>
</language>
<language xml:lang="fr" engname="French" langname="franζais" charset="Windows-1252">
<title>Localize ASP with XML - French</title>
<firstname>Prénom</firstname>
<lastname>Nom</lastname>
<prefix>Préfixe (M., Mme, Mlle)</prefix>
<suffix>Suffixe/Titre</suffix>
<address1>Rue</address1>
<address2/>
<address3/>
<city>Ville</city>
<region>Région/Province</region>
<postalcode>Code postal</postalcode>
<areacode>Indicatif régional</areacode>
<telephone>Numéro de téléphone</telephone>
<submit>OK</submit>
<lang>fr</lang>
<charset>Windows-1252</charset>
<langname>French</langname>
</language>
<language xml:lang="sp" engname="Spanish" langname="Espanol" charset="Windows-1252">
<title>Localize ASP with XML - Spanish</title>
<firstname>Nombre</firstname>
<lastname>Apellido paterno</lastname>
<prefix>Prefijo (Sr., Sra., Srta.)</prefix>
<suffix>Sufijo o título</suffix>
<address1>Línea 1 de dirección</address1>
<address2/>
<address3/>
<city>Ciudad</city>
<region>Región, estado o provincia</region>
<postalcode>Código postal</postalcode>
<areacode>Código de área</areacode>
<telephone>Número de teléfono </telephone>
<submit>OK</submit>
<lang>sp</lang>
<charset>iso-8859-1</charset>
<langname>Spanish</langname>
</language>
</languages>
The first line of the file is the XML declaration. The version attribute tells readers that the document complies with the XML 1.0 standard, and the encoding attribute indicates parsing
The compressed version of Unicode is used:
<?xml version="1.0" encoding="UTF-8"?>
An XML document requires a root element that contains other elements. Because the routine here contains some languages, the root element is named languages:
<languages>
As a collection type, the languages element contains one or more language elements:
<language xml:lang="en" engname="English" langname="English" charset="Windows-1252">
The above language tag contains 4 attributes. xml:lang attribute is the most important one. In the ASP program, you will search through the 2-letter language code of this attribute value.
String group. Other attributes, such as charset, can be used for future functional expansion.
In each language node, I added an element as a variable that contains the text displayed on the HTML page. XML allows the use of meaningful names as
Custom tags, for example, I use the <title> element to contain the title of the HTML page. Similarly, the <firstname> element contains the string "First name".
<title>Localize ASP with XML</title>
<firstname>First name</firstname>
Finally, a well-formatted XML document contains a complete set of strings corresponding to HTML pages. The following figure shows tags and English language characters in XML files
string. Note that only the English version is discussed here, and more languages can be added in the future.
ASP file code analysis
Let's take a look at the ASP files now. In it we use XML Path Language (XPath), which can be found in Microsoft's XML parser (MSXML)
implement. You can think of XPath as a tool to reach a predetermined location for part of the XML document, which is somewhat similar to entering a path to execute a file in the command line.
For example, c:\winnt\, or a URL access page is typed into the browser. Let's study this ASP file to see how to retrieve English language words
String. First, we instantiate an XML parser working object called:
set doc=server._
CreateObject _
("")
Since ASP programs cannot handle events like Win32 programs, we turn off the asynchronous operation option here. This ensures that you are turning to other tasks
Before, the script program will wait for the completion of the current event:
= False
Then use the Load method of the DOMDocument object to load the XML document containing the string. If an error occurs during the reprinting process, a warning message will be issued and stopped
Stop running:
if not (server._
mappath("")) then
msg="Failed to load "
msg=msg & "the XML file"
msg
end if
We can query XML documents using two methods: XSL and XPath. Let the parser use the latter here, passing the attribute name and correct value to
setProperty method:
_
"SelectionLanguage", "XPath"
Then, create a path to select part of the XML document. Obviously, the relevant string is located somewhere in the languages element, so languages become
The beginning part of the path. At the same time, we know that strings are contained in a language element, but which one is it? Please don't forget the XML document in the previous
In, we embed a property called xml:lang and give a unique value "en". This is what we want to choose. Here is the syntax for operating it:
sel="/languages/language"
sel=sel & "[@xml:lang='en']"
This is a bit difficult to understand, but you can imagine this XPath code as an SQL statement, similar to the command to retrieve a record set:
SELECT * FROM WHERE xml:lang='en'
Going back to the actual XPath query, use the following code to return the node object containing the first matching node:
set selectednode = _
(sel)
The last step is to pass the element name ("title") to the selectSingleNode method and ask it to retrieve the text attribute value of the "title" node. That is to say, take
Must contain text in <title> and</title>. Here, the retrieved text is "Localize ASP with XML":
txt=selectednode._
selectSingleNode("title").text
Comparing with SQL commands, it is like retrieving a field value from within an ADO record set, the statement is:
txt=rs("title")
Insert language strings in ASP page
Once you know how to select text from XML elements and assign values to variables, you can insert the variable value into the HTML stream. To make the code concise, create a
The function of getstring() is as follows:
function getstring(instring)
temp=selectednode._
selectSingleNode _
(instring).text
getstring= _
(temp)
end function
The input value of the getstring() function is the element name, and the element value must be retrieved from the XML document. For example, pass "firstname" to getstring(),
The getstring function selects the firstname element and returns its text value. To be on the safe side, we use the ASP's
Methods convert text to legitimate HTML code. In the ASP page, the call code is similar to the following:
<td>
<%=getstring("firstname")%>:
</td>
If the English part of the XML document is selected, the HTML output result is as follows:
<td>First name:v/td>
If French is selected, the HTML output result is as follows:
<td>Prénom:</td>
The following figure shows the French version form:
Select the available language
One of the big advantages of XML is that it is saved in a clear text format, and we can update XML files on the web server at any time. And, we can
Open the English version of the file in XML NotHPad, copy a copy, and translate the text into a new language. The following picture
The routine file in XML Notepad is displayed:
In the above figure, you can perform operations such as adding, deleting and selecting elements and attributes on the left, and you can edit relevant content on the right. When the site requires multiple languages
In this case, you only need to perform the paste operation in the content and then upload the latest XML document.
In order for the added language to take effect immediately, add a program to use it to determine the number of different languages in the file, and return the language code and language name.
As shown in the following code snippet, we can format the data to create an HTML list box. When a user submits a form, the language type code represented by 2 letters
Stored in the ASP session variable chosenang.
<select name="chosenlang">
<%
for i=0 to - 1
"<option value=" & _
chr(34) & _
(i)._
selectSingleNode("lang").text & chr(34)
if ((i)._
selectSingleNode("lang").text = _
session("chosenlang")) then
" selected>"
else
">"
end if
(i)._
selectSingleNode("langname").text & _
"</option>" & vbcrlf
next
%></select>
Add prompt information and character set data
The above uses XML to provide variable language and text display of HTML forms, and then some more rich uses should be considered. For example, add a
For prompt information, you only need to embed the HTML tag <LABEL> before and after the text, and provide variable content from the XML as the title attribute value. Similarly, to help the browser
Identify the language type of HTML page and save character set information into XML files, such as:
<charset>x-sjis</charset>
Then, you can use ASP to insert the character set type into the HTML stream:
<meta HTTP-EQUIV="Content-Type" content="text/html; charset=
<%=getstring("charset")%>" />
The final HTML code includes the Japanese character set reference:
<meta HTTP-EQUIV="Content-Type" content="text/html; charset=x-sjis" />
The following image shows the page using the Japanese character set. Remember, to see the correct content, the operating system and browser must support Unicode and have the
Require characters. Otherwise, you will see some strange text, maybe some question marks, maybe some square symbols, etc.
Summarize
This article describes how to use XML files to save language strings for web pages. From this we learned to use ASP scripting programs and use Microsoft XML parser and W3C
The XPath language implements the function of querying XML documents. ASP code inserts variable literals containing character set types into HTML streams and forms a list box for users to choose
Language available. This article covers a lot, but I guess it's just a starting point. While these functions can be simply implemented in XML-enabled browsers, we have
It has been seen that using server-side scripts can convert XML data into ordinary HTML content, and even earlier versions of browsers can see these wonderful contents.
Allow.