您的位置:首页 > 产品设计 > UI/UE

Use the Profile class to store and set the UI culture

2004-09-21 18:58 603 查看
I got some questions regarding how to use the Profile class to set the current UI culture on each request of a page. In the current version of ASP.Net 1.x, we could use the BeginRequest even in global.asax to set the UI culture. By doing that all pages will use the culture specified in the BeginRequest event. This solution still works in ASP.Net 2.0. But if you have a property in the Profile class that has the culture to be set, the BeginRequest could not be used. The Profile class is not available at that stage.

The Profile feature uses an HttpModule (ProfileModule) to get the profile data for the current user or the default value specified in the web.config.

The ProfileModule will make the Profile class available when the Application’s AcquireRequestState event is executed. The AcquireRequestState is executed on each request of a page.

Note: The AcquireRequestState will be executed after the Profile’s MigrateAnonymous event. So if you change the property of the Profile class in the MigrateAnuinbymouse event when a user is authenticated, the new values will be available in the AcquireRequestState event.

Instead of using the BeginRequest event you can now use the AcquireRequestState event to set the UI culture before the requested page is processed (If you want to store the user’s culture into the Profile class). If the user is not authenticated and you have enabled the support of anonymous users, the default value specified for the properties of the profile will be available for the anonymous user at the AcquireRequestState event.

The following is a simple example that will demonstrate the use of the Profile class and the AcquiredRequestState event to set the UI culture on each request:

Web.config:

<?xml version="1.0"?>
<configuration>
<system.web>
<authentication mode="Forms" />
<profile>
<properties>
<add name="Culture" defaultValue="en-US" type="System.String" allowAnonymous="true"/>
</properties>
</profile>
<anonymousIdentification enabled="true"/>
</system.web>
</configuration>

As you can see in the web.config above there is a default value for the Culture property. When the use is not authenticated the default value specified for the Culture property will be used to set the UI culture.

Global.asax:

void Application_AcquireRequestState(object sender, EventArgs e)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Profile.Culture);
}

The above code uses the AcquireRequestState event to set the UI culture on each request. The value form the Profile’s Culture property would be the culture. As I mention before, the DeafultValue (en-US in this case) will be used if the user is not authenticated.

If you now authentiacte the user and set a new value to the Profile’s Culture property, e.g. sv-SE for Swedish, the next request made to any of the pages in your web application will use the new Culture for the UI.

Here is an example where the Culture property is set after a user is authenticated:

if (!Request.IsAuthenticated)
{
FormsAuthentication.SetAuthCookie("fredrik", false);
Profile.Culture = "sv-SE";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