Page.InitializeCulture Method

本文介绍如何通过覆盖InitializeCulture方法实现ASP.NET页面的本地化支持。文章详细解释了使用配置文件设置默认语言、处理用户选择的语言并保存到Cookies的方法。

Sets the Culture and UICulture for the current thread of the page.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

Code
1protected virtual void InitializeCulture()
2
3

How to: asp.net web localization by extending Page class.
部分代码:
Code
 1using System;
 2using System.Configuration;
 3using System.Web;
 4using System.Globalization;
 5using System.Threading;   
 6public class Page : System.Web.UI.Page
 7    {
 8        // Override InitializeCulture method to provide integrated localization support.
 9        protected override void InitializeCulture()
10        {
11            base.InitializeCulture();
12
13            // For this example, we will use a local web.config application settings
14            // section to define
15            // page defaults and available settings.
16
17            // To start we need a CultureInfo object constructed from a default language
18            // selection.
19            // You could also contruct a specialized section or a private configuration 
20            // file.
21            CultureInfo selectedCulture = new CultureInfo(ConfigurationManager.
22                                          AppSettings["Localization_DefaultLanguage"]);
23
24            // To save permanent information about user language selection, we will use
25            // cookies.
26            HttpCookie cookie = Request.Cookies.Get("lang");
27
28            // Setting up a cookie to expire in a custom-defined time frame
29            // (also defined in web.config).
30            DateTime cookieExpiration = DateTime.Now.AddDays(
31                                          Convert.ToInt32(
32                                            ConfigurationManager.
33                             AppSettings["Localization_LanguageCookieExpirationInDays"]));
34
35            // Now, we will check for explicit query string language selection.
36            //  This way we enable users to change language using url variables
37            if (Request.QueryString["lang"!= null)
38            {
39                selectedCulture = new CultureInfo(Request.QueryString["lang"]);
40                
41                // We will also write a cookie to remember our selection.
42                cookie = new HttpCookie("lang", selectedCulture.Name) { Expires = cookieExpiration };
43                Response.Cookies.Add(cookie);
44            }

45            // If no explicit selection is found, use the one saved in a cookie.
46            else if (cookie != null)
47            {
48                selectedCulture = new CultureInfo(cookie.Value);
49            }

50            // If for any reason both methods fail, fall to default settings.
51            else
52            {
53                // Just write a cookie to save default option.
54                cookie = new HttpCookie("lang", selectedCulture.Name) { Expires = cookieExpiration };
55                Response.Cookies.Add(cookie);
56            }

57
58            // Apply selected language to Page culture.
59            Thread.CurrentThread.CurrentUICulture = selectedCulture;
60            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedCulture.Name);
61
62        }

63    }

64
65
66
67

Code
1<appSettings>
2        <add key="Localization_AvailableLanguages" value="en-gb,en-us,hr,it,fr,de,es,ru"/>
3        <add key="Localization_DefaultLanguage" value="fr"/>
4        <add key="Localization_LanguageCookieExpirationInDays" value="5"/>
5</appSettings> 

The menu will be created using a simple inline foreach loop:


 
Code
1  foreach (
2              string language in ConfigurationManager.AppSettings["Localization_AvailableLanguages"].Split(','))
3            {
4                Response.Write(
5                    string.Format(
6                          "<li><a href='{0}?lang={1}'>{2}</a></li>",
7                          Request.Path, language, new CultureInfo(language).NativeName));
8            }

9

转载于:https://www.cnblogs.com/onlyendure/archive/2008/07/09/1238592.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值