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

E

Edge

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
 
E

Edge

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;
}
}
}
 
E

Edge

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




Phillip Williams said:
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


Edge said:
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;
}
}
}
 
G

Guest

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


Edge said:
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




Phillip Williams said:
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


Edge said:
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;
}
}
}
 
E

Edge

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


Phillip Williams said:
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


Edge said:
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




Phillip Williams said:
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


:

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;
}
}
}
 
G

Guest

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


Edge said:
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


Phillip Williams said:
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


Edge said:
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




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


:

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;
}
}
}
 
M

michaelcoleman72

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
 
E

Edge

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
 
M

michaelcoleman72

Edge - Try adding a handler to the following PostAcquireRequestState
event in the global.asax.
From the doc's:

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top