Forum Discussion
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
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>
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
- heavenerp11 years agoLeader
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
- HaidongG11 years agoLithium Alumni (Retired)
Hi Doug,
I am glad to hear that my code helps you.
btw: I have just tried to solve the issue with a different approach with Page initialization, you can find more code sample on this link http://lithosphere.lithium.com/t5/developers-discussion/Change-users-language-preference-with-REST-API/m-p/129671#M4970
Related Content
- 10 days ago
- 11 months ago
- 9 months ago
- 2 years ago