惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Security Latest
Security Latest
P
Palo Alto Networks Blog
AWS News Blog
AWS News Blog
NISL@THU
NISL@THU
T
Threatpost
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Latest news
Latest news
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
J
Java Code Geeks
P
Privacy International News Feed
阮一峰的网络日志
阮一峰的网络日志
S
Schneier on Security
博客园 - 聂微东
Project Zero
Project Zero
美团技术团队
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Scott Helme
Scott Helme
I
Intezer
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hacker News: Front Page
S
Security @ Cisco Blogs
博客园 - 司徒正美
O
OpenAI News
Last Week in AI
Last Week in AI
L
LINUX DO - 热门话题
酷 壳 – CoolShell
酷 壳 – CoolShell
SecWiki News
SecWiki News
月光博客
月光博客
S
Security Affairs
The GitHub Blog
The GitHub Blog
P
Privacy & Cybersecurity Law Blog
S
Secure Thoughts
V
V2EX
S
Securelist
F
Fortinet All Blogs
W
WeLiveSecurity
D
Docker
博客园 - 三生石上(FineUI控件)
Simon Willison's Weblog
Simon Willison's Weblog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
V
Visual Studio Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Webroot Blog
Webroot Blog
Engineering at Meta
Engineering at Meta

博客园 - 阿福

TypeScript forms authentication failed the ticket supplied was invalid错误 (Windows Server 2008 + IIS 7.5) jQuery Mobile 1.1.1 RC1发布 HashSet<T> vs List<T> 不要用把无序GUID既作为主键又作为聚集索引 WCF Data Services 5.0 RTM发布 EF Power Tools Beta 2发布 Entity Framework 5性能方面的注意事项 jQuery Mobile 1.1.0 RC2发布 源码+幻灯片:学习HTML5/jQuery/ASP.NET MVC/EF Code First的绝佳资源Account at a Glance项目 使用Autofac在ASP.NET Web API上实现依赖注入 在Windows Azure上开发ASP.NET程序与在Windows Sever上有何不同 充分利用缓存来提高网站性能 ASP.NET MVC 4, ASP.NET Web API, ASP.NET Web Pages v2 (Razor)全部开源,并接受来自社区的贡献(contributions) EF5 beta2通过NuGet发布 Getting Started with HTML5 开发HTML5应用你需要了解的 WCF入门资源 jquery history plugin, url hash Run Tasks in an ASP.NET Application Domain ASP.NET 2.0: 生成Excel报表 VS 2008 hot-fix终于出来了 Images; How to create an HTTP handler to dynamically resize images and change quality. ASP.NET 2.0: Add build-in paging feature to repeater/为repeater添加内置分页功能 Tip/Trick ASP.NET 2.0: DropDownList DataBind ASP.NET 2.0: Forms Authentication Across domains Ajax类库、框架、工具包完全列表 怀旧啊 Search Box ASP.NET 2.0: Site Maps Checking All CheckBoxes in a GridView Efficient Data Paging and Sorting with ASP.NET 2.0 and SQL 2005 JavaScript Calendar Cool MSDN ASP.NET 2.0 GridView Control Article Do I have to cry for you Google无法访问 扯淡 狗*! Atlas Control Toolkit and Source Code for the Build-in Asp.Net 2.0 Providers Great New Advanced Article on ASP.NET 2.0 Master Pages ASP.NET 2.0:通过SqlDataSource绑定数据到普通控件 通过客户端扩展实现固定GridView表头功能 不使用ISAPI或IIS wildcard实现不带扩展名URL的转向 VS 2005 Web Application Project & Atlas Control Toolkit & CSS Control Adapter
ASP.NET 2.0 Language Swithcer and Theme Swicher 多语言转换和多样式主题转换
阿福 · 2010-01-01 · via 博客园 - 阿福

在ASP.NET 2.0 中提供多语言转换和多样式主题转换功能,两种实现形式比较类似,所以放在一起说明一下。

1. Language switcher 多语言转换

