Set language for multilingual site from user control
Hi All,
I'm using Umbraco 4.7. I have site which contains 95% of logic in user controls and 5% actually content. My localization strategy for user controls based on http://www.nibble.be/?p=14, but a little bit extended. I also wrote language selector user control and placed it on master template. Main task of language selector control is depending on user choise set the UI culture:
And it works fine for content in user controls. Just fill dictionary, create new languages, make translation and viola. But I faced with problem of localizing umbraco items. For example on the same master template page I have following code:
And I have appropriate record in dictionary. And when user change language on front-end nothing happens. UI culture set correctly by language selector user control, but Umbraco doesn't show me appropriate dictionary record for selected language. So what I'm doing wrong?
P.S. I notice that if I use manage hostnames on my global homepage and add new domain with for example Danish language, then when I open site Umbraco gets appropriate item from dictionary for Danish language. So when I delete Danish language domain and add English domain, Umbraco gets item for English language. IU want such behaviour from my language selector user control and actualy I thought that Umbraco use UI culture set by my control in right way, but it seems I was wrong. Most crazy idea that I have right now is to delete previous and make new domain in Umbraco on gloabal homepage node each time when user switch language.But I balive you have a better solution :)
Where, in what method, do you set the CultureInfo and UICultureInfo on the thread?
As far as I remember, Umbraco doesn't actually support front-end visitors to set the language, meaning that somewhere in the request to a page, Umbraco set the CultureInfo based on the hostheader settings.
So if you need to override this, you'll have to do it after Umbraco has set it, but early on in the request, before the different macros, user controls, etc. start rendering - I'm not really sure it's early enough in the request, but maybe in the OnInit method of a user control will do it?
I did some trick based on your input and it seems it works. I added a little httpModule in Umbraco bin folder which checks cookie with user language and set thread UI culture:
public class TMSLocalizationModule : IHttpModule
{
#region IHttpModule Members
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
In my language selector user control I have dropdown list with list of installed on Umbraco languages. When user choose one, I set cookie for my httpModule:
Hi I am new to umbraco and I have a question regarding your solution. How do you get the httpModule to work with umbraco is it enought to do " I added a little httpModule in Umbraco bin folder" or do i need to do some thing else too?
Set language for multilingual site from user control
Hi All,
I'm using Umbraco 4.7. I have site which contains 95% of logic in user controls and 5% actually content. My localization strategy for user controls based on http://www.nibble.be/?p=14, but a little bit extended. I also wrote language selector user control and placed it on master template. Main task of language selector control is depending on user choise set the UI culture:
And it works fine for content in user controls. Just fill dictionary, create new languages, make translation and viola. But I faced with problem of localizing umbraco items. For example on the same master template page I have following code:
And I have appropriate record in dictionary. And when user change language on front-end nothing happens. UI culture set correctly by language selector user control, but Umbraco doesn't show me appropriate dictionary record for selected language. So what I'm doing wrong?
P.S. I notice that if I use manage hostnames on my global homepage and add new domain with for example Danish language, then when I open site Umbraco gets appropriate item from dictionary for Danish language. So when I delete Danish language domain and add English domain, Umbraco gets item for English language. IU want such behaviour from my language selector user control and actualy I thought that Umbraco use UI culture set by my control in right way, but it seems I was wrong. Most crazy idea that I have right now is to delete previous and make new domain in Umbraco on gloabal homepage node each time when user switch language.But I balive you have a better solution :)
Where, in what method, do you set the CultureInfo and UICultureInfo on the thread?
As far as I remember, Umbraco doesn't actually support front-end visitors to set the language, meaning that somewhere in the request to a page, Umbraco set the CultureInfo based on the hostheader settings.
So if you need to override this, you'll have to do it after Umbraco has set it, but early on in the request, before the different macros, user controls, etc. start rendering - I'm not really sure it's early enough in the request, but maybe in the OnInit method of a user control will do it?
Hmm.
I did some trick based on your input and it seems it works. I added a little httpModule in Umbraco bin folder which checks cookie with user language and set thread UI culture:
public class TMSLocalizationModule : IHttpModule
{
#region IHttpModule Members
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
public void Dispose() { }
#endregion
void context_BeginRequest(object sender, EventArgs e)
{
HttpRequest request = ((HttpApplication)sender).Request;
HttpContext context = ((HttpApplication)sender).Context;
string lang = string.Empty;
HttpCookie cookie = request.Cookies["TMSUserLanguage"];
if (cookie != null && !String.IsNullOrEmpty(cookie.Value))
lang = cookie.Value;
if (!String.IsNullOrEmpty(lang))
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
}
}
}
In my language selector user control I have dropdown list with list of installed on Umbraco languages. When user choose one, I set cookie for my httpModule:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie(COOKIE_NAME);
cookie.Value = DropDownList1.Text;
Response.SetCookie(cookie);
Response.Redirect(Request.RawUrl);
}
Hi I am new to umbraco and I have a question regarding your solution. How do you get the httpModule to work with umbraco is it enought to do " I added a little httpModule in Umbraco bin folder" or do i need to do some thing else too?
is working on a reply...