让asp.net网站支持多语言,使用资源文件

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>asp.net多语言支持例子</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddlLanguage" runat="server" AutoPostBack="true"
            onselectedindexchanged="ddlLanguage_SelectedIndexChanged">
            <asp:ListItem Text="France" Value="fr"></asp:ListItem>
            <asp:ListItem Text="简体中文" Value="zh-cn"></asp:ListItem>
            <asp:ListItem Text="English" Value="en-us"></asp:ListItem>
        </asp:DropDownList>
        <asp:Label ID="lb1" runat="server" ></asp:Label>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class test : PageBase  //继承
{
    private string CultureLang = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lb1.Text = Resources.Resource.LoginTitle;//后台代码中使用
            CultureLang = Session["PreferredCulture"].ToString().ToLower().Trim();
            switch (CultureLang)
            {
                case "zh-cn":
                    this.ddlLanguage.Items.FindByText("简体中文").Selected = true;
                    Session["PreferredCulture"] = "zh-cn";
                    break;
                case "en-us":
                    this.ddlLanguage.Items.FindByText("English").Selected = true;
                    Session["PreferredCulture"] = "en-us";
                    break;
                case "fr":
                    this.ddlLanguage.Items.FindByText("France").Selected = true;
                    Session["PreferredCulture"] = "fr";
                    break;
                default:
                    break;
            }
            //Response.Write(lb1.Text);
        }
    }


    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        string value = ddlLanguage.SelectedItem.Value;
        Session["PreferredCulture"] = value;
        //重定向页面
        Response.Redirect(Request.Url.PathAndQuery);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.Globalization;

/// <summary>
///PageBase 的摘要说明
/// </summary>
public class PageBase : System.Web.UI.Page
{
	public PageBase()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    protected override void InitializeCulture()
    {
        // 用Session来存储语言信息
        if (Session["PreferredCulture"] == null) { Session["PreferredCulture"] = Request.UserLanguages[0]; }
        string UserCulture = Session["PreferredCulture"].ToString();
        if (UserCulture != "")
        {
            //根据Session的值重新绑定语言代码
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(UserCulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(UserCulture);
        }
    } 
}
创建“App_LocalResources”文件夹,建立三个资源文件,同样三个LoginTitle键,值不一样,对应各自语种

让asp.net网站支持多语言,使用资源文件_第1张图片

CultureInfo 类  参考 
http://msdn.microsoft.com/zh-cn/library/system.globalization.cultureinfo(v=vs.80).aspx

你可能感兴趣的:(让asp.net网站支持多语言,使用资源文件)