Forum Discussion

Tyler's avatar
9 years ago

Retrieving Session Variable Profile.Language

I'm looking for some help accessing the session variable necessary to detect what language the user has set on their account. The session variable should already be set when they log in; I imagine that I just need to access it with a REST api call. The overall objective is to detect what language their account is in and redirect them to a URL with the appropriate language pack enabled.

 

Does anyone have any insight on creating the endpoint api call to check the profile.language setting?

  • How is your community set up language-wise? Are you using (or maybe want to use) different top-level categories for different languages and are looking to redirect the browser to the right category based on what language they are using in their profile, or something different than that? If it's that, you might want to check out Lithium's Top Level Categories functionality which is designed to segment off users sets into different top-level categories of the community (most implementations are based on language).

     

    As far as doing a redirect goes, I would recommend using the Studio Page Initialization Script (be careful b/c you can shoot yourself in the foot by doing something like redirecting all requests for any page somewhere else and then Lithium Support will have to get involved to fix things for you) and doing a check to find out what language the user has set (I'm assuming you have set the profile.language profile field for the user already) and redirecting them to the right place based on that. Here is some sample code you might add to the page initalization script:

     

    <#if page.name == "CommunityPage">
      <#assign langCategegoryMap = { 
      "en": "English",
      "fr": "Français",
      "es": "Español"
      } />
    
      <#function getLangCategoryId lang>
        <#if langCategegoryMap[lang]??>
          <#return langCategegoryMap[lang] /> 
        <#else>
          <#return "en" />
        </#if>
      </#function>
      <#assign langCatUrl = "" />
      <#attempt>
        <#assign lang = rest("/users/id/" + user.id + "/profiles/name/language").value />
        <#assign langCat = rest("/categories/id/" + getLangCategoryId(lang) + "?restapi.response_style=view").category />
        <#assign langCatUrl = langCat.@view_href />
      <#recover>
    <#-- just ignore and treat it like there is no such category --> </#attempt> <#if (langCatUrl?length > 0)> ${http.response.setRedirectUrl(langCatUrl)} </#if> </#if>

    -Doug

3 Replies

  • DougS's avatar
    DougS
    Khoros Oracle
    9 years ago

    How is your community set up language-wise? Are you using (or maybe want to use) different top-level categories for different languages and are looking to redirect the browser to the right category based on what language they are using in their profile, or something different than that? If it's that, you might want to check out Lithium's Top Level Categories functionality which is designed to segment off users sets into different top-level categories of the community (most implementations are based on language).

     

    As far as doing a redirect goes, I would recommend using the Studio Page Initialization Script (be careful b/c you can shoot yourself in the foot by doing something like redirecting all requests for any page somewhere else and then Lithium Support will have to get involved to fix things for you) and doing a check to find out what language the user has set (I'm assuming you have set the profile.language profile field for the user already) and redirecting them to the right place based on that. Here is some sample code you might add to the page initalization script:

     

    <#if page.name == "CommunityPage">
      <#assign langCategegoryMap = { 
      "en": "English",
      "fr": "Français",
      "es": "Español"
      } />
    
      <#function getLangCategoryId lang>
        <#if langCategegoryMap[lang]??>
          <#return langCategegoryMap[lang] /> 
        <#else>
          <#return "en" />
        </#if>
      </#function>
      <#assign langCatUrl = "" />
      <#attempt>
        <#assign lang = rest("/users/id/" + user.id + "/profiles/name/language").value />
        <#assign langCat = rest("/categories/id/" + getLangCategoryId(lang) + "?restapi.response_style=view").category />
        <#assign langCatUrl = langCat.@view_href />
      <#recover>
    <#-- just ignore and treat it like there is no such category --> </#attempt> <#if (langCatUrl?length > 0)> ${http.response.setRedirectUrl(langCatUrl)} </#if> </#if>

    -Doug

  • Tyler's avatar
    Tyler
    Ace
    9 years ago

    The community is set up just as you said, different top-level categories per language. I think you nailed it on helping with the implementation on the page initialization script as well. Thank you!

     

    The biggest problem I was having is knowing where to direct the api call, or what information is available to me and where. After reading your solution and searching a bit, I found this: 

     

    http://community.lithium.com/t5/Developer-Documentation/bd-p/dev-doc-portal?section=commv1&branch=User

     

    Is this a good resource to know what information is available via api? Is there a better source that you know of that I could use in the future?

     

    Thanks again!

  • DougS's avatar
    DougS
    Khoros Oracle
    9 years ago

    API V1 documentation  can be found here (the user resource is a good place to look for user-related API v1 calls): http://community.lithium.com/t5/Developer-Documentation/bd-p/dev-doc-portal?section=commv1

     

    API V2 documentation can be found here: http://community.lithium.com/t5/Developer-Documentation/bd-p/dev-doc-portal?section=commv2

     

    (start typing something in the search box in both API sections and you will notice it starts populating a large list of API calls and shortens that list the more specific you get)

     

    -Doug