Groups | Blog | Home
all groups > asp.net > january 2006 >

asp.net : where to set the current culture in ASP.NET 2.0?


Phillip Williams
1/15/2006 12:00:03 PM
Why not use the user's browser settings upon each postback instead? You can
set Culture="auto" UICulture="auto" on the page directive, e.g.:
http://www.webswapp.com/CodeSamples/aspnet20/AutoCulture.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


[quoted text, click to view]
Phillip Williams
1/15/2006 8:06:02 PM
If you add Culture="auto" UICulture="auto" to the page directive and an
American user logs when his/her browser setting is “en-US” then they would
not need to select the language of their preference unless they want to
switch to a language that is not in the first selection in their browser
setting. You can then add a link that says “Select language of preference”
which would save the selection in a cookie and upon next visit the master
page would simply execute Page.Culture=selectedCulture if there were a
cookie. This would last longer than using the Session object which expires
when the browser is closed or the Session expired (in which scenario the user
has to select the culture upon every visit)
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


[quoted text, click to view]
Edge
1/15/2006 11:04:36 PM
hi,
I am saving the user selected culture in a session variable so I can apply
it back to all pages when refreshed and then load the proper .resx values.
For that I am using global.asax

Edge
1/15/2006 11:10:28 PM
hi,
I am saving the user selected culture in a session variable so I can apply
it back to all pages when refreshed and then load the proper .resx values.
For that I am using global.asax, Application_AcquireRequestState, because
every page load will execute this method.
Even thou it works, very often I get en exception telling that the session
can not be retrieved in this context.

What's the best place to actually place this verification in order to be
reflected in every page of my application?
here the global.asax code

public void Application_AcquireRequestState(object sender, EventArgs e)
{

if ( Session["myculture"] != null )
{
string currentCulture = (string)Session["myculture"];
if (String.Compare(currentCulture,
System.Threading.Thread.CurrentThread.CurrentCulture.ToString(),
StringComparison.OrdinalIgnoreCase) != 0)
{
try
{
System.Threading.Thread.CurrentThread.CurrentCulture =
System.Globalization.CultureInfo.CreateSpecificCulture(currentCulture);
}
catch
{
System.Threading.Thread.CurrentThread.CurrentCulture =
new System.Globalization.CultureInfo("en-us");
}
System.Threading.Thread.CurrentThread.CurrentUICulture =
System.Threading.Thread.CurrentThread.CurrentCulture;
}
}
}

Phillip Williams
1/16/2006 12:58:03 AM

In the AcquireRequestState stage, the SessionStateModule attempts to extract
the session ID from the request and retrieve the session data for that
session ID. You would get the error message "Session State is not available
in this Context" if the SessionStateModule failed in this process.

You can verify this info by creating a blank solution with one page marked
with EnableSessionState="false" in the page directive and place your code in
the gloabl.asax.

So if you are setting the culture using the Session object, make sure that
none of your webpages (or the web.config settings) disables the Session state
otherwise your code in the global.asax will continue to catch that exception.

The web.config can also disable the Session state like this:
<configuration>
<system.web>
<pages enableSessionState="false" />
</system.web>
</configuration>

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


[quoted text, click to view]
michaelcoleman72 NO[at]SPAM gmail.com
1/16/2006 6:29:41 AM
I have recently done the same thing in 2.0. We have custom
implementation of the Membership provider. The MembershipUser has been
extended to support various custom properties. One is the culture
name. When the user logs in, the culture is associated with their
login id. In the page (or if you create a base page and derive your
pages from it making it even easier) override the "protected virtual
void InitializeCulture ()" method and pull from the membership user
first putting the value in the session variable. The initializeculture
is called very early in the pages lifecycle so your controls should
render the correct culture.

Regards
Coleman
Edge
1/16/2006 9:12:32 AM
hi Phillip,
because for example, in a chinese cybercafe I want a american user to login
in our system and be able to choose his preferred language just once and
then this selection to be applied in all the other pages.
I tried your suggestion but, I do not want to put in every single page code
like Page.Culture = "en-us".

-E




[quoted text, click to view]

Edge
1/16/2006 2:57:10 PM
hi Phillip,

Thanks for the reply.
I should not put this in a cookie, some browsers/users might not
support/want it and I decided not to used. So I load a session variable with
the user language that I get from the DB after his login.
Right now, this is working in my global.asax file, but I get an exception
(but the application won't break) when the user hits the first page, than
after that is all good.

So...the problem still persists, how can I set the current culture for a
logged user without having to set in every single page the page culture?


-E


[quoted text, click to view]

Edge
1/17/2006 10:05:41 AM
hi michael,
in fact I just started doing this, I am creating a page base for that. but I
won't use the membership provider or profile object.
Meanwhile I will do some more research in the global.asax file, there must
be something in there:)

TIA
-E

[quoted text, click to view]

michaelcoleman72 NO[at]SPAM gmail.com
1/17/2006 1:01:06 PM
Edge - Try adding a handler to the following PostAcquireRequestState
event in the global.asax.

[quoted text, click to view]

HttpApplication.PostAcquireRequestState Event

Note: This event is new in the .NET Framework version 2.0.

Occurs when the request state (for example, session state) that is
associated with the current request has been obtained.

Regards
Coleman
AddThis Social Bookmark Button