Forum Discussion

iftomkins's avatar
11 years ago

Get language setting of current node?

Good morning!

 

How would I get the language setting of the current node?

 

Here is a screenshot of the language setting in the Node's settings in the Admin area: https://www.dropbox.com/s/1ri15f4cl7wjtun/Screenshot%202014-02-05%2010.51.53.png. How do I access this language setting via the rest api?

 

To return the settings object for the node, so I can figure out what it consists of, I have this:

 

            <#assign nodeSettings = rest("/categories/id/${coreNode.id}/settings") />
            <#assign nodeSettingsHTML>
                <#list nodeSettings.settings.setting as setting>
                    console.log('${setting.name}');
                </#list>
            </#assign>

 

But it gives me this error:

Freemarker template 'Hitbox' processing failed

NonStringException:For "${...}" content: Expected a string or something automatically convertible to string (number, date or boolean), but this evaluated to a sequence+hash (wrapper: f.e.dom.NodeListModel):
==> setting.name  [in template "Hitbox" at line 62, column 56]

The failing instruction (print stack trace for 1 more):
==> ${setting.name}  [in template "Hitbox" at line 62, column 54]

 

KaelaCIt was fun seeing you on BART this morning! Any tips? As you might have guessed by this post, we're seeing which items on the Statement of Work we can do in-house, since the price estimate given to us was higher than we had anticipated.

  • HaidongG's avatar
    HaidongG
    Lithium Alumni (Retired)

    Hi iftomkins, 

     

    From your recent posts, it seems that you are building something similar to one of my recent projects: a language selection bar, which allows user to automatically switch to another language by a single click

     

    Just to share what I did,

     

    1) create an endpoint which can update user's profile language and expose itself as JSON.

    <#assign result_result = "error" />
    <#assign lang = http.request.parameters.name.get("lang", "")?trim />
    <#if user.registered && lang?? >
        <#assign update_lang_setting = rest("/users/self/profiles/name/language/set?value=${lang}") />
        <#if update_lang_setting.@status == 'success'>
            <#assign result_result = "success" />
        </#if>
    </#if>
    {"response":"${result_result}"}

     2) create a component, which calls that endpoint and refreshes the page when user clicks a particular language tab.

    LITHIUM.jQuery.ajax({
                type: "GET",
                url: "/plugins/custom/apac/apac/custom.apac.switch_language_edpt?lang=" + new_lang_code,
                dataType: "json",
                success: function (data) {
                    if(data.response == "success")    {
                        window.location.reload();
                    } else {
                       .........
                    }
                },

     

    The output is like 

     

    lang1.jpg

    lang2.jpg

     

    btw: for anonymous user, you has no user profile to update, you may just need to add  to load http://<your current URL>?profile.language=<new language code>

    • heavenerp's avatar
      heavenerp
      Leader

      Will definitely leverage this. Thanks!

       

      These endpoints and macros are areas I've never worked with before.  To everyone that has replied, thanks for the help!

       

      Doug

      • heavenerp's avatar
        heavenerp
        Leader

        It took a bit of tweaking, but I did get it working.  Again, thanks so much for your help!

         

        Inside the custom component I added an onclick element to the Language links of:

         

        <a href="#" onclick="changelanguage('en');">English</a>
        <a href="#" onclick="changelanguage('es');">Spanish</a>

         

         

         

        and at the bottom of the custom component added the tweaked script you provided

         

        <script type="text/javascript">
        function changelanguage(new_lang_code){
        LITHIUM.jQuery.ajax({
                                type: "GET",
                                url: "url_to_endpoint/endpoint_name?lang=" + new_lang_code,
                                dataType: "json",
                                success: function (data) {
                                    if(data.response == "success"){
                                        window.location.reload();
                                    }else{
                                       return false;
                                    }
                                }
                            });
        }
        </script>

         

         

        Works great!

         

        Will be live in our community soon.

         

        Doug

  • KaelaC's avatar
    KaelaC
    Lithium Alumni (Retired)

    You can use the settings context object like this:

     

    <#assign lang = coreNode.settings.name.get("profile.language", "en") />

     

    This will set lang to the language of this node with a default of English. 

     

    Good to see you this morning too!

    • iftomkins's avatar
      iftomkins
      Maven

      Awesome, thanks Kaela!

       

      As we had talked about, I'm hoping to check the language setting of the current code against the user's current language setting on page load. If they are different, I'd take the language setting of the node, and change the user's language setting. I believe I can now write the logic with freemarker, but I have a question:

       

      I tried accessing the REST API via jQuery before launch to change the user's profile setting. This worked on stage, but it didn't work on production. Here's an example call:

       

      jQuery.post('/restapi/vc/users/id/' + userId + '/settings/name/profile.language/set?value=de');

       

      If I do the same thing with freemarker, will it work on production? 

       

      <#assign response = restadmin('/restapi/vc/users/id/' + userId + '/settings/name/profile.language/set?value=de')/> 

      <#if response.hasError>
      <#-- handle error-->
      <#else>
      <#-- do cool stuff -->
      </#if>

       

      - Alan

       

       

       

      • KaelaC's avatar
        KaelaC
        Lithium Alumni (Retired)
        You can make the rest call like this:
        "/users/id/"+user.id+"/profiles/name/language/set?value=de"

        The rest looks good