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 cla...
  • DougS's avatar
    8 years ago

    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