如何在ASP.NET C#网站中实现多语言链接菜单及下拉列表
在ASP.NET网站(非MVC)中用App_GlobalResources实现母版页多语言切换
没问题,我一步步带你实现ASP.NET网站里用App_GlobalResources做母版页的多语言本地化,完全匹配你的需求:
1. 创建全局资源文件
首先得建立存放多语言文本的资源文件:
- 右键你的ASP.NET网站项目 → 「添加」→「ASP.NET文件夹」→「App_GlobalResources」(这个文件夹是ASP.NET专门用来放全局资源的,必须叫这个名字)
- 在这个文件夹里添加资源文件:
- 先加一个默认的
Resources.resx(比如作为你的默认语言,比如英文) - 再添加对应其他语言的资源文件,比如
Resources.am.resx(阿姆哈拉语)、Resources.zh-CN.resx(简体中文),命名规则是「基础名+语言代码.resx」
- 先加一个默认的
- 打开每个.resx文件,添加对应的键值对,示例如下:
键名 Resources.resx(英文) Resources.am.resx(阿姆哈拉语) Buy Buy ገዛ Rent Rent ከዛ Estimate Estimate ግምት Sell Sell ሽያ SelectCategory Select the Category ክፍል ይምረጡ Villa Villa ቪላ ApartmentCondos Apartment and Condos አፓርትመንት እና ኮንዶስ Farm Farm ምግብ ማርያት ቤት Office Office ኦፊስ Store Store ሱፋ StoreyHouse Storey House ፊት ቤት Plot Plot ቦታ Shop Shop ስፖ OtherCommercial Other commercial ሌላን ዕቃ ሽያጭ
2. 添加语言切换控件与逻辑
接下来要做一个让用户切换语言的功能,这里用下拉列表示例:
母版页前端代码
在母版页的合适位置(比如导航栏旁边)添加这个下拉列表:
<asp:DropDownList ID="ddlLanguage" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlLanguage_SelectedIndexChanged"> <asp:ListItem Value="en-US" Text="English"></asp:ListItem> <asp:ListItem Value="am-ET" Text="አማርኛ"></asp:ListItem> <!-- 可以添加更多语言选项,比如 <asp:ListItem Value="zh-CN" Text="中文"></asp:ListItem> --> </asp:DropDownList>
母版页后台代码(.cs文件)
在母版页的后台添加页面加载和语言切换的逻辑:
protected void Page_Load(object sender, EventArgs e) { // 页面加载时设置当前显示的语言 if (Session["CurrentCulture"] != null) { var culture = Session["CurrentCulture"].ToString(); System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(culture); System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture); } else { // 默认设置为英文 Session["CurrentCulture"] = "en-US"; } // 页面首次加载时,设置下拉列表的选中项为当前语言 if (!IsPostBack) { ddlLanguage.SelectedValue = Session["CurrentCulture"].ToString(); } } protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e) { // 把用户选择的语言保存到Session Session["CurrentCulture"] = ddlLanguage.SelectedValue; // 刷新页面让语言切换生效 Response.Redirect(Request.Url.AbsoluteUri); }
3. 替换母版页中的硬编码文本
现在把你原来的链接和下拉列表的硬编码文本,替换成资源引用:
导航链接部分
原来的硬编码链接改成这样:
<li><a href="Buy.aspx"><%$ Resources:Resources, Buy %></a></li> <li><a href="Rent.aspx"><%$ Resources:Resources, Rent %></a></li> <li><a href="Estimate.aspx"><%$ Resources:Resources, Estimate %></a></li> <% if (Session["user"] == null) { %> <li><a href="login.aspx"><%$ Resources:Resources, Sell %></a></li> <% } else { %> <li><a href="Sell.aspx"><%$ Resources:Resources, Sell %></a></li> <%} %>
这里的<%$ Resources:Resources, Buy %>是ASP.NET的资源表达式,会自动根据当前加载的语言,从对应的.resx文件里取出Buy键对应的值。
分类下拉列表部分
有两种方式实现,选一种你觉得方便的:
方式一:直接在前端用资源表达式
<asp:DropDownList ID="ddlCategory" runat="server"> <asp:ListItem Text="<%$ Resources:Resources, SelectCategory %>" Value=""></asp:ListItem> <asp:ListItem Text="<%$ Resources:Resources, Villa %>" Value="Villa"></asp:ListItem> <asp:ListItem Text="<%$ Resources:Resources, ApartmentCondos %>" Value="Apartment and Condos"></asp:ListItem> <asp:ListItem Text="<%$ Resources:Resources, Farm %>" Value="Farm"></asp:ListItem> <asp:ListItem Text="<%$ Resources:Resources, Office %>" Value="Office"></asp:ListItem> <asp:ListItem Text="<%$ Resources:Resources, Store %>" Value="Store"></asp:ListItem> <asp:ListItem Text="<%$ Resources:Resources, StoreyHouse %>" Value="Storey House"></asp:ListItem> <asp:ListItem Text="<%$ Resources:Resources, Plot %>" Value="Plot"></asp:ListItem> <asp:ListItem Text="<%$ Resources:Resources, Shop %>" Value="Shop"></asp:ListItem> <asp:ListItem Text="<%$ Resources:Resources, OtherCommercial %>" Value="Other commercial"></asp:ListItem> </asp:DropDownList>
方式二:后台代码绑定(适合需要动态控制选项的场景)
如果需要更灵活的控制(比如根据权限显示某些选项),可以在母版页的Page_Load里添加绑定逻辑:
if (!IsPostBack) { ddlCategory.Items.Clear(); // 从资源文件获取文本并添加选项 ddlCategory.Items.Add(new ListItem(GetGlobalResourceObject("Resources", "SelectCategory").ToString(), "")); ddlCategory.Items.Add(new ListItem(GetGlobalResourceObject("Resources", "Villa").ToString(), "Villa")); ddlCategory.Items.Add(new ListItem(GetGlobalResourceObject("Resources", "ApartmentCondos").ToString(), "Apartment and Condos")); ddlCategory.Items.Add(new ListItem(GetGlobalResourceObject("Resources", "Farm").ToString(), "Farm")); ddlCategory.Items.Add(new ListItem(GetGlobalResourceObject("Resources", "Office").ToString(), "Office")); ddlCategory.Items.Add(new ListItem(GetGlobalResourceObject("Resources", "Store").ToString(), "Store")); ddlCategory.Items.Add(new ListItem(GetGlobalResourceObject("Resources", "StoreyHouse").ToString(), "Storey House")); ddlCategory.Items.Add(new ListItem(GetGlobalResourceObject("Resources", "Plot").ToString(), "Plot")); ddlCategory.Items.Add(new ListItem(GetGlobalResourceObject("Resources", "Shop").ToString(), "Shop")); ddlCategory.Items.Add(new ListItem(GetGlobalResourceObject("Resources", "OtherCommercial").ToString(), "Other commercial")); }
4. 一些实用注意事项
- 资源文件命名规范:必须遵循「基础文件名+语言代码.resx」的格式,比如
Resources.en.resx对应英文,ASP.NET会根据当前的UICulture自动加载对应的资源文件 - 持久化语言选择:如果希望用户下次访问时保持上次选择的语言,可以把语言信息存在Cookie里,而不是Session(Session会过期)。只需要把后台代码里的
Session["CurrentCulture"]替换成Cookie操作即可 - 测试语言切换:切换语言后,页面会刷新,所有绑定资源的文本都会自动变成对应语言的内容
内容的提问来源于stack exchange,提问作者Mintesnot Benti
相关产品推荐
相关产品推荐

