多语言IhttpModule方式

public class MatchLanguage
{
    private static Dictionary<string, int> _dicLang;
    public MatchLanguage()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    private static string GetLanguage()
    {
        if (HttpContext.Current.Cache["MatchLanguage_dicLang"] != null)
        {
            _dicLang = HttpContext.Current.Cache["MatchLanguage_dicLang"] as Dictionary<string, int>;
        }
        else
        {
            _dicLang = new Dictionary<string, int>();
            _dicLang.Add("zh-cn", 1);
            _dicLang.Add("en-us", 2);
            HttpContext.Current.Cache.Insert("MatchLanguage_dicLang", _dicLang, null, DateTime.Now.AddDays(1),System.Web.Caching.Cache.NoSlidingExpiration);
        }
        string lang="zh-cn";
        if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["lang"]))
        {
            if (_dicLang.ContainsKey(HttpContext.Current.Request.QueryString["lang"].ToLower()))
            {
                lang = HttpContext.Current.Request.QueryString["lang"].ToLower();
            }
        }
        return lang;
    }

    public static void SetLanguage()
    {

        System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(GetLanguage());
      
    }

public class InfoHttpModule : IHttpModule
{
 public static string GetCommon(string key)
        {
            string result = Resources.Common.ResourceManager.GetString(key);
            return result;
        }


  public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(Begin_Request);
            context.EndRequest += new EventHandler(End_Request);
        }
 private void Begin_Request(Object sender, EventArgs e)
        {
            startTime = DateTime.Now;
            MatchLanguage.SetLanguage();
        }
}

程序就会根据当前的 CurrentUICulture  去找对应的资源文件
<httpModules>
      <add type="Sunxw.SEO.InfoHttpModule" name="InfoHttpModule"/>
<htpModules>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值