How to keep a session for Localization & Globalization?
i would like to know how to keep a session or how to make the browser remember which language should the application keep while navigating and moving from a page to another should i recall the overrided methode :
protected override void InitializeCulture()
{
base.InitializeCulture();
string cult = Request["lstLanguage"];
if (cult != null)
{
Culture = cult;
UICulture = cult;
}
}开发者_开发百科
i've tried :
Session["cult"] = cult;
but it does not work more informations there is a a drop down list at the home page which make the user choose the language. what should i do ? thanks
You can keep the selected culture in a cookie. That way, when the user returns to your site the culture will be "remembered".
Using Session will mean the user has to keep choosing the culture when they return - not good.
You can access the site's cookies with the Request object:
Request.Cookies["culture"].Value = chosenCultureCode;
You need to store the users specified Culture in the session when the user first logs into the system. Then you can use it in your InitializeCulture methods.
eg. when login
Session["CurrentCulture"] = (Your users chosen culture)
and in your override for InitializeCulture you would retrieve with:
Session["CurrentCulture"].
Here is an example of one of my methods.
/// <summary>
/// Initializes culture for the page
/// </summary>
[VersionChange( "6.1.34.89", "24/12/2009", "Custom Cultures added" )]
protected override void InitializeCulture()
{
try
{
CultureInfo oCultureInfo;
try
{
oCultureInfo = CultureInfo.CreateSpecificCulture( this.CurrentCustomCulture );
}
catch ( ArgumentException )
{
//Get culture info based on Great Britain
CultureInfo cultureInfo = new CultureInfo( "en-GB" );
RegionInfo regionInfo = new RegionInfo( cultureInfo.Name );
CultureAndRegionInfoBuilder cultureAndRegionInfoBuilder = new CultureAndRegionInfoBuilder( this.CurrentCustomCulture, CultureAndRegionModifiers.None );
cultureAndRegionInfoBuilder.LoadDataFromCultureInfo( cultureInfo );
cultureAndRegionInfoBuilder.LoadDataFromRegionInfo( regionInfo );
// Custom Changes
cultureAndRegionInfoBuilder.CultureEnglishName = this.CurrentCustomCulture;
cultureAndRegionInfoBuilder.CultureNativeName = this.CurrentCustomCulture;
cultureAndRegionInfoBuilder.Register();
oCultureInfo = CultureInfo.GetCultureInfo( this.CurrentCustomCulture );
}
catch ( Exception )
{
throw;
}
Thread.CurrentThread.CurrentCulture = oCultureInfo;
Thread.CurrentThread.CurrentUICulture = oCultureInfo;
Page.Culture = oCultureInfo.Name;
Page.UICulture = oCultureInfo.Name;
base.InitializeCulture();
}
catch ( Exception )
{
throw;
}
}
This.CurrentCustomCulture is my session property
精彩评论