Quick Start Tutorial中,介绍了如何存储和应用用户选择的语言。一般是用一个DropDownList展示支持的语言,供用户选择,通常是放在masterpage 里面,将用户选择的语言存储起来 这里用了ASP.NET 2.0的Profile,当然也可以存在cookie session 或者querystring里。在页面里重写InitializeCulture 方法,使用用户之前选择的语言。因为设置语言的操作 (这里是SelectedIndexChanged事件)发生在InitializeCulture 时间后面,所以在设置操作完成后为了使的当前页面也马上生效,需要做个重转向,以从新加载本页面,触发InitializeCulture 事件。下面使quickstart中的部分代码,注意红色部分。因为有的页面地址后面可能还存在queystring,所以个人觉得红色代码部分最好用Response.Redirect(Request.Url.PathAndQuery);代替。

    protectedvoid DropDownLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        string SelectedLanguage = DropDownLanguage.SelectedValue.ToString();
        //Save selected user language in profile
        Profile.SetPropertyValue("PreferredCulture", SelectedLanguage);

        //Force re-initialization of the page to fire InitializeCulture()
        Response.Redirect(Request.Url.LocalPath);
    }

    protectedoverridevoid InitializeCulture()
    {
        // override virtual method InitializeCulture() to check if profile contains a user language setting
        string UserCulture = Profile.GetPropertyValue("PreferredCulture").ToString();
        if ( UserCulture != "")
        {
            // there is a user language setting in the profile: switch to it
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(UserCulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(UserCulture);
        }
    }

为了减少代码的重复,一般会自定义一个customer base page类,使它继承Page类,然后在自定义的页基类中重新InitializeCulture方法。最后把你的每个页面继承自你的自定义页面基类。这样你就不需要每个页面都重写InitializeCulture方法了。

但是上面这个方法还是不是很爽,因为每添加一个页面都要去修改后置代码,来继承自定义页基类。

我们注意到,在InitializeCulture方法中实际上只是修改了当前线程的Culture和UICulture。那么可不可以在一个全局的事件中,比如Application的某个事件,来修改这两个属性呢?很早以前我这么试过,在Application的BeginRequest事件触发时来实现InitializeCulture 的细节,类似于下面代码:

    void Application_BeginRequest(object sender, EventArgs e)
    {
        string lang = string.Empty;//default to the invariant culture
        lang = Profile.PreferredCulture;
        if (string.IsNullOrEmpty(lang))
        {
            lang = string.Empty;
        }

        Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang);
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);

    }
注意红色部分应用其他方式取代,因为在beginrequest触发阶段,profile对象还没有被asp.net创建。可以用cookies取代。

我记得当时这么做后,语言设置后并不起作用,当时认为在全局事件中处理,可能到后来还是会被覆盖掉,所以可能不行。所以当时还是用了 InitializeCulture方法。今天在asp.net论坛里看到有人如此实现了,

void Application_BeginRequest(Object sender, EventArgs e){
      string lang =string.Empty;//default to the invariant culture
      HttpCookie cookie = Request.Cookies["DropDownName"];

      if (cookie !=null&& cookie.Value !=null)
         lang = Request.Form[cookie.Value];

      Thread.CurrentThread.CurrentUICulture =CultureInfo.GetCultureInfo(lang);
      Thread.CurrentThread.CurrentCulture =CultureInfo.CreateSpecificCulture(lang);
}

所以觉得当时可能哪里没有设置好,于是又试了一次,原来是页面头指令<%@ Page UICulture="auto" Culture="auto" %>的原因,如果在页面中设置了UICulture和Culture后,它们就会覆盖掉在全局中的设置。去掉之后,全局设置起作用了。看来页面中的culture的设置会覆盖全局的设置,而页面中InitializeCulture方法(确切说是一切支持该方法的控件)的设置会覆盖页面的设置。其实在Page类中InitializeCulture方法的默认实现是空的,因此再将页面头指令 UICulture="auto" Culture="auto" 去掉后,Global中的设置就起作用了。

另外,如果很想使用Profile(像我一样)来存储用户的选择,那就不能在beginrequest阶段来处理了,我是在PreRequestHandlerExecute事件触发时处理:

    void Application_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        string lang = string.Empty;//default to the invariant culture
  
        lang = Profile.PreferredCulture;
        if (string.IsNullOrEmpty(lang))
        {
            lang = string.Empty;
        }

        Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang);
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);

       }

这个时候Profile已经被创建了,所以可以使用了。

2. 多样式主题转换 Theme switcher

这篇文章讲了Theme的切换,觉得形式上和语言的切换很类似。他使用了HttpModule,我觉得直接放在Global.asax文件里对应的事件处理发放下就可以了,说到底都是一样的。他的存储采用了cookie,我还时觉得用Profile好,既然提供了就用呗,Profile应该是有缓存的吧,所以性能应该不是问题。