1. Create an Asp.net Web Application Program
2. Shows the created Project.
3. Right-click the WEB Project name and add a global resource folder "app_globalresources", which is unique to Asp.net 2.0.
4. Right-click the "app_globalresources" folder and add two resource files: language. resx (simplified resource file) and language. en-us.resx (resource file in English)
5. Open two resource files and add corresponding resource information, as shown in
6. Open the default. aspx file and enter the following Code :
<Body>
<Form ID = "form1" runat = "server">
<Center>
<Div style = "margin: 20px; padding: 10px; Height: 200px; width: 200px; Border: solid 1px # c0c0c0;
Text-align: center; ">
<Br/>
<A href = "? Curlanguage = ZH-CN "> Chinese </a> & nbsp;
<A href = "? Curlanguage = en-us "> English </a>
<Br/>
<Br/>
Country: & nbsp; <asp: literal id = "ltlcountry" runat = "server"> </ASP: literal>
<Br/>
City: & nbsp; <asp: literal id = "ltlcity" runat = "server"> </ASP: literal>
<Br/>
<Br/>
Country 2: & nbsp; <asp: literal id = "ltlcountry2" runat = "server"> </ASP: literal>
<Br/>
City 2: & nbsp; <asp: literal id = "ltlcity2" runat = "server"> </ASP: literal>
</Div>
</Center>
</Form>
</Body>
Preview as shown in:
7. Open the default. aspx. CS file and enter the following code:
// This code is very important
protected override void initializeculture ()
{< br> string culture = request. querystring ["curlanguage"];
If (! String. isnullorempty (Culture)
{< br> system. threading. thread. currentthread. currentuiculture = new system. globalization. cultureinfo (Culture);
system. threading. thread. currentthread. currentculture = system. globalization. cultureinfo. createspecificculture (Culture);
}< BR >}
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Ltlcountry. Text = resources. Language. Country. tostring ();
Ltlcity. Text = resources. Language. City. tostring ();
Ltlcountry2.text = (string) getglobalresourceobject ("language", "country ");
Ltlcity2.text = (string) getglobalresourceobject ("language", "city ");
}
}
8. For the entire project under build, press F5 to browse: click the "Chinese" and "English" links in to view the results we want.
In the previous articleArticleBasically, the multi-language functions of the website have been implemented. But how can I use document files for images and CSS style files on the website?
1. We will continue with the project created in the previous article, create a new test2.aspx file, and create a new images folder and style folder.
Place two images in the images Folder: loading.gif and loading_en.gif. Create two CSS style sheet files in the style folder: css.css and css_en.css.
The style code of the css.css file is:
Body
{
Font-family: Arial @ fonfont-size: 12px;
}
# Maindiv
{
Margin: 20px;
Padding: 10px;
Height: 200px;
Width: 200px;
Border: solid 5px # c0c0c0;
Text-align: center;
Background-color: olive;
}
The CSS file style code is as follows:
Body
{
Font-family: Arial verdana;
Font-size: 12px;
}
# Maindiv
{
Margin: 20px;
Padding: 10px;
Height: 200px;
Width: 200px;
Border: solid 5px # c0c0c0;
Text-align: center;
Background-color: Gray;
}
2. A new class named baselanguage. CS is added and the inheritance class is set: system. Web. UI. Page. The Code is as follows:
Public class baselanguage: System. Web. UI. Page
{
Protected override void initializeculture ()
{
String culture = request. querystring ["curlanguage"];
If (! String. isnullorempty (Culture ))
{
System. Threading. thread. currentthread. currentuiculture = new system. Globalization. cultureinfo (Culture );
System. Threading. thread. currentthread. currentculture = system. Globalization. cultureinfo. createspecificculture (Culture );
}
}
}
Because the above code is required for every page, it is written to a special class and inheritedSystem. Web. UI. Page, and then each page inherits this class.
Shows the file structure of the entire project:
3. Open the test2.aspx file and add it to the head.
<Head runat = "server">
<Title> implement multiple languages in Asp.net 2.0 </title>
<Link href = "style/css.css" rel = "stylesheet" type = "text/CSS" id = "Link" runat = "server"/>
</Head>
Here, I added runat = "server" id = "Link" to <link> and used link as a server control, which can be referenced in the background.
The code in the body is as follows:
<Body>
<Form ID = "form1" runat = "server">
<Center>
<Div id = "maindiv">
<Br/>
<A href = "? Curlanguage = ZH-CN "> Chinese </a> & nbsp;
<A href = "? Curlanguage = en-us "> English </a>
<Br/>
<Br/>
Country: & nbsp; <asp: literal id = "ltlcountry" runat = "server"> </ASP: literal>
<Br/>
City: & nbsp; <asp: literal id = "ltlcity" runat = "server"> </ASP: literal>
<Br/>
<Br/>
Country 2: & nbsp; <asp: literal id = "ltlcountry2" runat = "server"> </ASP: literal>
<Br/>
City 2: & nbsp; <asp: literal id = "ltlcity2" runat = "server"> </ASP: literal>
<Br/>
<Br/>
<Br/>
<Br/>
</Div>
</Center>
</Form>
</Body>
// Add an IMG label, runat = "server"
4. Open the resource file and add a resource item named loadingimg and pagestyle, as shown in
5. Open the test2.aspx. CS file and replace it with "system. Web. UI. Page" to inherit the "baselanguage" class.
The Code is as follows:
Public partial class Test2:Baselanguage
{
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Link. href = resources. Language. pagestyle. tostring (); // set the style file on the page
Imgloading. src = resources. Language. loadingimg. tostring (); // path of the image
Ltlcountry. Text = resources. Language. Country. tostring ();
Ltlcity. Text = resources. Language. City. tostring ();
Ltlcountry2.text = (string) getglobalresourceobject ("language", "country ");
Ltlcity2.text = (string) getglobalresourceobject ("language", "city ");
}
}
}
6. Build the entire project, F5 browse test2.aspx effect: the image and style have changed
7. Finally, how can I enable my website to automatically display the corresponding information based on the browser language settings of the user end?
1) Open the Web. config file and add the globalization configuration section in the middle of the system. Web Section. The following code:
<System. Web>
.......
<Globalization culture = "Auto" uiculture = "Auto" requestencoding = "UTF-8" responseencoding = "UTF-8"/>
</System. Web>
2) Open the IE browser, and set the language of the tool> Internet Options> General> language to move the English language (U.S.) to the top of the browser, as shown in:
Click "OK", close the browser, and then browse test2.aspx again. The English is displayed automatically when we do not click "English.