[原创]ASP.NET多语系(1)前端页面多语系设置

最近在项目中需要使用包括中文简体、中文繁体、英文等多语系。所以留此作为记录。

【1】首先是关于浏览器的语言设置问题。

如果在项目中使用了多语系资源,但是没有后台控制语言设置,页面设置了

那么浏览器会默认显示浏览器语言中的第一个语系(当然,如果项目中没有浏览器设置的语系,则显示项目中设置的默认语系)。

【2】项目中多语系设置

页面初始代码

View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication.WebForm1" 
Culture="auto" meta:resourcekey="PageResource1" UICulture="auto" %>

<!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>未命名頁面</title>
</head>
<body style="100%; height:100%; background-image:url('img/bg.bmp'); background-repeat:repeat; margin:0; padding:0;">
    <form id="form1" runat="server">
    <div style=" text-align:center; ">
    
    <asp:Button ID="btnSearch" runat="server" Text="查询" />
    <br />
    <asp:Localize ID="Localize1" runat="server" Text="单独的字体"></asp:Localize>
    <br />
    <asp:DropDownList ID="ddlType" runat="server">
        <asp:ListItem Text="请选择" Value="0"></asp:ListItem>
        <asp:ListItem Text="中文简体" Value="1"></asp:ListItem>
        <asp:ListItem Text="中文繁体" Value="2"></asp:ListItem>
    </asp:DropDownList>
    </div>
    </form>
</body>
</html>

   然后在aspx设计页面 ,使用菜单栏的【工具 】选项

然后系统自动产生

页面代码变为

View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication.WebForm1" 
Culture="auto" meta:resourcekey="PageResource1" UICulture="auto" %>

<!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>未命名頁面</title>
</head>
<body style="100%; height:100%; background-image:url('img/bg.bmp'); background-repeat:repeat; margin:0; padding:0;">
    <form id="form1" runat="server">
    <div style=" text-align:center; ">
    
    <asp:Button ID="btnSearch" runat="server" Text="查询" meta:resourcekey="btnSearchResource1" />
    <br />
    <asp:Localize ID="Localize1" runat="server" Text="单独的字体" meta:resourcekey="Localize1Resource1"></asp:Localize>
    <br />
    <asp:DropDownList ID="ddlType" runat="server" meta:resourcekey="ddlTypeResource1">
        <asp:ListItem Text="请选择" Value="0" meta:resourcekey="ListItemResource1"></asp:ListItem>
        <asp:ListItem Text="中文简体" Value="1" meta:resourcekey="ListItemResource2"></asp:ListItem>
        <asp:ListItem Text="中文繁体" Value="2" meta:resourcekey="ListItemResource3"></asp:ListItem>
    </asp:DropDownList>
    </div>
    </form>
</body>
</html>

注意:将 Culture="auto" 和UICulture="auto" 如果让浏览器选择语系则留着,如果后台代码选择语系则必须去掉。

然后将WebForm1.aspx.resx复制然后黏贴改名

其他需要注意的比较多,目前注意到的如下:

  1.单独的文字使用控件Localize来替换

<asp:Localize ID="Localize1" runat="server" Text="单独的字体"></asp:Localize>

  2.服务器控件 DropDownList 的 ListItem 需要设置Value属性,否则会默认为Text值(不设置Value属性js取值value为Text值)。

哎!还有的没写出来。回家了。

  年后回来继续。

※如果你觉得这篇文章不错,请点击推荐。如果你觉得我写的对你有用请关注我。
作者:Max蚊子
网站:feiger.cn         飞鸽博客,关注互联网、站长圈的程序员博客!
             
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/kim01/p/2908877.html