(Author: hear the wind compile, 2001 at 14:42 on February 22)
Want to make your site understand the surfers from multiple countries? Undoubtedly, this needs to provide a plurality of language versions of the page, that is, the so-called "localization" function. The easiest way to prepare a multilingual page content, then place it to different directories, add a link to the respective language location on the homepage. Obviously, such processing methods will waste a lot of space containing public elements and design frames, and modify, and is very cumbersome. Is there a good way? Of course, this article describes how to combine this to use XML and ASP technology. This article will discuss the following aspects: Create an XML document containing language resources, create a web page template using the ASP, use the XPath syntax and MSXML3 analyzer to locate the target content in the XML document and insert language string in the HTML stream. . Preparation Conditions This article discussed herein relates to the following: IIS4 or PWS (including ASP function), MSXML version 3. Function Shows the routine to be discussed in this article that is a simple HTML form to submit names and address information. Below is a map of Spanish language in Netscape Navigator: Use the technology discusses this article, you can easily add a variety of languages to the site very simply by updating an XML file. Create an XML document containing language resources First, create a web page file called XmlTrans.asp using your favorite page editor. I found that the use of static placeholder text (such as the "First Name") design page is easier. When XML is ready, you can replace these static texts using variables. To download the routine file, please click here. After completing the functional design of the basic page, start creating a good format XML document with a language string. Here, I use the plain text editor Notepad to write the initial XML language set - English version. Notepad makes me feel closer to source code. The XML file is named XMLTrans.xml.
The contents of XML file codes containing English, French and spanish strings are listed: XML Version = "1.0" encoding = "utf-8"?> localize asp with xml title> first name firstname> Last Name lastname> prefix Prefix> Suffix SUFFIX> address address1> address 2 address2> address 3 address3> city city> State / Province region> Postal code postalcode> Area code area> Telephone Number telephone> OK submit> en lang> Windows-1252 charset> English langname> language> localize Asp with XML - French title> prénom firstname> NOM lastname> préfixe (M., MME, MLLE) prefix> Suffixe / Titre suffix> rue address1> Ville city> Région / province region> code postal Postalcode> Indicatif Régional area>
Numéro de téléphone telephone> OK submit> fr lang> Windows-1252 charset> French langname> logage> localize asp with xml - spanish title> Nombre firstname> APELLIDO PATERNO lastname> Prefijo (Sr., Sra., Srta.) prefix> Sufijo o título suffix> Línea 1 de dirección address1> < city> Ciudad city> Región, estado o provincia region> Código postal postalcode> Código de área areacode> Número de teléfono telephone> OK Submit> sp lang> ISO-8859-1 charset> spanish layname> language> language> XmlTrans.xml file first line It is an XML declaration. The version of Version property tells the reader document that meets the standard of XML 1.0. Encoding property indicates the resolver uses the Unicode: XML Version = "1.0" encoding = "utf-8"?> An XML document requires a contained other elements. Root elements. Because the routines here include some languages, named the root element for languages: as a collection type, the languages element contains one or more language elements: The above Language tag contains 4 properties. XML: LANG attribute is the most important in which a string group will be searched in a 2 alphabet code code of this attribute value in an ASP program.
Other properties, such as Charset, can be used for future features. In each language node, I added an element as a variable, which contains text displayed on the HTML page. XML allows the use of meaningful names as a custom tag, for example, I use the
element to contain the title of the HTML page. Similarly, the element contains a string "first name". localize asp with xml title> first name firstname> Final, a good format XML document contains a set of strings corresponding to the HTML page. The following figure shows the tags and English language strings in the XML file. Note, now only the English version is discussed here, and more languages can be added later. ASP file code analysis is now come back to see the ASP file XmlTrans.asp. In these XML Path Language (XPath), it can be executed in Microsoft's XML parser (MSXML). You can think XPath is a tool that arrives in a predetermined location of the XML document, which is similar to entering a path in the command line to perform files, such as c: /winnt/notepad.exe, or knocking a URL in the browser. Access page. Below to study this ASP file to see how to retrieve the ENGLISH language string. First, we instantiate a XML parser called MSXml2.DomDocument: set doc = server._ createObject _ ("msxml2.domdocument") Since the ASP program cannot handle events like Win32 programs, here we turn off asynchronous asynchronous Operation options. This ensures that the script will wait for the current event before turning to other tasks: doc.async = false then loads the XML document containing the string using the DomDocument object's LOAD method. If an error occurs during the reprint, a warning message is issued and the operation is stopped: 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, pass the attribute name, and the correct value to the setProperty method: doc.setproperty _ "SelectionLANGuage", "XPath", then create the path to the selection of the XML document section. Obviously, the relevant string is located in a location of the Languages element, so Languages becomes the first part of the path. At the same time, we know that the string is included in a language element, but is it? Please don't forget that in the previous XML document, we embed an attribute called XML: LANG and give a unique value "en".
This is what we have to choose. Here is the syntax to operate: SEL = "/ Languages / Language" SEL = SEL & "[@XML: lang = 'en']" This is a bit hard understanding, but you can file this paragraph XPath code imagine as a SQL statement, similar to the command to retrieve a recordset: select * from language, back to the real-name XPath query, return to the following code to return to contain the first match Node object: set selectedNode = _ doc.selectsinglenode (SEL) The last step is to transfer element name ("title") to the Selectsinglenode method and ask it to retrieve the TEXT attribute value of the "Title" node. That is, the text included in
and Title> is obtained. Here, the recovered text is "localize asp with xml": txt = selectedNode._ selectsinglenode ("Title"). TEXT with SQL command control, just retrieve a field value in the inside of the ADO recordset, statement is: txt = RS ("Title" Inserting a language string in the ASP page knows how to select text from the XML element and assign a value to the variable, you can insert the variable value into the HTML stream. Input function getstring (instring) temp = selectednode._ selectSingleNode _ (instring) .text getstring = _ server.htmlencode (temp) end function getstring () function: In order to code simple, create a function called getString (), as follows The value is the element name, and the element value is to 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 insurance, we use the ASP's Server.htmlencode method to convert text as a legitimate HTML code. In the ASP page, the calling code is similar to:
<% = getString ("firstname")%>: td> If the ENGLISH section of the XML document is selected, the HTML output is as follows:
first name: V / td> If French, the HTML output is as follows:
préNom: td> The following figure shows the French language version of the form: Select a big advantage of the available language XML is that it is saved in a clear text format, We can update the XML file on the web server at any time. Moreover, we can also open the English version of the XMLTrans.xml file in the XML editor XML NothPad, copy one, and then translate it into a new language. The figure below shows the routine files in XML Notepad: in the above image, adding, deleting, and selecting an operation of adding, deleting, and selecting elements and attributes on the left, editing the relevant content on the right.
When the site requires multiple language versions, you only need to perform paste operations in the content, and then upload the latest XML documentation. In order to make the added language immediately take effect, add a program, use it to determine the number of different languages in the file, and return to the language code and language name. As shown in the following code segment, we can format the data to create an HTML list box. When the user submits the form, the language type code represented by the 2 letter is stored in the ASP session variable choselang.