1.建立一個asp.net web應用程式
2.建立的項目如所示
3.右擊web項目名稱,添加一個全域資源檔夾"app_GlobalResources" ,這個是asp.net 2.0特有的
4.右擊"app_GlobalResources"檔案夾,添加兩個資源檔: language.resx(簡體資源檔) 和language.en-us.resx (英文的資源檔)
5.開啟兩個資源檔,添加相應的資源資訊,如所示
6.開啟default.aspx檔案,輸入如下代碼:
<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">中文</a>
<a href="?curlanguage=en-us">英文</a>
<br />
<br />
國家: <asp:Literal ID="ltlcountry" runat="server"></asp:Literal>
<br />
城市: <asp:Literal ID="ltlcity" runat="server"></asp:Literal>
<br />
<br />
國家2: <asp:Literal ID="ltlcountry2" runat="server"></asp:Literal>
<br />
城市2: <asp:Literal ID="ltlcity2" runat="server"></asp:Literal>
</div>
</center>
</form>
</body>
預覽如所示:
7.開啟Default.aspx.cs檔案,輸入如下代碼:
//這段代碼很重要
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);
}
}
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. build下整個項目,按F5瀏覽: 點擊中的 "中文" 和 "英文" 連結就可以查看我們所要的效果了