让asp.net网站支持多语言,使用资源文件
2014-11-26 16:50
946 查看
<%@ 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键,值不一样,对应各自语种
CultureInfo 类 参考
http://msdn.microsoft.com/zh-cn/library/system.globalization.cultureinfo(v=vs.80).aspx
相关文章推荐
- 让asp.net网站支持多语言,使用资源文件
- 让asp.net网站支持多语言,使用资源文件
- 让asp.net网站支持多语言,使用资源文件
- Asp.net中使用资源文件实现网站多语言
- Asp.net 使用资源文件实现双语言
- 使用 根据语言环境不同 而显示不同的 资源本地化 ASP.NET 网页
- ASP.NET提供文件下载函数(支持大文件、续传、速度限制、资源占用小)
- ASP.NET提供文件下载函数(支持大文件、续传、速度限制、资源占用小)
- ASP.NET 中使用资源文件
- 如何:为 ASP.NET 网站创建资源文件 (Visual Studio)
- ASP.NET提供文件下载函数(支持大文件、续传、速度限制、资源占用小)
- ASP.NET 下的资源文件使用
- ASP.NET全球化 资源文件使用
- asp.net中使用资源文件
- 如何:为 ASP.NET 网站创建资源文件 (Visual Studio)
- ASP.NET提供文件下载函数(支持大文件、续传、速度限制、资源占用小)
- 在ASP.NET AJAX中使用应用程序服务和本地化(6):创建支持多语言的ASP.NET AJAX站点、小结
- ASP.NET提供文件下载函数(支持大文件、续传、速度限制、资源占用小)
- ASP.NET 中使用资源文件
- ASP.NET提供文件下载函数(支持大文件、续传、速度限制、资源占用小)