How to: asp.net web localization by extending Page class.
部分代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
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.Page
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
8
// Override InitializeCulture method to provide integrated localization support.
9
protected override void InitializeCulture()
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
11
base.InitializeCulture();
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
// For this example, we will use a local web.config application settings
14
// section to define
15
// page defaults and available settings.
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
// To save permanent information about user language selection, we will use
25
// cookies.
26
HttpCookie cookie = Request.Cookies.Get("lang");
27![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
39
selectedCulture = new CultureInfo(Request.QueryString["lang"]);
40
41
// We will also write a cookie to remember our selection.
42![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
selectedCulture = new CultureInfo(cookie.Value);
49
}
50
// If for any reason both methods fail, fall to default settings.
51
else
52![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
53
// Just write a cookie to save default option.
54![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
cookie = new HttpCookie("lang", selectedCulture.Name)
{ Expires = cookieExpiration };
55
Response.Cookies.Add(cookie);
56
}
57![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
// Apply selected language to Page culture.
59
Thread.CurrentThread.CurrentUICulture = selectedCulture;
60
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedCulture.Name);
61![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
}
63
}
64![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
65![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
66![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
67![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
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:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
foreach (
2
string language in ConfigurationManager.AppSettings["Localization_AvailableLanguages"].Split(','))
3![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
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/Images/OutliningIndicators/None.gif)