A question on ASP.net localization

  • Thread starter Thread starter JollyK
  • Start date Start date
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 ?
 
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
 
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)
 
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 ?
 
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 ?
 
Back
Top