ASP.NET(3):多语言实现

1.定义用户语言设置信息类,对用户选择的语言进行保存,可以用Session,这里选用的是Cookies.

 

using  System;
using  System.Data;
using  System.Configuration;
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  CommonUtility;

ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary>
///ASPNET方式下为上下文语言环境提供语言设置对象
/// </summary>

public   class  ASPNETLocalizer : ILocalizer
ExpandedBlockStart.gifContractedBlock.gif
{
    HttpContext httpContext;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 创建对象
    
/// </summary>
    
/// <param name="httpResponse">HTTP相应对象</param>

    public ASPNETLocalizer()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
this.httpContext = HttpContext.Current;
    }


ContractedSubBlock.gifExpandedSubBlockStart.gif    
ILocalizer 成员#region ILocalizer 成员

    
public string LocalizerName
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//由于Cookies有Response.Request之分,所以需要在HttpContext上下文中保存语言信息
            if (httpContext.Items[CookiesCenter.Language] == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                var localizerName 
= Cookies.ReadCookies(httpContext.Request, CookiesCenter.Language);
                
if (String.IsNullOrEmpty(localizerName))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    localizerName 
= "zh-CN";
                    SetName(localizerName);
                }

                
return localizerName;
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return httpContext.Items[CookiesCenter.Language].ToString();
            }

        }

        
set
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            SetName(value);
        }

    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 设置名称
    
/// </summary>
    
/// <param name="localizerName">语言名称</param>

    void SetName(string localizerName)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        Cookies.WriteCookies(httpContext.Response, CookiesCenter.Language, localizerName);
        httpContext.Items[CookiesCenter.Language] 
= localizerName;
        System.Globalization.CultureInfo ci 
= new System.Globalization.CultureInfo(localizerName);
        System.Threading.Thread.CurrentThread.CurrentCulture 
= ci;
        System.Threading.Thread.CurrentThread.CurrentUICulture 
= ci;
    }


    
#endregion

}

由于对Cookies的访问有一定的限制,Request对象时候读,Response对象写,所以我们需要将用户选择语言的值保存在其它对象里面,可以保存在Session里,这里选择保存在HttpContext.Items中。

2.定义HttpModule,每次访问的时候根据用户的设置来改变语言

 

ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Data;
using System.Configuration;
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 CommonUtility;

/// <summary>
///自定义向实现类提供模块初始化和处置事件。
/// </summary>
public class MYHttpModule : IHttpModule
{
    HttpApplication context;
    
public PhenixHttpModule()
    {
     
    }

    
#region IHttpModule 成员

    
public void Dispose()
    {
        UnityContainer.ObjectProvider 
= null;
        context.PostAcquireRequestState 
-= new EventHandler(context_PostAcquireRequestState);
        
    }

    
public void Init(HttpApplication context)
    {
        
this.context = context; 
        context.PostAcquireRequestState 
+= new EventHandler(context_PostAcquireRequestState);
    }

    
void context_PostAcquireRequestState(object sender, EventArgs e)
    {
        var app 
= sender as HttpApplication;

        
if (UnityContainer.ObjectProvider == null)
        {
            var provider 
= new ASPNETObjectProvider();
            UnityContainer.ObjectProvider 
= provider;
        }

        System.Globalization.CultureInfo ci 
= new System.Globalization.CultureInfo(UnityContainer.GetObject<ILocalizer>().LocalizerName);
        System.Threading.Thread.CurrentThread.CurrentCulture 
= ci;
        System.Threading.Thread.CurrentThread.CurrentUICulture 
= ci;
    }

    
#endregion
}

 

3.在Web.Config中添加记录

 

< httpModules >
      
< add  name ="MYModule"  type ="MYHttpModule" />
</ httpModules >

 

4.定义资源文件存取对象ResourceManager

 

ContractedBlock.gif ExpandedBlockStart.gif Code
public class ResourceManager
{
    XElement ResourceManager;
    public Localizer()
    {
       //可以根据自己的需要处理资源文件的读取
        var culture = System.Threading.Thread.CurrentThread.CurrentCulture;

        var filepath = HttpContext.Current.Server.MapPath("~/App_Code/"+ culture.Name+"/lang.xml");
        xmlLocalizer = XElement.Load(filepath);
    }

    public Localizer()
        : this())
    {
    }

    public string GetString(string id)
    {
         return return xmlLocalizer.Descendants("resource").Where(p => p.Attribute("name").Value == id).Select(p1 => p1.Value).FirstOrDefault();
    }
}

 

5.在需要赋值的地方,实例化赋值即可

 

说明:初学ASP.NET,此方案很不成熟,自己练手用。

转载于:https://www.cnblogs.com/rock_chen/archive/2008/10/30/1323287.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值