A question on ASP.net localization

J

JollyK

Hi everyone...

I am creating a web app that will support english and french languages.

My code is fairly simple and it working fine. In my global.asax file I have
the
following code in the Application_BeginRequest event.

try
{
Thread.CurrentThread.CurrentCulture = new
CultureInfo(Request.UserLanguages[0]);
}
catch(Exception)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
}

I have a resource file for french and a resource file for english, and if
the browser langauge
is set to french, then french info is displayed, and if the browser
langauage is set to english,
then english is displayed. So far everything is perfectly.
My issue is, suppose the browser is set to Arabic, or Italian, or any other
langauge, I want to
default it to English. How do I do this ?
 
M

Martin Dechev

Hi, JollyK,

It is the CurrentUICulture property on the Thread class that is taken in
consideration by the Resource Manager to look up culture-specific resources
at run time. If you replace CurrentCulture with CurrentUICulture in the code
you posted, and if everything else works, you should be set.

Greetings
Martin
 
B

bruce barker

this code will have problems because of asp.net thread agility. the same
thread is not necessarily used for a page process, so if a thread switch is
done, you will have the wrong culture.

you need to set the thread culture before any calls that need it (you should
also restore it, before the threads returned to the pool)

-- bruce (sqlwork.com)
 
J

JollyK

Hi Bruce,
How about an example


bruce barker said:
this code will have problems because of asp.net thread agility. the same
thread is not necessarily used for a page process, so if a thread switch is
done, you will have the wrong culture.

you need to set the thread culture before any calls that need it (you should
also restore it, before the threads returned to the pool)

-- bruce (sqlwork.com)




JollyK said:
Hi everyone...

I am creating a web app that will support english and french languages.

My code is fairly simple and it working fine. In my global.asax file I have
the
following code in the Application_BeginRequest event.

try
{
Thread.CurrentThread.CurrentCulture = new
CultureInfo(Request.UserLanguages[0]);
}
catch(Exception)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
}

I have a resource file for french and a resource file for english, and if
the browser langauge
is set to french, then french info is displayed, and if the browser
langauage is set to english,
then english is displayed. So far everything is perfectly.
My issue is, suppose the browser is set to Arabic, or Italian, or any other
langauge, I want to
default it to English. How do I do this ?
 
M

Martin Dechev

Hi, bruce baker,

Could you prove your point with some documentation?

There is a tutorial that in fact shows how to set the CurrentUICulture in
the Application_BeginRequest handler:

http://msdn.microsoft.com/library/en-us/cptutorials/html/resourcemanager_and_asp_net.asp

I personally don't see a situation in which more than one thread could be
used in the execution of a Page class given the used model for the page
lifecycle.

Greetings
Martin
bruce barker said:
this code will have problems because of asp.net thread agility. the same
thread is not necessarily used for a page process, so if a thread switch is
done, you will have the wrong culture.

you need to set the thread culture before any calls that need it (you should
also restore it, before the threads returned to the pool)

-- bruce (sqlwork.com)




JollyK said:
Hi everyone...

I am creating a web app that will support english and french languages.

My code is fairly simple and it working fine. In my global.asax file I have
the
following code in the Application_BeginRequest event.

try
{
Thread.CurrentThread.CurrentCulture = new
CultureInfo(Request.UserLanguages[0]);
}
catch(Exception)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
}

I have a resource file for french and a resource file for english, and if
the browser langauge
is set to french, then french info is displayed, and if the browser
langauage is set to english,
then english is displayed. So far everything is perfectly.
My issue is, suppose the browser is set to Arabic, or Italian, or any other
langauge, I want to
default it to English. How do I do this ?
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top