码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-11-26 19:04:43      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   使用   sp   

<%@ 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键,值不一样,对应各自语种

bubuko.com,布布扣

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

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

标签:style   blog   http   io   ar   color   os   使用   sp   

原文地址:http://blog.csdn.net/smartsmile2012/article/details/41518195

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!