Board index » Web Programming » trying to change the culture in an IHttpModule
|
TerryAnnThomas
|
trying to change the culture in an IHttpModule
Web Programming322
This is a multi-part message in MIME format. ------=_NextPart_000_0232_01C61470.38996EF0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable I'm trying to write a multilingual web site where the user could choose = his language explicitely. I have 2 flag, the user could click on the flag to change the culture. The core of the logic goes in an HttpModule: =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D public class LangModule : IHttpModule { public void Dispose() {} public void Init(HttpApplication application) { HttpContext context =3D application.Context; HttpCookie lc =3D context.Request.Cookies[LangCookieName]; if (lc =3D=3D null) return; CultureInfo ci =3D new CultureInfo(lc.Value); Thread.CurrentThread.CurrentCulture =3D ci; Thread.CurrentThread.CurrentUICulture =3D ci; } public static string LangCookieName =3D "UserSelectedLanguage"; public static void SetLocale(HttpContext context, CultureInfo = locale) { if (locale =3D=3D null) return; HttpCookie lc =3D new HttpCookie(LangCookieName, locale.Name); lc.Expires =3D DateTime.Now.AddDays(360); context.Response.Cookies.Add(lc); Thread.CurrentThread.CurrentCulture =3D locale; Thread.CurrentThread.CurrentUICulture =3D locale; } } =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D However that doesn't seems to work, when page are loaded they are = Page.Culture is always en-AU. Any idea why is it so? --=20 I have taken a vow of poverty. If you want to really piss me off, send = me money. ------=_NextPart_000_0232_01C61470.38996EF0 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD> <META http-equiv=3DContent-Type content=3D"text/html; = charset=3Diso-8859-1"> <META content=3D"MSHTML 6.00.2900.2802" name=3DGENERATOR> <STYLE></STYLE> </HEAD> <BODY> <DIV>I'm trying to write a multilingual web site where the user could = choose his=20 language explicitely.</DIV> <DIV>I have 2 flag, the user could click on the flag to change the=20 culture.</DIV> <DIV>The core of the logic goes in an HttpModule:</DIV> <DIV>=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D</DIV> <DIV><FONT size=3D2><FONT color=3D#0000ff>public</FONT><FONT=20 color=3D#0000ff>class</FONT><FONT color=3D#008080>LangModule</FONT>: = </FONT><FONT=20 size=3D2><FONT color=3D#008080>IHttpModule<BR></FONT>{<BR><FONT=20 color=3D#0000ff> public</FONT><FONT = color=3D#0000ff>void</FONT>=20 Dispose() {}<BR><BR><FONT color=3D#0000ff> = public</FONT><FONT=20 color=3D#0000ff>void</FONT>Init(<FONT = color=3D#008080>HttpApplication</FONT>=20 application)<BR> {<BR><FONT = color=3D#008080> =20 HttpContext</FONT>context =3D = application.Context;<BR><FONT=20 color=3D#008080> HttpCookie</FONT>= lc =3D=20 context.Request.Cookies[LangCookieName];<BR><FONT=20 color=3D#0000ff> if</FONT>(lc = =3D=3D <FONT=20 color=3D#0000ff>null</FONT>)<BR><FONT color=3D#0000ff> = return</FONT>;<BR><BR><FONT=20 color=3D#008080> CultureInfo</FONT>= ci =3D=20 <FONT color=3D#0000ff>new</FONT><FONT=20 color=3D#008080>CultureInfo</FONT>(lc.Value);<BR><FONT=20 color=3D#008080> =20 Thread</FONT>.CurrentThread.CurrentCulture =3D ci;<BR><FONT=20 color=3D#008080> =20 Thread</FONT>.CurrentThread.CurrentUICulture =3D = ci;<BR> =20 }<BR><FONT color=3D#0000ff> public</FONT><FONT=20 color=3D#0000ff>static</FONT><FONT color=3D#0000ff>string</FONT>= LangCookieName =3D=20 <FONT color=3D#800000>"UserSelectedLanguage"</FONT>;<BR><FONT=20 color=3D#0000ff> public</FONT><FONT = color=3D#0000ff>static</FONT>=20 <FONT color=3D#0000ff>void</FONT>SetLocale(<FONT = color=3D#008080>HttpContext</FONT>=20 context, <FONT color=3D#008080>CultureInfo</FONT>= locale)<BR> =20 {<BR><FONT color=3D#0000ff> = if</FONT>(locale=20 =3D=3D <FONT color=3D#0000ff>null</FONT>)<BR><FONT = color=3D#0000ff> =20 = return</FONT>;<BR> =20 <FONT color=3D#008080>HttpCookie</FONT>lc =3D <FONT=20 color=3D#0000ff>new</FONT><FONT = color=3D#008080>HttpCookie</FONT>(LangCookieName,=20 locale.Name);<BR> lc.Expires =3D = <FONT=20 color=3D#008080>DateTime</FONT>.Now.AddDays(360);<BR> =20 context.Response.Cookies.Add(lc);<BR><FONT=20 color=3D#008080> =20 Thread</FONT>.CurrentThread.CurrentCulture =3D locale;<BR><FONT=20 color=3D#008080> =20 Thread</FONT>.CurrentThread.CurrentUICulture =3D = locale;<BR> =20 }<BR>}<BR></FONT>=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D</DIV> <DIV><FONT face=3DArial size=3D2>However that doesn't seems to work, = when page are=20 loaded they are Page.Culture is always en-AU.</FONT></DIV> <DIV><FONT face=3DArial size=3D2>Any idea why is it so?</FONT></DIV> <DIV><FONT face=3DArial size=3D2></FONT><BR>-- <BR>I have taken a vow of = poverty. If=20 you want to really piss me off, send me money.<BR></DIV></BODY></HTML> ------=_NextPart_000_0232_01C61470.38996EF0-- - |
