首页 > 学院 > 开发设计 > 正文

asp.net2.0 国际化

2019-11-15 02:30:18
字体:
来源:转载
供稿:网友

asp.net2.0 国际化

公司业务需要在国外开展了, 因此以前的系统要做多国语言了, 从网上搜集了好多资料, 最后选择了一个比较简单的方案

1. 打开vs2005, 新建网站,

  首先在配置文件中添加配置:

  <system.web>

    <globalization culture="auto" uiCulture="auto"/>

  </system.web>

  添加asp.net文件夹App_GlobalResources, 添加“资源文件”, 如下图

  

2. 复制Resource.resx, 粘贴到App_GlobalResources文件夹下, 更改名称为Resource.en-us.resx,内容更改如下:

  

3. asp:Label调用

  <asp:Label ID="Label1" runat="server" Text="<%$Resources:Resource,msg1%>"></asp:Label>

4. asp:Button调用

  <asp:Button ID="Button1" runat="server" Text="<%$ Resources:Resource,msg1 %>" OnClientClick="test();" />

5. js调用

  alert("<%=Resources.Resource.msg1 %>");

6 后台调用

  <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

  Label2.Text = Convert.ToString(GetGlobalResourceObject("Resource", "msg1"));

7. 图片资源

  <asp:Image ID="Image1" runat="server" />

  然后在Page_Load中Image1.ImageUrl = "~/Image.aspx?culture=" + Convert.ToString(session["culture"]);

  

PRotected override void InitializeCulture()    {        base.InitializeCulture();        if (null != Session["culture"])        {            string c = Convert.ToString(Session["culture"]);            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(c);            Thread.CurrentThread.CurrentUICulture = new CultureInfo(c);                    }    }    protected void Page_Load(object sender, EventArgs e)    {        string c = Request["culture"];        System.Drawing.Bitmap img = (System.Drawing.Bitmap)GetGlobalResourceObject("Resource","test");        System.IO.MemoryStream ms = new System.IO.MemoryStream();        img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);        Response.ClearContent();        Response.ContentType = "image/jpeg";        Response.BinaryWrite(ms.ToArray());        img.Dispose();        ms.Dispose();        ms.Flush();    }

8 手动设置语言

  

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">            <asp:ListItem Value="zh-cn">中文</asp:ListItem>            <asp:ListItem Value="en-us">english</asp:ListItem>        </asp:DropDownList>
protected override void InitializeCulture()    {        base.InitializeCulture();        if (null != Session["culture"])        {            string c = Convert.ToString(Session["culture"]);            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(c);            Thread.CurrentThread.CurrentUICulture = new CultureInfo(c);        }    }       protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)    {        string c = DropDownList1.SelectedValue;        if (!string.IsNullOrEmpty(c))        {            Session["culture"] = c;            this.Response.Redirect("~/Default.aspx", true);        }    }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表