全局方案:
在资源方案视图中选中网站,右键,点击[添加ASP.NET 文件夹]->[App_GlobalResources]

建完之后,项目之下多了这一个文件夹

再选中App_GlobalResources文件夹,右键,点击[添加新项],在弹出的对话框中选中“资源文件”,命名为“LocalizedText.resx”,点击[ 添加],如图所示:

双击LocalizedText.resx进行编辑,添加一条新的字符串资源,如图所示:
复制LocalizedText.resx,粘贴到App_GlobalResources目录,重命名为LocalizedText.en-us.resx,双击进行编辑,添加一条新的字符串资源 ,如图所示:
打开Default.aspx,切换到[设计]视图,从工具箱拖一个Label控件到页面上。切换到[源]视图,修改代码:
<asp:Label ID="Label1" runat="server" Text='<%$ Resources:LocalizedText,Name %>'></asp:Label>
动态切换语言设置
以上的介绍都是通过IE浏览器获取语言设置,其实我们可以自己设置使用哪种语言。
1)通过在每个页面里的Page节指定
<%@ Page Culture="en-us" UICulture="en-us" %>
如上所设,该页将使用en-us的语言设置。
注意:这只是个概要式写法,实际的页面中的Page一般都包含更多的属性。
2)通过在Web.Config里的globalization节指定
<system.web>
    <globalization Culture="en-us" UICulture="en-us" />
</system.web>
3)当然还有一种就是通过编程动态切换语言设置啦,这也是实际项目中经常用到的方式
打开Default.aspx,切换到[源]视图,添加如下代码
<a href="?currentculture=zh-cn">中文(中国)</a>
         
        <a href="?currentculture=en-us">English(USA)</a>
        打开Default.aspx.cs,添加如下代码
String s;
    protected override void InitializeCulture()
    {
        s = Request.QueryString["currentculture"];
        if (!String.IsNullOrEmpty(s))
        {
            //UICulture - 决定了采用哪一种本地化资源,也就是使用哪种语言
            //Culture - 决定各种数据类型是如何组织,如数字与日期
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(s);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(s);
        }
    }
    记得添上这个引用
using System.Threading; using System.Globalization;
运行程序,切换语言设置,可以看到全局资源的使用效果了,如图所示:
中文(中国)
英语(美国)
Default.aspx全整示例代码:
前台:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
    <form id="form1" runat="server">
    <div>
     <a href="?currentculture=zh-cn">中文(中国)</a>
       
      <a href="?currentculture=en-us">English(USA)</a>
        <asp:Label ID="Label1" runat="server" Text='<%$ Resources:LocalizedText,Name %>'></asp:Label>
    </div>
    </form>
</body>
</html>
后台:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
     String s;
    protected override void InitializeCulture()
    {
        s = Request.QueryString["currentculture"];
        if (!String.IsNullOrEmpty(s))
        {
            //UICulture - 决定了采用哪一种本地化资源,也就是使用哪种语言
            //Culture - 决定各种数据类型是如何组织,如数字与日期
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(s);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(s);
        }
    }
}
- 本文标题: ASP.NET如何实现中英文双语言切换国际化效果
- 文章分类:【.NET/Web】
- 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.