Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Danyl 6 posts 26 karma points
    Jul 01, 2011 @ 11:55
    Danyl
    0

    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:

     

    Thread.CurrentThread.CurrentUICulture = cultureInfo;
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);

    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:

    <input type="submit" value="<umbraco:Item field="#Site Name" runat="server"></umbraco:Item>" id="submit"/>

    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 :)

  • Steen Tøttrup 191 posts 291 karma points c-trib
    Jul 01, 2011 @ 12:18
    Steen Tøttrup
    0

    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?

     

     

  • Danyl 6 posts 26 karma points
    Jul 01, 2011 @ 16:19
    Danyl
    0

     

    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);

            }

     

     

  • Mattias Nilsson 3 posts 23 karma points
    Aug 23, 2011 @ 09:08
    Mattias Nilsson
    0

    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?

Please Sign in or register to post replies

Write your reply to:

Draft