More in this section

Forums / Developing with Sitefinity / Custom localization

Custom localization

7 posts, 0 answered
  1. Bostjan
    Bostjan avatar
    10 posts
    Registered:
    18 Sep 2009
    23 Sep 2009
    Link to this post

    I would like to implement localization where users could be able to choose website language with the image buttons. Our requirements are as follows:

    1. we have english and german versions, english is default
    2. first time visitors got the version according to their client brower settings
    3. if the user chooses the language, peristent cookie is set. When he or she returns, the language version is selected according to the cookie. If the cookie does not exist anymore, the language version is detected from the client browser settings.

    I have a working solution in asp.net 2008, including master pages, but it does not work in Sitefinity.
    I enabled localized versions in web.config:
     <localization defaultProvider="Sitefinity" persistenceMode="PathPrefix" defaultCulture="en" cultures="en, de">
    and it works fine.
    Then I added two command buttons to the master page that sets language cookies and cookie checking into Global.asax's method Application_BeginRequest. The procedure alters System.Threading.Thread.CurrentThread.CurrentCulture according to cookie setting. I also added label to the master template's Form_Load event that checks for CurrentCulture in current thread:

    lbl1.Text =

    Convert.ToString(System.Threading.Thread.CurrentThread.CurrentCulture);

     

    The Culture in label lbl1 works correct (according to buttons), but Sitefinity continues to renders English version, even if CurrentThread.CurrentCulture displayed in label is German. I have read in the forum that default language settings in the client browser overrides defaultCulture definition in the application web.config. The client browser setting is overrided with CreateSpecificCulture. So it should work... What am I missing here?

    Thank you in advance,
    Bostjan

     

     

     

  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    25 Sep 2009
    Link to this post
    Hi Bostjan,

    Thank you for using our services.

    Sitefinity detects the default browser language, and if it is set to be different than the one in your website web.config it will automatically display the content for the default language. Take a look at my configuration and attached images for reference:
    <localization defaultProvider="Sitefinity" persistenceMode="QueryString" defaultCulture="en" allowSpecificCultures="true" cultures="bg, fr, de, en"

    My suggestion for your requirement would be to use a customization of our Language bar control to change current languages. This is so because when language is selected it changes selected language in the CmsHttpModule. I am attaching a customization of the language bar to make it look as a flag list.  In the code file for the ~/CustomControls/LanguageFlagList.acx you can see that I have subscribed for the event of changing language and in the event handler you should add logic for storing cookies.

    As for the second part of the requirement - loading page from cookie content it is a little bit tricky. You could create an extension of the CmsHttpModule. Here it gets a little bit tricky choosing which method you would have to override in order to get this working. I have an example how to do this in an overriden isAuthenticated method:
    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Web;  
    using Telerik.Cms.Web;  
    using System.Web.Profile;  
    using System.Globalization;  
      
    namespace Telerik.Samples.CustomCmsHttpModule  
    {  
      
        /// <summary>  
        /// Summary description for TestHttpModule  
        /// </summary>  
        public class CustomCmsHttpModule : CmsHttpModule  
        {  
      
            protected override bool IsAuthenticated(HttpContext context, bool ensureRoleProvider)  
            {  
                bool isAuthenticated = base.IsAuthenticated(context, ensureRoleProvider);  
                if (!isAuthenticated && context.Items[UrlHelper.EditModeKey] == null)  
                {  
                    string lang;  
                    //get language from cookie and assign it to lang variable  
                    //note the sting should contain a neutral culture name e.g.  "en", not "en-US"  
                    if (lang != null)  
                    {  
                        if (System.Threading.Thread.CurrentThread.CurrentUICulture.Name != lang)  
                        {  
                          //change UI language for your website   
                          base.SetCurrentCulture(new CultureInfo(lang));  
                        }  
                    }  
                }  
                return isAuthenticated;  
            }  
        }  

    Then in the web.config instead of having the default CmsHttpModule you will declare the customization. Like this:
    <add name="Cms" type="Telerik.Samples.CustomCmsHttpModule.CustomCmsHttpModule, App_Code"/>  


    Kind regards,
    Radoslav Georgiev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. clayman
    clayman avatar
    38 posts
    Registered:
    23 Sep 2009
    05 Nov 2009
    Link to this post
    I have a requirement to support localization --  however much of my content needs to be accessed via code since it is filtered/displayed in the context of a particular product.  Using the sitefinity API, I am able to access documents, images, and generic content for a particular product based on categories and tags.  This content is intermingled with enterprise content (prices, etc) from other dbs and all of it is exposed via a unifying service layer.  Works pretty well.  I was planning on relying on the fact that the Current Culture on the active thread would contain the culture of the requestor - allowing my service layer to access the appropriate localized content.

    It seems that the language selector (tested with both the default sitefinity "dropdown" version and a custom one as described in this thread) doesn't seem to be setting the CurrentCulture on the active thread.  Localization is pretty easy to setup in the config and I followed this thread so I am pretty sure I did it right.

    To describe my testing of this problem -- I have the custom control below, and you can see that I am writing the CurrentCulture.Name out on the page.  Doesn't matter which language I select, the CurrentCulture is always en-US. The interesting thing is that I can include my custom control along with your default LanguageList control on a single page, and they remain in-sync.  So apparently when the language is selected, it is setting "something" that the other language list is aware of...  I guess I can hack up my code to parse the URL since my persistence mode is "path prefix", but shouldn't the culture on the thread be changing?  Any suggestions / ideas about what I am doing wrong?

    public class HyperLinkLanguageList : LanguageList 
        { 
            // a collection of all the controls that represents list items (Hyperlinks) 
            private List<Control> _listItems; 
     
            // fill the collection - get all child controls with ID listItem recursively 
            private void FillItems(Control root) 
            { 
                foreach (Control child in root.Controls) 
                { 
                    if (child != null && child.ID == "listItem") 
                    { 
                        _listItems.Add(child); 
                    } 
                    // recursive call 
                    FillItems(child); 
                } 
            } 
     
            // override this method to fill the collection of HyperLinks 
            protected override void CreateChildControls() 
            { 
                // call the base method 
                base.CreateChildControls(); 
                // initialize and fill the collection recursively 
                this._listItems = new List<Control>(); 
                this.FillItems(this); 
     
                Controls.Add(new LiteralControl("<br/>")); 
                Controls.Add(new LiteralControl(string.Concat("<strong>", Thread.CurrentThread.CurrentCulture.Name, "</strong>")));             
            } 
        } 

    Thanks much!
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    05 Nov 2009
    Link to this post
    Hello clayman,

    Can you give a try to  the code shown below:

    string current = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
    Response.Write(current);

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. clayman
    clayman avatar
    38 posts
    Registered:
    23 Sep 2009
    06 Nov 2009
    Link to this post
    Doh!  That was it - thanks!
  6. Bostjan
    Bostjan avatar
    10 posts
    Registered:
    18 Sep 2009
    24 Nov 2009
    Link to this post

    I have created custom control to make it look like flag and it works ok. Howewer I have a problem with the custom contact form that we have designed. It is also a custom control that has submit button control and some validation controls. When the page is submitted (postback occured) the costumized language bar dissapears and clasic dropdown is displayed. How can this be solved?

    Regards,
    Bostjan
  7. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    24 Nov 2009
    Link to this post
    Hi Bostjan,

    Most probably after post back the template of your control is not initialized and the controls is convert back the the default structure.

    Regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Register for webinar
7 posts, 0 answered