XML plus ASP implementation web "localization"

zhaozj2021-02-08  280

XML plus ASP implementation web "localization"

(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: 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> <received> Area code </ area> <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> <suffi X> Suffixe / Titre </ suffix> <address1> rue </ address1> <address2 /> <address3 /> <city> Ville </ city> <region> Région / province </ region> <postalcode> code postal </ Postalcode> <AreaCode> Indicatif Régional </ area> <Telephone></p> <p>Numéro de téléphone </ telephone> <submit> OK </ submit> <lang> fr </ lang> <charset> Windows-1252 </ charset> <langname> French </ langname> </ logage> <language XML: LANGUAGUAGE = "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 </ 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: <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 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.</p> <p>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 <title> element to contain the title of the HTML page. Similarly, the <firstname> element contains a string "first name". <Title> localize asp with xml </ title> <firstname> 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".</p> <p>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 <title> 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: <TD> <% = getString ("firstname")%>: </ td> If the ENGLISH section of the XML document is selected, the HTML output is as follows: <TD> first name: V / td> If French, the HTML output is as follows: <TD> 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.</p> <p>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. <select name = "chosenland"> <% 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 ("chosenland)) Then response.write" SELECTED> "else response.write"> " Endiffes.Write SelectedNodes.Write SELECTEDNODES.ITEM (i) ._ selectsinglenode ("langname"). Text & _ </ option> "& vbrlf Next%> </ select> Add prompt information and character set data is provided with XML The variable language text of the HTML form is displayed, and then some richer uses are considered. For example, to add a prompt when selecting a language, just embed HTML tag <label> before and after text, and provide variable content from XML as a title attribute value. Similarly, in order to help the browser identify the language type of the HTML page, save the character set information to the XML file, such as <Charset> X-SJIS </ CHARSET>, you can use ASP to insert the character set type into the HTML stream. Medium: <meta http-equiv = "Content-type" content = "text / html; charset = <% = getString (" charset ")%>" /> The last HTML code includes the japanese characters collection: <meta http -Equiv = "Content-Type" content = "text / html; charset = x-sjis" /> The figure shows the page using the Japanese character set. Remember, to see the correct content, the operating system, and browser must support Unicode and install the corresponding characters. Otherwise, you will see some strange words, maybe some question marks, or some square symbols, etc. Summary This article tells how to save the language string of the web page using the XML file. From China, I learned to use the ASP script and use the Microsoft XML parser and the XPath language of the W3C to realize the functionality of the XML document. The ASP code will insert the variable text containing the character set type into the HTML stream and form a list box to allow the user to select the available language.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-1563.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="1563" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.034</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'Dc2pSxIzTVllB_2BZFsg6eKX5p_2FQPmxWjNN1zVaRsCPHyIS6oGiNB3_2Fag_2BqWJCpkFa1YF4upPqs5Me6Q3z6y6wxQ_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>