首页 > 代码库 > 让asp.net网站支持多语言,使用资源文件
让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=http://www.mamicode.com/"fr">>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 = http://www.mamicode.com/ddlLanguage.SelectedItem.Value;>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网站支持多语言,使用资源文件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。