+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Change the current language of the public site using the API instead of using LanguageBar

Change the current language of the public site using the API instead of using LanguageBar

39 posts, 1 answered
  1. Skywalker
    Skywalker avatar
    8 posts
    Registered:
    22 Sep 2007
    16 Nov 2007
    Link to this post
    Hi,

    What I'm trying to achieve is a horizontal language bar displaying country flag images.
    I can't seem to manage this by implementing a WebControl derived from ListControl, as I'm not sure on how to set the current language.
    The simplest solution seems to be to redirect the page, changing the sflang querystring parameter, but I prefer using the API to set the current language.

    How should I do this?
    Thanks.
  2. Rebecca
    Rebecca avatar
    536 posts
    Registered:
    24 Sep 2012
    16 Nov 2007
    Link to this post
    Hi Sipke,

    You can achieve this with a little code in a user control. The LanguageBar control in Sitefinity's toolbox relies on a control that inherits from ListControl to render its markup. In the admin part of Sitefinity, there is a custom control that is used for this purpose, Telerik.Localization.WebControls.LanguageList. If you want to change the layout, you need to create a similar control, or extend the existing LanguageList. We've included an example of how to extend it. Then, you have to tell LanguageBar to use the new control by setting its LayoutTemplate property. For the purpose, we created a user control, and, in the code, told it to use LanguageFlagList to render the content.
    The logic behind the custom LanguageFlagList control is to get a reference to all Hyperlinks rendered by LanguageList and set their ImageUrl property. The path to the image is set to ~/Images/<culture_code>.gif.
    So, in order to see the flags, you should put the pictures in the ~/Images folder and set their names to the name of the culture with a GIF extension.
    The code in the example has been commented for clarity. You should put the LanguageFlagList.cs file in the App_Code folder and include the user control in the toolbox, either using the Sitefinity interface or through the  web.config.
    If you have any questions on the example, please don't hesitate to ask.

    We are planning to extend the LanguageList control so that this can be done more easily in the upcoming releases, but for the time being this solution will work.

    Best wishes,
    Rebecca
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. Skywalker
    Skywalker avatar
    8 posts
    Registered:
    22 Sep 2007
    16 Nov 2007
    Link to this post
    Hi Rebecca,

    Thank you for your quick and clear response! That helps a lot.
    I have a question right away: Is it also possible to call a method, say, LocalizationManager.SetCurrentCulture("en") ?
    How does the LanguageBar control set the current language anyway?

    Thanks,

    Sipke
  4. Slavo
    Slavo avatar
    295 posts
    Registered:
    24 Sep 2012
    16 Nov 2007
    Link to this post
    Hello Sipke,

    Is is not that easy to change the language through calling a method. The difficulty comes from the fact that you have to change the language of the current thread and the page has to know which language to use early, so you can't just do it on postback. The LanguageBar control changes the language by modifying the URL depending on the persistenceMode set in the web.config.
    If the only purpose is to change the layout for the control, I suggest that you extend the LanguageBar. As you can see in the provided example, it is not that hard and you get the functionality out of the box. Since it uses a template, modifying the look and layout is easy. Of course, if you prefer the hard way, we can send you the code for the control for your reference.

    Sincerely yours,
    Slavo
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  5. Chris Breymeyer
    Chris Breymeyer avatar
    21 posts
    Registered:
    16 Nov 2007
    14 Dec 2007
    Link to this post
    Rebecca,

    I'm a little confused about your instructions.  They say to se the LayoutTemplate property of the LanguageBar control to use the extended LanguageList (now LanguageFlagList), but I can't seem to find that property?

    Can you help me become more clear on what I need to do to implement this?  I tried copying the LanguageFlagList control to the screen bet get an unordered list <ul> of the flag images?

    Thanks for your time, I'm becoming more confident in our choice to use sitefinity every day!

    John
  6. Slavo
    Slavo avatar
    295 posts
    Registered:
    24 Sep 2012
    15 Dec 2007
    Link to this post
    Hello John Reynolds,

    The LanguageBar control uses a Layout template for its presentation. The Layout template is a property of type ITemplate. You have to set it in code and tell the language bar to use a LanguageList as a template. Please download the attachment given in the second post to this thread and look at the code-behind file in the UserControl folder (the filename is LanguageFlagList.ascx.cs). In the Page_Load method there you can see how to set the LayoutTemplate property.

    All the best,
    Slavo
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  7. Andreas
    Andreas avatar
    49 posts
    Registered:
    29 Dec 2007
    15 Jan 2008
    Link to this post
    This works like a charm for me. However, when I make a postback on my news pages (paging through the news items) it switch back to the default language selector (the dropdownlist).

    Any idea where I messed up? Thanks in advance.
  8. Vlad
    Vlad avatar
    498 posts
    Registered:
    15 Jul 2016
    16 Jan 2008
    Link to this post
    Hello Andreas,

    Following your comments, we cannot reproduce a similar issue in Sitefinity. Could you please describe the problem in more detail?

    Regards,
    Vlad
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  9. Andreas
    Andreas avatar
    49 posts
    Registered:
    29 Dec 2007
    16 Jan 2008
    Link to this post
    Sure, sorry for being a bit short on my description earlier. I had high hopes of solving it myself but havent managed so far.

    I'm using the LanguageFlagList controls attached earlier in the thread to and its working great switching between the selected uicultures. The problem arises as soon as I make a postback on any page. At the postback its not using the template set in code anymore but instead the regular one in LanguageBar (the one with a select input type / dropdownlist).

    I havent really altered the code anything beyond image paths so Im not sure whats going wrong tbh.

    Thanks a lot for your help.

    //Andreas
  10. Vlad
    Vlad avatar
    498 posts
    Registered:
    15 Jul 2016
    17 Jan 2008
    Link to this post
    Hi Andreas,

    Sorry, now I understand...

    To fix the problem, modify LanguageFlagList.ascx.cs file:

    Change this code:
        protected void Page_Load(object sender, EventArgs e) 
        { 
            // tell the LanguageBar to use a custom template 
            this.flagList.LayoutTemplate = new DefaultTemplate(); 
        } 
     

    with this one:
        protected override void OnInit(EventArgs e) 
        { 
            base.OnInit(e); 
     
            // tell the LanguageBar to use a custom template 
            this.flagList.LayoutTemplate = new DefaultTemplate(); 
        } 
     


    We have attached a zip with the updated LanguageFlagList.

    All the best,
    Vlad
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  11. Andreas
    Andreas avatar
    49 posts
    Registered:
    29 Dec 2007
    17 Jan 2008
    Link to this post
    Thanks alot for the swift help! Much appreciated!

    //Andreas
  12. Markus66
    Markus66 avatar
    6 posts
    Registered:
    28 Dec 2006
    07 May 2008
    Link to this post
    I want to use this control!!! Is there a way to show the flags horizontally??? 
    instead of  vertically?

  13. Vlad
    Vlad avatar
    498 posts
    Registered:
    15 Jul 2016
    08 May 2008
    Link to this post
    Hello Markus,

    The layout depends on the HTML styling. The LanguageList control renders <ul> element with <li> elements. In order set the flags horizontally, you should set float: left style for the <li>. For example, you can do this by creating a CSS class:

    .langList li
    {
        float:left;
        padding: 5px;
        list-style-type: none;
    }

    ... and setting it to the CssClass property of the LanguageList control in the LanguageFlagList.ascx.cs file:

        // Change the template for Language bar - use the custom LanguageFlagList to render markup 
        private class DefaultTemplate : ITemplate 
        { 
            public void InstantiateIn(Control container) 
            { 
                // initialize the LanguageBarList 
                LanguageFlagList list = new LanguageFlagList(); 
                // set the ID so that it is recognized by the container 
                list.ID = "languageList"
                list.CssClass = "langList"
                container.Controls.Add(list); 
            } 
        } 

    Hope this is helpful.

    All the best,
    Vlad
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
    Answered
  14. Markus66
    Markus66 avatar
    6 posts
    Registered:
    28 Dec 2006
    08 May 2008
    Link to this post
    Thank you Vlad this was very helpfully.
  15. Markus66
    Markus66 avatar
    6 posts
    Registered:
    28 Dec 2006
    17 Jul 2008
    Link to this post

    Hi Sitefinity-Team

    Is there a possibility to use this language bar also in a RadPanelbar? For example: With a parent node “language” and three child nods with “de”, “en” and “it”. Can you please post a example?

    Thank you

  16. Nikifor
    Nikifor avatar
    232 posts
    Registered:
    18 May 2013
    17 Jul 2008
    Link to this post
    Hi Markus66,

    Can you please elaborate a bit on the exact implementation you want to achieve. Reading your post our understanding is that your requirement is for the following:

    <form id="form1" runat="server"
    <div> 
    <asp:ScriptManager ID="ScriptManager1" runat="server"
    </asp:ScriptManager> 
    </div> 
    <telerik:RadPanelBar ID="RadPanelBar1" runat="server"
    <CollapseAnimation Type="None" Duration="100"></CollapseAnimation> 
    <Items> 
    <telerik:RadPanelItem runat="server" Text="language"
    <Items> 
    <telerik:RadPanelItem runat="server" Text="de"
    </telerik:RadPanelItem> 
    <telerik:RadPanelItem runat="server" Text="en"
    </telerik:RadPanelItem> 
    <telerik:RadPanelItem runat="server" Text="it"
    </telerik:RadPanelItem> 
    </Items> 
    </telerik:RadPanelItem> 
    </Items> 
    <ExpandAnimation Type="None" Duration="100"></ExpandAnimation> 
    </telerik:RadPanelBar> 
    </form> 

    Please let us know if this is what you mean or you have something else in mind.

    Regards,
    Nikifor
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  17. Markus66
    Markus66 avatar
    6 posts
    Registered:
    28 Dec 2006
    17 Jul 2008
    Link to this post

    Thank you for your help and sorry for my bad declaration, but what I want is the same example like the "LanguageFlagList“, descript in the older posts, but only for the RadPanelbar. Instead of flags with the different languages I want a RadPanelbar with my languages.

    Thank you

  18. Nikifor
    Nikifor avatar
    232 posts
    Registered:
    18 May 2013
    18 Jul 2008
    Link to this post
    Hi Markus66,

    Thank you for the elaboration.

    We understood what your aim is, but unfortunately this would not be an easy task to achieve. We recommend using the already posted approach, as implementing this for the RadPanelbar would cost a lot of coding effort.

    Kind regards,
    Nikifor
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  19. Christopher
    Christopher avatar
    8 posts
    Registered:
    11 Jun 2008
    19 Sep 2008
    Link to this post
    Hello,

    I'm currently trying to modify this control to output the following but not having much luck, could anyone help with this please? What we would like is:

    <ul>
    <li class="en"><a href="">English</a></li>
    <li class="nl"<a href="">Nederlands</a></li>
    </ul>

    So basically we'd like a corresponding class outputted on the list item. Is this possible?

    Thanks in advance!
    -Christopher
  20. Christopher
    Christopher avatar
    8 posts
    Registered:
    11 Jun 2008
    22 Sep 2008
    Link to this post
    Just as an update, I've done this by adding the class to the anchor rather than the list item.
  21. Imran
    Imran avatar
    75 posts
    Registered:
    17 Jan 2007
    28 Nov 2008
    Link to this post
    Hello

    Many thanks for the Flag Control. I've set it up and it works well. I've noticed a problem on my site. I have a on my master page which is the navigation. The navigation is not bound to the site map datasource, instead i create Items within the menu and link to the various pages of the site.

    The default language is en, and secondary is de. when viewing pages in de, which I click something in the Radmenu the PathPrefix parameter gets removed and there reverts back to the default language.

    the menu is set up like this:

    <radM:RadMenu ID="RadMenu1" CausesValidation="false" runat="server" CssClass="ArdmoreMenu">

    <Items>

    <radM:RadMenuItem NavigateUrl="~/home.aspx" Text="Home" ImageUrl="~/images/navigation/home.jpg" />

    <radM:RadMenuItem NavigateUrl="~/news.aspx" Text="News" ImageUrl="~/images/navigation/news.jpg" />

    <radM:RadMenuItem NavigateUrl="~/distillery_tour/photo_tour.aspx" Text="Distillery Tour" ImageUrl="~/images/navigation/tour.jpg" ImageOverUrl="~/images/navigation/tour_over.jpg">

    <Items>

    <radM:RadMenuItem Text="Photo Tour" NavigateUrl="~/distillery_tour/photo_tour.aspx" />

    <radM:RadMenuItem Text="Video Tour" NavigateUrl="~/distillery_tour/video_tour.aspx" />

    </Items>

    ... etc

    My question would be, how would I make the radmenu NavigateUrl's take notice of localization so it goes to the correct page when viewing in various languages.

    All help would be greatly apprecaited. Thanks
  22. Imran
    Imran avatar
    75 posts
    Registered:
    17 Jan 2007
    28 Nov 2008
    Link to this post
    small text ammend:

    Many thanks for the Flag Control. I've set it up and it works well. I've noticed a problem on my site. I have a RADMENU on my master page which is the navigation. The navigation is not bound to the site map datasource, instead i create Items within the menu and link to the various pages of the site.
  23. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    28 Nov 2008
    Link to this post
    Hello Imran,

    I think that you need to create a page  that supports two or more languages. Let say you have EN and BG languages and both of them using your template. In your master.cs you have to use "if loop" as a simple solution.

    if (Request.Url.ToString().Contains("/BG"))   
            {   
                RadMenu1.Items.FindItemByText("Home").Text = "BGversion";   
                RadMenu1.Items.FindItemByText("Search").Text = "BGversion";   
            }  


    I hope this helps.


    All the best,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  24. Imran
    Imran avatar
    75 posts
    Registered:
    17 Jan 2007
    28 Nov 2008
    Link to this post
    Thanks Ivan. I did think about manipulating the navigateUrl based on what language is on.

    I was hoping there was something more intutive that i could  have done within the radmenu, but your solution is perfectly fine.

    many thanks
  25. Pierre
    Pierre avatar
    433 posts
    Registered:
    16 Feb 2006
    21 Feb 2010
    Link to this post
    Hi guys,

    Ok, but.
    - what about changing the current language using Api instead using the Language bar ?
    - it's possible to alter or modify the item order in the languagesbar ?.
     I need to implement one country detection based on ip and fire the languageBar to change programmatically using some parameter and later change the order of items. Regards, Romi.
  26. Andree
    Andree avatar
    44 posts
    Registered:
    11 Jan 2010
    21 Feb 2010
    Link to this post
    Hi Pierre,

    I am also after knowing how to change the language using the API directly, rather than an external control, as posted on http://www.sitefinity.com/support/forums/sitefinity-3-x/developing-with-sitefinity/282208-language-selector.aspx

    Any tips Ivan?


    Regards,
    Alvaro
  27. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    22 Feb 2010
    Link to this post
    Hi Alvaro,

    The code below will change the current page culture

    CultureInfo culture;
    culture = CultureInfo.GetCultureInfo("bg");
    System.Threading.Thread.CurrentThread.CurrentUICulture = culture;


    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  28. Andree
    Andree avatar
    44 posts
    Registered:
    11 Jan 2010
    22 Feb 2010
    Link to this post
    Hi Ivan,

    I have used your code in the following format:

    CultureInfo culture = CultureInfo.GetCultureInfo("ja");
    System.Threading.Thread.CurrentThread.CurrentUICulture = culture;

    However, links generated by Sitefinity that look like this:

    http://www.domain.com/analytics/

    When clicked on, redirect to:

    http://www.domain.com/en/analytics/

    Looking at System.Threading.Thread.CurrentThread.CurrentUICulture on a second postback reveals that the CurrentUICulture has reverted back to "en", rather than "ja" as per my example. It appears Sitefinity is reverting the Culture change to its original "en" culture.

    There must be more code to change sitefinity's handling of the language than what you've already mentioned?


    Regards,
    Alvaro
  29. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    22 Feb 2010
    Link to this post
    Hello Alvaro,

    The code changes the language of the current page. You make a response redirect and this switches the culture. You need a custom control that persists the state or custom handler where you set the culture on context.BeginRequest

    HttpContext context = HttpContext.Current;
    CultureInfo cultureInfo = System.Threading.Thread.CurrentThread.CurrentUICulture;
    System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("bg");


    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  30. Andree
    Andree avatar
    44 posts
    Registered:
    11 Jan 2010
    22 Feb 2010
    Link to this post
    Hi Ivan,

    So there isn't anything in place that does this automatically within Sitefinity? e.g. The existing Language selector sets a variable which the Sitefinity framework uses for working out the current language the user should be looking at?

    It all has to be written manually?


    Regards,
    Alvaro
Register for webinar
39 posts, 1 answered
1 2