Add XML and ASP to "localize" webpages"

Source: Internet
Author: User
Tags html encode xml parser xpath code

Do you want your website to be viewed by surfers from multiple countries? Undoubtedly, this requires the provision of page content in multiple language versions, that is, the so-called
"Localization" function. The easiest way to think of this is to compile the page content in multiple languages, place it in different directories, and add navigation on the home page.
Link to the location of the language. Obviously, this processing method will waste a lot of space including public elements and design frameworks, and it will be annoying to modify and maintain them.
. Is there any good way? Of course, this article describes how to use XML and ASP technologies to achieve this purpose.
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 a language string into the HTML stream.
Prerequisites
The technology discussed in this article involves the following aspects: IIS4, PWS (including ASP functions), and MSXML version 3.
Function display
The routine to be discussed in this article is a simple HTML form for submitting name and address information. The following is how to display Spanish in Netscape Navigator:
Illustration:
Using the technology described in this article, you can simply update an XML file to add multiple languages to the site.
Create XML documents containing language resources
First, create a web page file named xmltrans. asp using your favorite page editor. I found that using static placeholder text (such as "first
Name ") the easiest way to design a page. After the XML file is ready, you can replace the static text with variables. To download the routine file, click here.
After completing the functional design of the basic page, you can create an XML document that contains a language string in good format. Here, I use a plain text editor
Notepad writes the initial XML Language Set-English version. Notepad makes me feel closer Source code . The XML file is named xmltrans. xml. Listed below
XML file containing English, French, and Spanish strings Code Content:
<? XML version = "1.0" encoding = "UTF-8"?>
<Ages>
<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>
</Ages>
The first line of the xmltrans. xml file is the XML declaration. The version attribute tells the reader that the document complies with the XML 1.0 standard and the encoding attribute indicates parsing.
Uses the compressed UNICODE:
<? XML version = "1.0" encoding = "UTF-8"?>
An XML document requires a root element containing other elements. Because the routine here contains some languages, the root element is named ages:
<Ages>
As a collection type, the ages element contains one or more language elements:
<Language XML: lang = "en" engname = "English" langname = "English" charset = "Windows-1252">
The preceding language tag contains four attributes. XML: Lang attributes are the most important of all. Program Search by the 2-letter language code of this attribute value
String group. Other attributes, such as charset, can be used for future function expansion.
In each language node, I add an element as a variable, which contains the text displayed on the HTML page. XML allows the use of meaningful names
Custom tag. For example, I use the <title> element to include the title of an 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 HTML page strings. The markup and English characters in the XML file are displayed.
String. Note: Only the English version is discussed here. You can add more languages later.

ASP file code analysis
Now let's take a look at the ASP file xmltrans. asp. Here, we use the XML Path Language (XPath), which can be found in Microsoft's XML Parser (MSXML ).
Run. You can think of xpath as a tool to get to the pre-location of some content in the XML document, which is similar to entering a path in the command line to execute the file,
For example, c: \ winnt \ notepad.exe, or enter a URL in the browser to access the page. Next we will study this ASP file to see how to retrieve English characters.
String. First, we instantiate an XML Parser working object called msxml2.domdocument:
Set Doc = server ._
Createobject _
("Msxml2.domdocument ")
Because ASP programs cannot process events like Win32 programs, asynchronous operation options are disabled here. This ensures that you are switching to other tasks.
Before, the script program will wait until the current event is completed:
Doc. async = false
Then, load the XML document containing the string using the load method of the domdocument object. If an error occurs during the reprinting process, the system issues a warning and stops
Stop running:
If not Doc. Load (server ._
Mappath ("xmltrans. xml") then
MSG = "failed to load"
MSG = MSG & "the XML file"
Response. Write msg
Response. End
End if
We can use two methods to query XML documents: XSL and XPath. Here let the parser use the latter and pass the attribute name and correct value
Setproperty method:
Doc. setproperty _
"Selectionlanguage", "XPath"
Then, create a path for selecting the content of the XML document. Obviously, the relevant string is located in a certain position of the ages element, so ages becomes
Path. At the same time, we know that a string is contained in a language element, but which one is it? Do not forget the preceding XML document
We embed an attribute named XML: Lang and provide a unique value "en ". This is what we want to choose. The following is the syntax for operating on it:
Sel = "/ages/language"
Sel = sel & "[@ XML: lang = 'en']"
This is not easy to understand, but you can think of this XPath code as an SQL statement, similar to the command to retrieve a record set:
Select * From ages. language where XML: lang = 'en'
Return to the actual XPath query. Use the following code to return the Node object containing the first matching node:
Set selectednode = _
Doc. selectsinglenode (SEL)
The last step is to pass the element name ("title") to the selectsinglenode method and request it to retrieve the text attribute value of the "title" node. That is to say, take
Text included in <title> and </title>. Here, the retrieved text is "localize ASP with XML ":
TXT = selectednode ._
Selectsinglenode ("title"). Text
Compared with SQL commands, it is like retrieving a field value from the ADO record set. The statement is:
TXT = RS ("title ")
Insert a language string to the ASP page
Once you know how to select text from the XML Element and assign it to the variable, you can insert the variable value into the HTML stream. To make the code concise, create
The getstring () function is as follows:
Function getstring (instring)
Temp = selectednode ._
Selectsinglenode _
(Instring). Text
Getstring = _
Server.html encode (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. For the sake of insurance, we use ASP's
The server.html encode method converts the text into valid HTML code. On the ASP page, the calling code is similar to the following:
<TD>
<% = Getstring ("firstname") %>:
</TD>
If the English part of the XML document is selected, the HTML output is as follows:
<TD> First name: V/TD>
If French is selected, the HTML output is as follows:
<TD> pr é Nom: </TD>
Display the form of the French language version:

Select available language
One of the major advantages of XML is that it is saved in clear text format, and we can update XML files on the web server at any time. In addition, we can
Open the xmltrans. xml file in the XML editor XML nothpad, copy it, and translate the text into a new language.
The routine file in XML notepad is displayed:

On the left side, you can add, delete, and select elements and attributes. You can edit relevant content on the right side. When the site requires a multi-language version
At this time, you only need to paste the content, and then upload the latest XML document.
To make the added language take effect immediately, add a program, 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 segment, You Can format the data to create an HTML list box. When a user submits a form, the language type code is 2 letters
Stored in the ASP session variable choselang.
<Select 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>
Add prompt information and Character Set Data
The above uses XML to provide variable-language text display of HTML forms, and then takes into account more extensive use. For example, add
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 browsers
Identify the language type of the HTML page and save the character set information to an XML file. For example:
<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 page for using the Japanese character set is displayed. Remember, to see the correct content, the operating system and the browser must support Unicode and install
Characters. Otherwise, some strange texts may be question marks or square symbols.

Summary
This article describes how to use an XML file to save Web page language strings. We learned how to use the ASP script program and use the Microsoft XML Parser and W3C
To query XML documents. ASP code inserts variable text that contains character set types into the HTML stream and forms a list box for users to select
Available languages. This article covers a lot of content, but I think this is just a starting point. Although these functions can be simply implemented in a browser that supports XML, we have
We can see that using server scripts can convert XML data into common HTML content. Even in earlier versions of browsers, we can also see these highlights.
.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.