让asp.net网站支持多语言,使用资源文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了让asp.net网站支持多语言,使用资源文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2942字,纯文字阅读大概需要5分钟。
内容图文
![让asp.net网站支持多语言,使用资源文件](/upload/InfoBanner/zyjiaocheng/1071/4bea628c805c402a849f99f2e0432bc8.jpg)
<%@ 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
原文:http://blog.csdn.net/smartsmile2012/article/details/41518195
内容总结
以上是互联网集市为您收集整理的让asp.net网站支持多语言,使用资源文件全部内容,希望文章能够帮你解决让asp.net网站支持多语言,使用资源文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。