Net2.0 + xml for multi-language development 333

Source: Internet
Author: User

Method:

1. Index. aspx

<Form id = "form1" runat = "server">
<Div>
<Asp: Label ID = "lanuage" runat = "server"> Label </asp: Label>
<Asp: DropDownList ID = "drp_language" runat = "server" AutoPostBack = "True" OnSelectedIndexChanged = "drp_registrage_selectedindexchanged"
Width = "152px">
</Asp: DropDownList>
<Asp: Button ID = "btnLogon" runat = "server" Text = "Button" OnClick = "btnLogon_Click"/> </div>
</Form>

2. index. aspx. cs

Public partial class index: System. Web. UI. Page
{
Public static string strName;

Protected void Page_Load (object sender, EventArgs e)
{
If (! Page. IsPostBack)
{
Init ();
}

}
Private void init ()
{
Drp_language.datasource = tangramminiweb. ResourceManager. getsupportedlanguages ();
Drp_language.datatextfield = "name ";
Drp_language.datavaluefield = "key ";
Drp_language.databind ();

If (drp_language.items.count> 0)
{
Strname = drp_language.selectedvalue;
Showtext (drp_language.items [0]. value );
}
}
Private void ShowText (string userlanguage)
{
BtnLogon. Text = TangramMiniWeb. ResourceManager. GetString ("GO", userlanguage );
// StrName = btnLogon. Text = MultLanuage. ResourceManager. GetString ("btnLogon", userlanguage );
Lanuage. Text = TangramMiniWeb. ResourceManager. GetString ("lanuage", userlanguage );

}
Protected void drp_language_SelectedIndexChanged (object sender, EventArgs e)
{
Showtext (drp_language.selectedvalue );
Strname = drp_language.selectedvalue;
}
Protected void btnlogon_click (Object sender, eventargs E)
{
Session ["selectvalue"] = drp_language.selectedvalue.tostring ();
Response. Redirect ("Tan/default. aspx ");
}
}

3. ResourceManager. CS

Public class ResourceManager
{

Public ResourceManager ()
{
//

}
Public static string getstring (string name, string userages)
{
// Return the hash table containing information about this attribute name by specifying the attribute name
Hashtable source = loadsorce (userages );
If (source! = NULL)
{
// If it is not null, its value is returned.
Return source [name]. ToString ();
}
Else
{
// Null
Return "";
}

}
// Obtain the language directory data and return a Data View
Public static DataView GetSupportedLanguages ()
{
String cacheKey = "Ages ";
// Determine whether the current data cache is empty
If (HttpContext. Current. Cache [cacheKey] = null)
{
// Obtain the physical path of the xml file
String PathFile = System. Web. HttpContext. Current. Server. MapPath (HttpContext. Current. Request. ApplicationPath + "/ages. xml ");
DataSet objDataSet = new DataSet ();
//
ObjDataSet. ReadXml (PathFile );
CacheDependency dp = new CacheDependency (PathFile );
HttpContext. Current. Cache. Insert (cacheKey, objDataSet. Tables [0]. DefaultView, dp, DateTime. MaxValue, TimeSpan. Zero );
}
Return (DataView) HttpContext. Current. Cache [cacheKey];

}
// Obtain the language statement dictionary

Public static Hashtable LoadSorce (string userages)
{
// Obtain the physical path of the xml file
String PathFile = System. Web. HttpContext. Current. Server. MapPath (HttpContext. Current. Request. ApplicationPath + "/ages/" + userages + "/Resources. xml ");
// Create an xml document Instance Object
System. Xml. XmlDocument LagXml = new XmlDocument ();
// Import the XML file to be operated
LagXml. Load (PathFile );
//
Hashtable supportedLanguages;
// Create a HASHTABLE object instance
SupportedLanguages = new Hashtable ();
// Traverse all the child nodes in the XML file
Foreach (XmlNode n in LagXml. SelectSingleNode ("root"). ChildNodes)
{
If (n. NodeType! = XmlNodeType. Comment)
{
// Add the key/value pair of the subnode to hashtable
SupportedLanguages. Add (n. Attributes ["name"]. Value, n. InnerText );
}

}
// Return hashtable
Return supportedLanguages;


}
}

4. Prepare web. config

<! -- Globalization
This section sets the global settings of the application.
-->
<Globalization requestEncoding = "UTF-8" responseEncoding = "UTF-8"/>
<XhtmlConformance mode = "Legacy"/>

5. global. asax

Global. asax. cs

Global. asax. resx

6. Create a resource file

Create a folder named ages

Three more data files en-us resourcesEN-US.xml

Zh-cn resourcesZH-CN.xml

Zh-tw resourcesZH-TW.xml

(Share XML files) language. xml

 

 

This small test is successful.
Now I want to display the whole site in Chinese and English.
Let's take a webpage named about. aspx. I will talk about my personal method.
Complete the Chinese page of about. aspx, call the base class of my language, and then write the XML file of the two languages of about. aspx XML.

Finally, every page must be completed in this way.

Yaqi

 

 

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.