OK, thanks for the precision.
"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
Event handlers don't need to be explicitely released for them to be cleaned up. The only time you'd really remove a handler is if you don't want it to fire anymore but you kept your instance around. Your dispose() isn't bad, but it is unecessary.
Karl
--
http://www.openmymind.net/
"Lloyd Dupont" <net.galador@ld > wrote in message news:OG******** ******@TK2MSFTN GP12.phx.gbl...
damn....
You completely open my mind on that!
(interesting link on your web page, BTW)
I did like that
============== Module ==============
public class LangModule : IHttpModule
{
HttpApplication application;
public void Dispose()
{
if (application != null)
{
application.Beg inRequest -= HTranslate;
application = null;
}
}
public void Init(HttpApplic ation application)
{
this.applicatio n = application;
application.Beg inRequest += HTranslate;
}
public void HTranslate(obje ct sender, EventArgs e)
{
HttpApplication application = (HttpApplicatio n)sender;
HttpContext context = application.Con text;
string lang = context.Request["lang"];
if (lang != null && lang != "" && SetLocale(conte xt, lang))
return;
HttpCookie lc = context.Request .Cookies[LangCookieName];
if (lc == null)
return;
CultureInfo ci = new CultureInfo(lc. Value);
Thread.CurrentT hread.CurrentUI Culture = ci;
}
public static string LangCookieName = "UserSelectedLa nguage";
public static bool SetLocale(HttpC ontext context, string locale)
{
if (locale == null)
return false;
CultureInfo ci = null;
try { ci = new CultureInfo(loc ale); }
catch { return false; }
HttpCookie lc = new HttpCookie(Lang CookieName, locale);
lc.Expires = DateTime.Now.Ad dDays(360);
context.Respons e.Cookies.Add(l c);
Thread.CurrentT hread.CurrentUI Culture = ci;
return true;
}
}
========== WebControl ==============
<a href="?lang=en-AU"><img id="Img4" runat="server" src="~/datas/flag_au.gif" border="1" width="23" height="17" alt="English" /></a>
<a href="?lang=fr-FR" ><img id="Img5" runat="server" src="~/datas/flag_fr.gif" border="1" width="23" height="17" alt="Fran&ccedi l;ais" /></a>
=============== =============== ====
BTW, since there is a Dispose() method in the module it looks to me as an appropriate place to unregister the event handler, what do you think?
"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:e1******** ******@TK2MSFTN GP09.phx.gbl...
First, Init() happens once per module load, not per request. To get code working on each request, you need to write it like:
public void Init(HttpApplic ation application)
{
application.Beg inRequest += new EventHandler(ap plication_Begin Request);
}
void application_Beg inRequest(objec t sender, EventArgs e)
{
HttpApplication application = (HttpApplicatio n)sender;
//code here
}
Your real problem is timing. By the time the event handler fires, the thing's already been decided and switching the thread's culture doesn't do much.
If possible, I would suggest that you use links with querystring values, which can be read during BeginRequest.
I have an example that uses URL rewriting which could easily be modified to use querystrings:
http://openmymind.net/index.aspx?doc...d=4#urlrewrite
Karl
--
http://www.openmymind.net/
"Lloyd Dupont" <net.galador@ld > wrote in message news:ur******** ******@TK2MSFTN GP10.phx.gbl...
I created a
==============
public class LangModule : IHttpModule
{
public void Dispose() {}
public void Init(HttpApplic ation application)
{
HttpContext context = application.Con text;
HttpCookie lc = context.Request .Cookies[LangCookieName];
if (lc == null)
return;
CultureInfo ci = new CultureInfo(lc. Value);
Thread.CurrentT hread.CurrentCu lture = ci;
Thread.CurrentT hread.CurrentUI Culture = ci;
}
public static string LangCookieName = "UserSelectedLa nguage";
public static void SetLocale(HttpC ontext context, CultureInfo locale)
{
if (locale == null)
return;
HttpCookie lc = new HttpCookie(Lang CookieName, locale.Name);
lc.Expires = DateTime.Now.Ad dDays(360);
context.Respons e.Cookies.Add(l c);
Thread.CurrentT hread.CurrentCu lture = locale;
Thread.CurrentT hread.CurrentUI Culture = locale;
}
}
==============
SetLocale() is successfully called by a LinkButton to either "en-AU" or "fr-FR" and the Init() method is called when there is an incoming query.
I'm trying to test if it works with a very simple literal:
==============
<asp:Literal runat="server" Text="<%$ Resources: Standart, Email %>" />
==============
And in App_GlobalResou rces I have 2 resources: "Standart.r esx" & "Standart.f r-FR.resx".
However I always get the default (English) label, I never get the French label.
Any idea what I'm missing or I have done wrong?
--
I have taken a vow of poverty. If you want to really piss me off, send me money.