在ASP.NET2.0中实现多语言解决方案的完整细节

本文介绍了如何在ASP.NET 2.0中创建一个支持中英文切换的多语言网站。通过新建解决方案、设置DefaultCulture、添加资源文件、编写global.asax代码、创建MyCulture类以及在页面中应用,详细阐述了实现过程。最终展示了运行效果并提供了完整的源代码下载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很多网站都有多种语言切换的功能,典型的是中、英文两种语言,本文介绍在ASP.NET中实现中、英文两种语言的完整细节。

一、打开Visual Studio 2005并新建一个解决方案,我们起名为MyWeb。

二、在MyWeb上新建一个网站,如下图所示:

三、为新建的网站上添加web.config文件,并在appSettings中加入DefaultCulture配置节:

   < appSettings >
    
< add key = " DefaultCulture "  value = " zh-CN " />
  
</ appSettings >

此配置节表示默认语言为中文

四、为网站添加App_GlobalResources文件夹,并加入资源文件Strings.resx,在Strings.resx中配置如下图:

五、用同样的方法配置String.en-US.resx文件,如下图所示:

六、为网站添加全局应用程序类global.asax文件,并在其中加入如下代码:

     protected   void  Application_BeginRequest(Object sender, EventArgs e)
    {
        
try
        {
            
if  (Request.Cookies[ " CultureResource " !=   null )
            {
                System.Threading.Thread.CurrentThread.CurrentCulture 
=   new  System.Globalization.CultureInfo(Request.Cookies

[
" CultureResource " ].Value);
            }
            
else
            {
                System.Threading.Thread.CurrentThread.CurrentCulture 
=   new  System.Globalization.CultureInfo

(ConfigurationSettings.AppSettings[
" DefaultCulture " ].ToString());
                System.Threading.Thread.CurrentThread.CurrentUICulture 
=  System.Threading.Thread.CurrentThread.CurrentCulture;
            }
            Resources.Strings.Culture 
=  System.Threading.Thread.CurrentThread.CurrentCulture;
        }
        
catch  (Exception)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture 
=   new  System.Globalization.CultureInfo(ConfigurationSettings.AppSettings

[
" DefaultCulture " ].ToString());
        }
    }

七、在网站中添加MyCulture类,系统会提示是否要加到App_Code文件夹下,选是。
MyCulture类的代码如下:

using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

///   <summary>
///  MyCulture 的摘要说明
///   </summary>
public   class  MyCulture
{
    
public  MyCulture()
    {
        
//
        
//  TODO: 在此处添加构造函数逻辑
        
//
    }

    
///   <summary>
    
///  获取语言设置字符串
    
///   </summary>
    
///   <returns></returns>
     public   static   string  GetCultureString()
    {
        
if  (Resources.Strings.Culture.ToString().ToUpper()  ==   " en-US " .ToUpper())
        {
            
return   " En " ;
        }
        
else
        {
            
return   " Cn " ;
        }
    }
    
///   <summary>
    
///  更新用户语言设置
    
///   </summary>
    
///   <param name="culture"> 用户选择的语言 </param>
     public   static   void  UpdateCultureCookie( string  culture)
    {
        
if  (HttpContext.Current.Request.Cookies[ " CultureResource " !=   null )
        {
            HttpContext.Current.Response.Cookies[
" CultureResource " ].Value  =  culture;
            HttpContext.Current.Response.Cookies[
" CultureResource " ].Expires  =  System.DateTime.Now.AddDays( 30 );
        }
        
else
        {
            HttpCookie cultureCookie 
=   new  HttpCookie( " CultureResource " );
            cultureCookie.Value 
=  culture;
            cultureCookie.Expires 
=  System.DateTime.Now.AddDays( 30 );
            HttpContext.Current.Response.Cookies.Add(cultureCookie);
        }
        HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.ToString());
    } 
}

八、好了,准备工作做好了,我们来完成页面的制作。打开Default.aspx,在页面中加入两个Label、两个TextBox、两个按钮,并将一个按钮的Text属性设为“中文”,另一个按钮的Text属性设为“English”,在Page_Load中加入如下代码:

 

protected   void  Page_Load( object  sender, EventArgs e)
{
    
if  ( ! IsPostBack)
    {
        
this .Label1.Text  =  Resources.Strings.Name;
        
this .Label2.Text  =  Resources.Strings.Password;
    }
}

为两个按钮添加点击事件,并在其中编写点击代码:

protected   void  btn_Chinese_Click( object  sender, EventArgs e)
{
    MyCulture.UpdateCultureCookie(
" zh-CN " );
}
protected   void  btn_English_Click( object  sender, EventArgs e)
{
    MyCulture.UpdateCultureCookie(
" en-US " );
}

好了,大功告成,按F5运行,效果如下图所示:

看看切换中文和English是不是可以了。

下面是整个解决方案的源代码:

按此下载

版权所有,转载必须注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值