Copied to clipboard

Flag this post as spam?

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


  • Stefan Kip 1614 posts 4131 karma points c-trib
    Aug 04, 2010 @ 14:22
    Stefan Kip
    3

    Umbraco overriding System.Threading.Thread.CurrentThread.CurrentCulture

    Hi Umbracians!

    I'm using Umbraco 4.5.1 at the moment and stumbled upon this (for us) unwanted behavior:

    In the BeginRequest event I'm changing the CurrentThread.CurrentCulture property by reading a 'culture' cookie.
    Then in the OnInit of the masterpage or a usercontrol the culture has been overwritten by Umbraco.
    Just took a look in the source and found out that in 'Default.aspx' the culture is overwritten if Domains are set (Domain - Culture combination):

    Snippet

    string tempCulture = m_umbPage.GetCulture();
    if (tempCulture != "")
    {
        System.Web.HttpContext.Current.Trace.Write("default.aspx""Culture changed to " + tempCulture);
        System.Threading.Thread.CurrentThread.CurrentCulture =
            System.Globalization.CultureInfo.CreateSpecificCulture(tempCulture);
        System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;
    }

     

    Now for us this is unwanted behavior, there should be an extra check in the if statement for an appSetting or something like that (so you can disable this from the web.config).

    Now I fixed it by creating our own Default.aspx, which inherits from "umbraco.UmbracoDefault" and add this code:

    Snippet

    public partial class Default : umbraco.UmbracoDefault
    {
    protected override void OnPreInit(EventArgs e)
    {
    base.OnPreInit(e);

    SetCulture();
    }

    void SetCulture()
    {
    try
    {
    CultureInfo culture;

    // Set culture
    HttpCookie objCookie = HttpContext.Current.Request.Cookies["culture"];
    if (objCookie != null && !string.IsNullOrEmpty(objCookie.Value))
    culture = new CultureInfo(objCookie.Value);
    else
    culture = new CultureInfo(UmbracoExtensions.DefaultMLLanguage);

    System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = culture;

    System.Web.HttpContext.Current.Trace.Write("HttpModule.cs""Culture changed to " + culture.TwoLetterISOLanguageName);
    }
    catch (Exception ex)
    {
    Log.Add(LogTypes.Error, -1, string.Format("BeginRequest exception: {0}", ex.Message));
    #if DEBUG
    System.Diagnostics.Trace.TraceError("BeginRequest exception: {0}", ex.Message);
    #endif
    }
    }
    }

    So the questions are:
    Why is Umbraco always overwriting the CurrentCulture, while in some situations this is unwanted?
    Why isn't there an appSetting which can disable this?
    And will there be an appSetting in the future for this?

  • Niels Hartvig 1951 posts 2391 karma points c-trib
    Aug 11, 2010 @ 13:59
    Niels Hartvig
    0

    This is by design and convention. It only overrides currentculture when a domain/language have been set on a node (to ensure that dictionary items and culture specific items are displayed correctly).

    There isn't an appSetting for this as it's a convention and we don't believe in a ton of configuration options.

    If you don't want this, just don't use the assign hostname/culture function. If you do use it, then use it as it has been designed; that the culture settings are updated based on the culture assigned to the node.

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Aug 11, 2010 @ 14:03
    Stefan Kip
    0

    @Niels

    Thanks for your reply.
    Our 'issue' is that we are hosting two websites (which belong together) in a single Umbraco installation. The customer chose to be able to edit multilanguage properties via extra tabs on documents, so they explicitly didn't want 3 different versions per site (one for every language).

    At the moment when adding a hostname, you are forced to specify a language. Isn't it a good idea to make this optional? If not set, don't adjust the current thread's culture?

  • Niels Hartvig 1951 posts 2391 karma points c-trib
    Aug 11, 2010 @ 14:07
    Niels Hartvig
    0

    Definitely worth for a future release - post it as a CP feature request :)

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Aug 11, 2010 @ 14:08
    Stefan Kip
    0

    Was just creating a CP ticket :-)

    Thanks!

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Aug 16, 2010 @ 09:57
    Sebastiaan Janssen
    0

    Thanks for the example code Stefan, just what I needed! :-)

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Aug 16, 2010 @ 09:59
    Stefan Kip
    1

    No problem! :-)

    Here is the workitem btw: http://umbraco.codeplex.com/workitem/28521

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Aug 16, 2010 @ 10:15
    Sebastiaan Janssen
    0

    Oh good, voted, excellent solution!

  • Christian Palm 278 posts 273 karma points
    Mar 24, 2011 @ 10:52
    Christian Palm
    0

    Voted - we could use that to

Please Sign in or register to post replies

Write your reply to:

Draft