+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. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    22 Feb 2010
    Link to this post
    Hello Alvaro,

    The current culture is changed in the CmsHttpModule.SetCurrentCulture method. This method performs exactly what Ivan has sampled in his last post. However this method is used withing the CmsHttpModule. Using the code as suggested in previous post will do the job.

    All the best,
    Radoslav Georgiev
    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.
  2. Pierre
    Pierre avatar
    433 posts
    Registered:
    16 Feb 2006
    22 Feb 2010
    Link to this post
    Hi Team,

    I have implemented like u suggest using one registred module as handler, one Webservices for api changes and Language selector using session states but is not working as need.  The Webservice, the module handler and the selector are sharing one session value to preserve states. If you change using one webservice the resulting message is done but the redirection is not make. If you load at Admin or other pages with the Languageselector is in the Default language, not in the new selected, and the contents are not in the new language. Could you explian if after change the language culture is necesary one redirect and how to perform. Is not better link some internal session variable used by your handler to preserve the states and solidity. Romi.

    void context_BeginRequest(object sender, EventArgs e)
        {
            HttpContext context = HttpContext.Current;
            if (CmsUrlContext.Current != null && context.Items["cmspagemode"] == null)
            {  
                // use /es-GB/
                CultureInfo culture = (CultureInfo)context.Items["cmscurrcult"];
     
                if (culture == null)
                {
                    //use Session selected culture in selector or using webservice
                    if (context.Session["selectedLanguageValue"] != null)
                        culture = (CultureInfo)context.Session["selectedLanguageValue"];
                }
                if (culture != null)
                {
                    HttpContext.Current.Items["cmscurrcult"] = culture;
                    System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
                    System.Globalization.CultureInfo cultureInfo = System.Threading.Thread.CurrentThread.CurrentUICulture;
                    System.Threading.Thread.CurrentThread.CurrentUICulture = culture;   
                }


  3. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    23 Feb 2010
    Link to this post
    Hello Pierre,

    After you change the current culture you should make a response redirect to the the same page however add the changed culture to the url.

    Kind regards,
    Radoslav Georgiev
    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.
  4. Ahmed Salman
    Ahmed Salman avatar
    31 posts
    Registered:
    13 Apr 2010
    16 May 2010
    Link to this post
    Dear sir ,

        i have two language ( en / ar ).

    if i am in the engalish page i need to see link for ar page and if i am in arabic page i need to see only the english link.


    how i can chage the LanguageFlagList control to do this ?
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    17 May 2010
    Link to this post
    Hi Ahmed Salman,

    Crate a user control with the following declaration in it

    <%@ Register Namespace="Telerik.Localization.WebControls" TagPrefix="lng" Assembly="Telerik.Localization" %>
     
    <style type="text/css"
    .langList li 
        float:left; 
        padding: 0px; 
        list-style-type:none;
            
    }
        
    </style>
       
    <lng:LanguageBar  ID="list1" runat="server">
        <LayoutTemplate>
            <lng:LanguageList CssClass="langList" ID="languageList" runat="server"></lng:LanguageList>
        </LayoutTemplate>
    </lng:LanguageBar>

    The control loads all cultures you have added in the web.config. Below is a sample code that you can use to hide the current selected language.

    protected void Page_Load(object sender, EventArgs e)
       {
           LanguageList lngList = this.FindControlRecursive(list1, "languageList") as LanguageList;
           string curentCulture= System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
           forint i = lngList.Items.Count-1 ; i >= 0 ; i-- )
           {
               ListItem li = lngList.Items[i];
                 
               if (li.Value == curentCulture)
               {
                   lngList.Items.RemoveAt(i);
               }
           }
       }
       private Control FindControlRecursive(Control root, string id)
       {
           if (root.ID == id)
           {
               return root;
           }
     
           foreach (Control c in root.Controls)
           {
               Control t = FindControlRecursive(c, id);
               if (t != null)
               {
                   return t;
               }
           }
     
           return null;
       }


    All the best,
    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.
  6. George
    George  avatar
    204 posts
    Registered:
    05 Sep 2006
    18 May 2010
    Link to this post
    Ok I think It will be good to share with you my thoughts on the subject. I had a requirement to develop my own language bar cause I had a simple link that would display as english when i'm in the arabic version of the site and as arabic when i'm in the english version of the site so here is what I did :

    private void ChangeLanguage()
    {
        //If the language is english then change the path prefix, set the thread culture and redirect
        if(System.Threading.Thread.CurrentThread.CurrentUICulture.Name.Equals(Constants.CULTURE_EN))
        {
            var targetUrl = Request.AppRelativeCurrentExecutionFilePath;
     
            if(targetUrl.StartsWith("~/" + Constants.CULTURE_EN + "/"))
            {
                targetUrl = targetUrl.Substring(5);
            }
            else if(targetUrl.StartsWith("~/"))
            {
                targetUrl = targetUrl.Substring(2);
            }
     
            targetUrl = "~/" + Constants.CULTURE_AR + "/" + targetUrl;
     
            System.Threading.Thread.CurrentThread.CurrentUICulture =
                new System.Globalization.CultureInfo(Constants.CULTURE_AR);
     
            Response.Redirect(targetUrl, true);
        }
        else if (System.Threading.Thread.CurrentThread.CurrentUICulture.Name.Equals(Constants.CULTURE_AR))
        {
            var targetUrl = Request.AppRelativeCurrentExecutionFilePath;
     
            if (targetUrl.StartsWith("~/" + Constants.CULTURE_AR + "/"))
            {
                targetUrl = targetUrl.Substring(5);
            }
            else if (targetUrl.StartsWith("~/"))
            {
                targetUrl = targetUrl.Substring(2);
            }
     
            targetUrl = "~/" + Constants.CULTURE_EN + "/" + targetUrl;
     
            System.Threading.Thread.CurrentThread.CurrentUICulture =
                new System.Globalization.CultureInfo(Constants.CULTURE_EN);
     
            Response.Redirect(targetUrl, true);
        }
    }


    This was the code behind for the Link Button that changes the language and it does not use the language bar.

    For those who are having problems with the menu control or any other scenario where you have to redirect to a page and keep the same language, you could do the following :

    When populating the menu control use this

    radMenuItem.NavigateUrl = UrlHelper.ResolveUrl(node.Url);

    The UrlHelper.ResolveUrl returns the Url of the page following the selected language so it should be used on every redirection to be safe.

    Best,

    George

  7. Ahmed Salman
    Ahmed Salman avatar
    31 posts
    Registered:
    13 Apr 2010
    20 May 2010
    Link to this post
    thank you it is working :)
  8. Matthew
    Matthew avatar
    9 posts
    Registered:
    09 Jun 2009
    04 Apr 2011
    Link to this post
    The problem for us is that the language bar relies on JavaScript.  So, for our mobile sites, we want to have a language switcher, but many mobile browsers don't have JavaScript.  How do we do a JavaScript-free solution?

    Thanks,
    matthew
  9. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    05 Apr 2011
    Link to this post
    Hello ,

    You can use a repeater with links and each link will make response redirect depending on the culture. To get all languages use LocalizationManager.Cultures.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

Register for webinar
39 posts, 1 answered
1 2