Forum Discussion

PandaUA's avatar
8 years ago

Custom User Online Component

Hi all!

 

Users of our community are able to view it in two languages.

They can select language in profile settings and via the next element: 

      <ul class="header__lingvs">
        <li><a class="sw_lang" title="Russian" href="/?profile.language=ru">Rus</a></li>
        <li><a class="sw_lang" title="Ukrainian" href="/?profile.language=uk">Ukr</a></li> 
      </ul>

When user clicks on the one of these buttons, language changes, but user's being redirected to the main page of community, no matter on what page he was before.

 

Adding the profile.language setting to any other page address has no effect - page just refreshes with with this setting in the end.

 

Is it some way to switch language and stay on the current page?

 

Thanks in advance,

Yurii

  • I believe the reason it was always going to the community page was because you had a "/" at the front of your anchor url. If you removed the "/" from the front of the anchor tag's href attribute, it would redirect you to the same page you were on:

     

    <ul class="header__lingvs">
      <li><a class="sw_lang" title="Russian" href="?profile.language=ru">Rus</a></li>
      <li><a class="sw_lang" title="Ukrainian" href="?profile.language=uk">Ukr</a></li> 
    </ul>

    However, it will remove any query parameters that are already in the URL when you do that, so you probably would want to programmatically create the URL (as Claudius mentioned in his reply). Something like this maybe (I've included a couple really crude functions which might have bugs so use at your own risk):

     

    <#function appendParam bf lang>
      <#if bf?ends_with("&")>
        <#return bf + "profile.language=" + lang />
      <#else>
        <#return bf + "&profile.language=" + lang />
      </#if>
    
    </#function>
    <#function getCurrentPageUrlWithNewLanguage lang>
      <#if http.request.url?contains("?")>
        <#if http.request.url?contains("profile.language=")>
          <#assign bf = http.request.url?keep_before("profile.language=") />
          <#assign af = http.request.url?keep_after("profile.language=") />
          <#if af?contains("&")>
            <#return appendParam(bf, lang) + "&" + af?keep_after("&") />
          <#else>
            <#return appendParam(bf, lang) />
          </#if>
        <#else>
          <#return http.request.url + "&profile.language=" + lang />
        </#if>
      <#else>
        <#return http.request.url + "?profile.language=" + lang />
      </#if>
    </#function>
    
    <ul class="header__lingvs">
      <li><a class="sw_lang" title="Russian" href="${getCurrentPageUrlWithNewLanguage("ru")}">Rus</a></li>
      <li><a class="sw_lang" title="Ukrainian" href="${getCurrentPageUrlWithNewLanguage("uk")}">Ukr</a></li> 
    </ul>

    -Doug

     

  • DougS's avatar
    DougS
    8 years ago

    If I scroll down to the bottom of http://club.volia.com/t5/Internet/Vopros-otvet-po-WiFi/td-p/55225?profile.language=ru, I see "Рекомендации" for the last section heading.

     

    If I scroll down to the bottom of http://club.volia.com/t5/Internet/Vopros-otvet-po-WiFi/td-p/55225?profile.language=uk, I see "Рекомендації" for the last section heading.

     

    So it looks like the language switching is working (I don't know Russian or Ukrainian so I'm just assuming it's working based on the fact that there is a difference between the two).

     

    Since this page has much more user-generated content, perhaps you are seeing posts that were made in one of the languages showing on this page no matter what language is set? Here is a good thread about machine-generated UGC translation you might want to look at if you are interested in looking into a way to translate your UGC.

     

    Also, might you have customizations that include hard-coded text in one language, instead of using text keys and providing translations, or might you have used text keys but haven't provided the translations yet? - something to check

     

    Finally, I believe the profile.language parameter only works for changing the language if the user is anonymous (not signed-in). If the user is signed in, it will use a "user setting" (also named profile.language) to determine the language. The best way to make sure that the language is changed for both anonymous and signed-in users will require you to add something to your Page Initialization script (via Studio) which sets the language using the http.request.setRequestLanguage Freemarker call. Here is an example call you might make:

     

     

    <#assign requestLanguage = http.request.parameters.name.get("profile.language", "none") />
    <#if requestLanguage != "none">
      ${http.request.setRequestLanguage(requestLanguage)}
    </#if>

     

    You need to make that call in your Page Initialization script for it to take effect on the page. If you want the language to persist for a signed in user across requests (so you don't have to keep passing the profile.language parameter around with each request), you might change your page init script to this:

     

     

    <#assign requestLanguage = http.request.parameters.name.get("profile.language", "none") />
    <#if requestLanguage != "none">
      ${http.request.setRequestLanguage(requestLanguage)}
      <#if !user.anonymous>
        <#assign userLang = rest("/users/id/" + user.id + "/profiles/name/language").value />
        <#if userLang != requestLanguage>
          ${rest("/users/id/" + user.id + "/profiles/name/language/set?value=" + requestLanguage)}
        </#if>
      </#if>
    </#if>

    Just by adding the profile.language query parameter to a request, it will set a cookie for anonymous (not signed-in) users that will persist across requests (so you shouldn't need to add any additional logic to handle anonymous users).

     

     

    I hope that helps!

     

    -Doug

  • PandaUA - Both the languages are basically 2 different categories in the backend. Also, this language switcher not just translates the language, it takes the user to a different category which is mapped to the respective locale/ language.

     

     

    • PandaUA's avatar
      PandaUA
      Ace

      VarunGrazitti, thank you for the reply!

       

      Regardless of language, content is the same - only interface language changes.

       

      But I think it's a bit inconvenient to go back to the previous page manually.

      Is there some way to create a redirect after switching language?

      • ClaudiusH's avatar
        ClaudiusH
        Khoros Alumni (Retired)

        Вітаю / Привет PandaUA :)

         

        If I understand you correctly the language switching element is present on every page and you are looking for a way to make it work everywhere without landing the user back on the root community page after following a language switch link.

         

        You would need to extend the component code to add the current page url to the link target. The "webUi.url" method of the coreNode context object should come in handy here. Keep in mind that the url of the current page might already have a ? url parameter separator, so you need to programmatically detect whether to append the language parameter via 

        ?profile.language=ru

        or as an additional parameter as

        &profile.language=ru

          

  • DougS's avatar
    DougS
    Khoros Oracle

    I believe the reason it was always going to the community page was because you had a "/" at the front of your anchor url. If you removed the "/" from the front of the anchor tag's href attribute, it would redirect you to the same page you were on:

     

    <ul class="header__lingvs">
      <li><a class="sw_lang" title="Russian" href="?profile.language=ru">Rus</a></li>
      <li><a class="sw_lang" title="Ukrainian" href="?profile.language=uk">Ukr</a></li> 
    </ul>

    However, it will remove any query parameters that are already in the URL when you do that, so you probably would want to programmatically create the URL (as Claudius mentioned in his reply). Something like this maybe (I've included a couple really crude functions which might have bugs so use at your own risk):

     

    <#function appendParam bf lang>
      <#if bf?ends_with("&")>
        <#return bf + "profile.language=" + lang />
      <#else>
        <#return bf + "&profile.language=" + lang />
      </#if>
    
    </#function>
    <#function getCurrentPageUrlWithNewLanguage lang>
      <#if http.request.url?contains("?")>
        <#if http.request.url?contains("profile.language=")>
          <#assign bf = http.request.url?keep_before("profile.language=") />
          <#assign af = http.request.url?keep_after("profile.language=") />
          <#if af?contains("&")>
            <#return appendParam(bf, lang) + "&" + af?keep_after("&") />
          <#else>
            <#return appendParam(bf, lang) />
          </#if>
        <#else>
          <#return http.request.url + "&profile.language=" + lang />
        </#if>
      <#else>
        <#return http.request.url + "?profile.language=" + lang />
      </#if>
    </#function>
    
    <ul class="header__lingvs">
      <li><a class="sw_lang" title="Russian" href="${getCurrentPageUrlWithNewLanguage("ru")}">Rus</a></li>
      <li><a class="sw_lang" title="Ukrainian" href="${getCurrentPageUrlWithNewLanguage("uk")}">Ukr</a></li> 
    </ul>

    -Doug

     

    • PandaUA's avatar
      PandaUA
      Ace

      ClaudiusHDougS, I'm really glad you guys are trying to help with my question! Thank you! :smileyhappy:


      ClaudiusH wrote:

      If I understand you correctly the language switching element is present on every page and you are looking for a way to make it work everywhere without landing the user back on the root community page after following a language switch link.


      You're absolutely right!

       

      DougS, thank you for the code. I'll check if it works.

      But before implementing it programmatically we can try to do it manually.

      I think it will be easier to understand if you try it on your own.

      Here is our community: club.volia.com.  Language switching element is in the upper right corner of the page:

      Show more

      On the main page we can easily switch language (even if you don't know Russian/Ukrainian, you can see how titles change :smileyhappy: )

      If I understand you right here is what we should have after substituting language parameter after current page url:

      http://club.volia.com/t5/Internet/Vopros-otvet-po-WiFi/td-p/55225?profile.language=ru
      or
      http://club.volia.com/t5/Internet/Vopros-otvet-po-WiFi/td-p/55225?profile.language=uk

      The problem is that putting this in browser address bar, has no effect.

       

      I guess I just don't understand exactly how it works.

      Maybe not only profile.language parameter should be changed.

      • DougS's avatar
        DougS
        Khoros Oracle

        If I scroll down to the bottom of http://club.volia.com/t5/Internet/Vopros-otvet-po-WiFi/td-p/55225?profile.language=ru, I see "Рекомендации" for the last section heading.

         

        If I scroll down to the bottom of http://club.volia.com/t5/Internet/Vopros-otvet-po-WiFi/td-p/55225?profile.language=uk, I see "Рекомендації" for the last section heading.

         

        So it looks like the language switching is working (I don't know Russian or Ukrainian so I'm just assuming it's working based on the fact that there is a difference between the two).

         

        Since this page has much more user-generated content, perhaps you are seeing posts that were made in one of the languages showing on this page no matter what language is set? Here is a good thread about machine-generated UGC translation you might want to look at if you are interested in looking into a way to translate your UGC.

         

        Also, might you have customizations that include hard-coded text in one language, instead of using text keys and providing translations, or might you have used text keys but haven't provided the translations yet? - something to check

         

        Finally, I believe the profile.language parameter only works for changing the language if the user is anonymous (not signed-in). If the user is signed in, it will use a "user setting" (also named profile.language) to determine the language. The best way to make sure that the language is changed for both anonymous and signed-in users will require you to add something to your Page Initialization script (via Studio) which sets the language using the http.request.setRequestLanguage Freemarker call. Here is an example call you might make:

         

         

        <#assign requestLanguage = http.request.parameters.name.get("profile.language", "none") />
        <#if requestLanguage != "none">
          ${http.request.setRequestLanguage(requestLanguage)}
        </#if>

         

        You need to make that call in your Page Initialization script for it to take effect on the page. If you want the language to persist for a signed in user across requests (so you don't have to keep passing the profile.language parameter around with each request), you might change your page init script to this:

         

         

        <#assign requestLanguage = http.request.parameters.name.get("profile.language", "none") />
        <#if requestLanguage != "none">
          ${http.request.setRequestLanguage(requestLanguage)}
          <#if !user.anonymous>
            <#assign userLang = rest("/users/id/" + user.id + "/profiles/name/language").value />
            <#if userLang != requestLanguage>
              ${rest("/users/id/" + user.id + "/profiles/name/language/set?value=" + requestLanguage)}
            </#if>
          </#if>
        </#if>

        Just by adding the profile.language query parameter to a request, it will set a cookie for anonymous (not signed-in) users that will persist across requests (so you shouldn't need to add any additional logic to handle anonymous users).

         

         

        I hope that helps!

         

        -Doug