473,394 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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
Jan 15 '06 #1
9 3892
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.CurrentCultu re.ToString(),
StringComparison.OrdinalIgnoreCase) != 0)
{
try
{
System.Threading.Thread.CurrentThread.CurrentCultu re =
System.Globalization.CultureInfo.CreateSpecificCul ture(currentCulture);
}
catch
{
System.Threading.Thread.CurrentThread.CurrentCultu re =
new System.Globalization.CultureInfo("en-us");
}
System.Threading.Thread.CurrentThread.CurrentUICul ture =
System.Threading.Thread.CurrentThread.CurrentCultu re;
}
}
}
Jan 15 '06 #2
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/...toCulture.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Edge" wrote:
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.CurrentCultu re.ToString(),
StringComparison.OrdinalIgnoreCase) != 0)
{
try
{
System.Threading.Thread.CurrentThread.CurrentCultu re =
System.Globalization.CultureInfo.CreateSpecificCul ture(currentCulture);
}
catch
{
System.Threading.Thread.CurrentThread.CurrentCultu re =
new System.Globalization.CultureInfo("en-us");
}
System.Threading.Thread.CurrentThread.CurrentUICul ture =
System.Threading.Thread.CurrentThread.CurrentCultu re;
}
}
}

Jan 15 '06 #3
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" <Ph**************@webswapp.com> wrote in message
news:43**********************************@microsof t.com...
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/...toCulture.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Edge" wrote:
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.CurrentCultu re.ToString(),
StringComparison.OrdinalIgnoreCase) != 0)
{
try
{
System.Threading.Thread.CurrentThread.CurrentCultu re
=
System.Globalization.CultureInfo.CreateSpecificCul ture(currentCulture);
}
catch
{
System.Threading.Thread.CurrentThread.CurrentCultu re
=
new System.Globalization.CultureInfo("en-us");
}
System.Threading.Thread.CurrentThread.CurrentUICul ture =
System.Threading.Thread.CurrentThread.CurrentCultu re;
}
}
}

Jan 16 '06 #4
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" wrote:
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" <Ph**************@webswapp.com> wrote in message
news:43**********************************@microsof t.com...
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/...toCulture.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Edge" wrote:
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.CurrentCultu re.ToString(),
StringComparison.OrdinalIgnoreCase) != 0)
{
try
{
System.Threading.Thread.CurrentThread.CurrentCultu re
=
System.Globalization.CultureInfo.CreateSpecificCul ture(currentCulture);
}
catch
{
System.Threading.Thread.CurrentThread.CurrentCultu re
=
new System.Globalization.CultureInfo("en-us");
}
System.Threading.Thread.CurrentThread.CurrentUICul ture =
System.Threading.Thread.CurrentThread.CurrentCultu re;
}
}
}


Jan 16 '06 #5
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" <Ph**************@webswapp.com> wrote in message
news:0B**********************************@microsof t.com...
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" wrote:
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" <Ph**************@webswapp.com> wrote in message
news:43**********************************@microsof t.com...
> 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/...toCulture.aspx
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "Edge" wrote:
>
>> 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.CurrentCultu re.ToString(),
>> StringComparison.OrdinalIgnoreCase) != 0)
>> {
>> try
>> {
>>
>> System.Threading.Thread.CurrentThread.CurrentCultu re
>> =
>> System.Globalization.CultureInfo.CreateSpecificCul ture(currentCulture);
>> }
>> catch
>> {
>>
>> System.Threading.Thread.CurrentThread.CurrentCultu re
>> =
>> new System.Globalization.CultureInfo("en-us");
>> }
>> System.Threading.Thread.CurrentThread.CurrentUICul ture
>> =
>> System.Threading.Thread.CurrentThread.CurrentCultu re;
>> }
>> }
>> }
>>
>>
>>


Jan 16 '06 #6

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" wrote:
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" <Ph**************@webswapp.com> wrote in message
news:0B**********************************@microsof t.com...
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" wrote:
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" <Ph**************@webswapp.com> wrote in message
news:43**********************************@microsof t.com...
> 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/...toCulture.aspx
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "Edge" wrote:
>
>> 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.CurrentCultu re.ToString(),
>> StringComparison.OrdinalIgnoreCase) != 0)
>> {
>> try
>> {
>>
>> System.Threading.Thread.CurrentThread.CurrentCultu re
>> =
>> System.Globalization.CultureInfo.CreateSpecificCul ture(currentCulture);
>> }
>> catch
>> {
>>
>> System.Threading.Thread.CurrentThread.CurrentCultu re
>> =
>> new System.Globalization.CultureInfo("en-us");
>> }
>> System.Threading.Thread.CurrentThread.CurrentUICul ture
>> =
>> System.Threading.Thread.CurrentThread.CurrentCultu re;
>> }
>> }
>> }
>>
>>
>>


Jan 16 '06 #7
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

Jan 16 '06 #8
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

<mi**************@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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

Jan 17 '06 #9
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

Jan 17 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Manu | last post by:
I have created a new culture : Dim objCulture As New CultureInfo("he") //hebrew When I tried to assign it to the current thread. Thread.CurrentThread.CurrentCulture = objCulture...
15
by: Mark Kamoski | last post by:
Hi Everyone-- Please help. How can one get the name of the current project and the current class? This is the situation. Suppose there is a project called "P1".
7
by: James P. | last post by:
Hello there, In my asp.net page using VB, I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is greater than or equal to the...
5
by: Stephanie Stowe | last post by:
Imagine that I had this <webServices> <soapExtensionTypes> <add type="Microsoft.Web.Services.WebServicesExtension, Microsoft.Web.Services, Version=1.0.0.0, Culture=neutral,...
3
by: cmrchs | last post by:
Hi, I try to obtain the current culture of a server by executing : string str = Thread.CurrentThread.CurrentCulture.ToString(); Executing this in a console-applicastion results in the...
4
by: Greg Allen | last post by:
I have a web service and am trying to use WSE 2.0 to pass attachments. I have enabled both the checkboxes in the WSE properties page in VS. The problem occurs when I try to get the...
1
by: Joseph Geretz | last post by:
Why isn't the ASP.NET runtime providing a valid reference for this object to my WebService? I'm trying to use WSE and DIME to send attachments via Web Service. I've provided a whole lot of...
4
by: adi | last post by:
Hi I'm using .NET Framework 1.1 My application needs to read the system's language settings. How to do this? Thanks.
2
by: tamirmalas | last post by:
Hi, iam trying to set a culture info different than the current culture info on the system, the code compiles fine, but nothing happens, and the system culture is the one used for the program. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.