Page.InitializeCulture Method
Namespace: System.Web.UI
Assembly: System.Web (in System.Web.dll)
1
protected virtual void InitializeCulture()2

3

How to: asp.net web localization by extending Page class.
部分代码:
1
using System;2
using System.Configuration;3
using System.Web;4
using System.Globalization;5
using System.Threading; 6
public class Page : System.Web.UI.Page7

{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 settings14
// section to define15
// page defaults and available settings.16

17
// To start we need a CultureInfo object constructed from a default language18
// 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 use25
// cookies.26
HttpCookie cookie = Request.Cookies.Get("lang");27

28
// Setting up a cookie to expire in a custom-defined time frame29
// (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 variables37
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
else52

{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

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

浙公网安备 33010602011771号