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 th...
  • DougS's avatar
    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