Multi-language support for ASP

Source: Internet
Author: User
Tags modify variables reference versions
Outline
Let's imagine that you've designed a successful site using Active Server Pages, and your customers have asked to internationalize this site to provide multiple language versions. What do you do at this time? Please read this article.

Body
First, introduce

Let's imagine that you've designed a successful site using Active Server Pages, and your customers have asked to internationalize this site to provide multiple language versions. The key to this problem is that you have to use some method to achieve fast page content conversion. This problem can be viewed as extracting the appropriate data from the database and dynamically displaying it using ASP. In this article we will discuss how to use the Dictionary object of Active Server Pages and a background database to enable internationalization support.

The specific implementation process is divided into the following three steps:
◇ Design the database and store the text and file path.
◇ read data from the database to the Dictionary object.
◇ reference text and file path in ASP page.

Second, step 1-Design the database and store the text and file paths

We need to spend some time on the database design. Using this database, we want to be able to properly catalogue all the text and pictures that we want to use, and the text and pictures cannot be duplicated, and if you want to change "yes" to "OK" in the application, Want to make changes in only one place; Web maintainers should be able to quickly find the target text when they change the content. Based on the above requirements, let's start designing the database.

First create two tables to place the text. One of the tables is commonstrings, used to store words and phrases that are reused throughout the site. Another table is specializedstrings, for storing words and phrases that are used only on a particular page. Storing text that is common and restricted to a particular page is useful for simplifying site maintenance.

All two tables use the Stringkey field to mark Records, use Stringlanguage to describe the language used, and joint stringkey and stringlanguage create a primary key. Field StringHolder is used to save words, phrases, or statements. This field must create a unique index to ensure that duplicate words, phrases, or statements are not entered.

For specializedstrings tables, you need an extra field asppage to mark the ASP page where the text resides.

For picture files, we can use the same steps to create a table to store paths to these files. You only need to replace the corresponding StringHolder with the FileName field in the Commonimage table and the Specializedimage table.

When you load data into a newly created table, the Stringkey content should give the site maintainer as many hints as possible to indicate the text that will appear on the page. For example, it is a good choice for an online sales site to use Orderdisclaimer for declarative statements that reject commitments, so that site maintainers can learn very clearly Use the content that the record will display on the Web page.

Put those common words and phrases on the commonstrings table. In this way, the maintainer knows that if they modify a record in the Commonstrings table, they will affect many pages within the site.

For large sites, you can design an Easy-to-use interface to enter and modify text in the database. Preparing this information is a tedious task, and the simpler it is, the less mistakes it makes.

Step 2-reading data from a database to a Dictionary object

The Dictionary object (Dictionary object) is a multipurpose server-side object that is equivalent to a two-D array, holding the key and the data associated with the key. The only way to extract data is to get the key value or index. You can configure the scope of the Dictionary object to be the entire scope of the application or the scope of the conversation without worrying about loss performance. Giving the application layer scope means that the object is instantiated only once, and that all sessions use the same Dictionary object. The following code creates two Dictionary object instances, which are associated with two text tables, respectively. Remember, we want the object to be initialized only once, so put the code in the Application_OnStart event:

< Script Runat=server language=vbscript>
Sub Application_OnStart ()
Dim dictcommonstrings
Dim dictspecializedstrings

Dim Conn
Dim rscommonstrings
Dim rsspecializedstrings

Set dictcommonstrings = Server.CreateObject ("Scripting.Dictionary")
Set dictspecializedstrings = Server.CreateObject ("Scripting.Dictionary")

The next task is to load the data into the Dictionary object. We will traverse the table, connect Stringkey and Stringlanguage, and put the concatenated value as the key value of the dictionary, and put the StringHolder in the corresponding dictionary data area. This brings us to the resilience of using the database to store data and avoids the performance impact of a persistent database connection. In fact, the database is accessed only once when the application is started, and then the ASP page reads the required data from the faster Dictionary object.

' Establish a database connection
Set Conn = Server. CreateObject ("ADODB.") Connection ") <BR>
conn.connectionstring = "Some Connection String" <BR>
Conn.Open

' Open the Commonstrings table, iterate through all the records and load the data
Set rscommonstrings = Conn.execute ("Select Stringkey, StringHolder from Commonstrings")
Do Until rscommonstrings.eof
Dictcommonstrings.add rscommonstrings ("Stringlanguage") & Rscommonstrings ("Stringkey") _
, Rscommonstrings ("StringHolder")
Rscommonstrings.movenext
Loop

' Open the Specializedstrings table, iterate through all the records and load the data
Set rsspecializedstrings = Conn.execute ("Select Stringkey, StringHolder from Specializedstrings")
Do Until rsspecializedstrings.eof
Dictspecializedstrings.add rsspecializedstrings ("Stringlanguage") & _
Rsspecializedstrings ("Stringkey"), Rsspecializedstrings ("StringHolder")
Rsspecializedstrings.movenext
Loop

Rsspecializedstrings.close
Rscommonstrings.close
Conn.close
Set rsspecializedstrings = Nothing
Set rscommonstrings = Nothing
Set Conn = Nothing
End Sub

</script>

Iv. Step 3-referencing text and file paths on ASP pages

The next step is to reference the text (and the file path) in a Dictionary object that has been instantiated in an ASP page. First we need to know the language the current user is using, one of which is to get the value from Request.Server.Variables. Please put the following code into the Session_OnStart event processing process:


< Script language=vbscript>
Sub Session_OnStart ()
Session ("Lang") = Request.Server.Variables ("Http_accept_language")
End Sub
</script>

You can get the corresponding text (or picture file path) from the Dictionary object by the key value, which is composed of the language prefix plus the name. The following is sample code:
< html>
< P align=center>
< h1><%= Dictspecializedstrings.item (Session ("Lang") & "Confirmation")%></p><br>
< P align=center>
< h5><%= Dictspecializedstrings.item (Session ("Lang") & "Orderdisclaimer")%></p>

For Chinese browsers, the prompts for this page are as follows:
Thank you for your purchase. Please go to the next page to pay.
For a browser with the default language in English, the prompt is:
Thank for the Your order. Continue to the Next Page to make your payment.

In my experience, there is no performance disadvantage when referencing text (and picture paths) from dictionary objects and entering data directly into ASP code. This is because the text (and picture path) dictionaries are placed in memory so that they can be extracted quickly.

V. Discussion

Of course, there are other factors that need to be considered to implement multilanguage support. Isolating the content of the site and applying the developer isolation is the essence of what this article is trying to illustrate. This program not only for the implementation of the site content internationalization is very useful, and for the site content frequently changed and the layout of the basic unchanged situation, this program can also play a role. Background databases, collection objects (Dictionary objects), and ASP Technologies provide an excellent solution to the expansion of site content from the source language to other languages.


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.