Practice-Implementation of simple multi-language

Source: Internet
Author: User

 

Many people have asked how to implement the multi-language implementation of the autumn garden. Here, I will give a brief introduction to some netizens.

 

You can use the following methods to implement multiple languages:

1: Use system resource files for translation

2: Read external files for translation

3: use external sites such as google for translation.

 

Here is a brief introduction to the second type:

 

Reading external files for translation is also the translation method used by the autumn garden.

 

External files are commonly used Xml files. Xml is also used in this section.

It is not difficult to use multiple languages. You only need to set the corresponding dictionary key/Value for the nodes or attributes in xml, and then read and translate them on the interface.

 

Let's look at a simple Xml dictionary:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Root>
<Div id = "title" eng = "English title" cus = "Custom title"> Chinese title </div>
<Div id = "autumn" eng = "Autumn" cus = ""> autumn garden </div>
<Div id = "url" eng = "http://www.cyqdata.com/english" cus = "http://cyq1162.cnblogs.com"> http://www.cyqdata.com </div>
</Root>

Each id corresponds to a key/Value. In order to implement multiple languages, some attributes are used to extend more languages.

 

What will happen next? Read the Xml file and display it on the interface.

 

The following uses a simple example code to read the above Xml:

System. Xml. XmlDocument doc = new System. Xml. XmlDocument ();
Try
{
Doc. Load (Server. MapPath ("Lang. xml "));
System. Xml. XmlNode node = doc. SelectSingleNode ("// div [@ id = 'title']");
If (node! = Null)
{
Response. Write (node. InnerText );
}
}
Catch
{
}

The above code will output"Chinese titleThe four words are hard to grasp for everyone. The above part of the XPath syntax is now [everyone will check more information].

 

Therefore, based on the above Code, as long as it is extended, after reading the node, according to different conditions [Cookie/Session/Url parameters] Judgment,

Output the values of different node attributes to obtain the corresponding output in different languages, for example:

Node. Attributes ["eng"]. InnerText;
Node. Attributes ["cus"]. InnerText;

 

Through the above methods, you can basically implement your own simple multi-language processing work and simply encapsulate it. A easy-to-use multi-language class can also be written by yourself.

 

The following uses the multi-language classes encapsulated by the CYQ. Data framework to demonstrate a complete example.

 

1: create a website project: MutilLanguageDemo

 

2: add reference to the projectCYQ. Data Data frameworkV4.0To perform Xml operations:

 

3: Create Lang. Xml with the xml content above.

 

The final project is as follows:

 

Here we use cookies to set the language version. Therefore, you need to set the domain name in the configuration file as follows:

<Deleetask>
<Add key = "Domain" value = "localhost: 9690"/>
</AppSettings>

The demonstration uses localhost: 9690, you know.

 

The following is the front-end code of Default. aspx:

 

Front-end html:

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: Button ID = "btnChina" runat = "server" Text = "Chinese output" OnClick = "btnChina_Click"/>
<Asp: Button ID = "btnEnglish" runat = "server" Text = "English output" OnClick = "btnEnglish_Click"/>
<Asp: Button ID = "btnCustom" runat = "server" Text = "Custom output" OnClick = "btnCustom_Click"/>
<P> html: <% = lang. Get ("autumn") %> </p>
<P> cs: <asp: Label ID = "labUrl" runat = "server" Text = ""> </asp: Label> </p>
</Div>
</Form>
</Body>
</Html>

 

Note:

1: There are three buttons to switch the language by clicking.

2: title, a label that is switched to a different language.

3: Use the syntax to obtain: <% = lang. Get ("autumn") %>:

4: The server assigns a value to the control.

 

 

Backend CS code:

Protected MutilLanguage lang = null; // defines the global variable
Protected void Page_Load (object sender, EventArgs e)
{
If (lang = null)
{
Lang = new MutilLanguage (Server. MapPath ("Lang. xml"), false );
}
If (! IsPostBack)
{// Set the page title and Lable
This. Title = lang. Get ("title ");
LabUrl. Text = lang. Get ("url ");
}
}
// Set the Cookie and then refresh the page.
Protected void btnChina_Click (object sender, EventArgs e)
{
Lang. SetToCookie (LanguageKey. China );
Response. Redirect (Request. RawUrl );
}
Protected void btnEnglish_Click (object sender, EventArgs e)
{
Lang. SetToCookie (LanguageKey. English );
Response. Redirect (Request. RawUrl );
}
Protected void btnCustom_Click (object sender, EventArgs e)
{
Lang. SetToCookie (LanguageKey. Custom );
Response. Redirect (Request. RawUrl );
}

 

The code above is very simple. It is not explained much. Through the Get method, the Xpath syntax is encapsulated to make it easier to use.

 

Next, let's take a look at the final implementation results:

 

Click "Chinese output ":

 

Click "English output ":

 

Click "Custom output ":

 

Finally, download the following sample code: Click to enter the download page.

 

 

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.