- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2023 03:52 PM
Not sure if this is the right place to put this, but I want to see if anyone has any ideas here.
We are looking for a way to automatically set the language of our Service Portal to something other than our SP's default language (English) if a user has something other than English set as their browser language.
How it is currently working :
1. Unauthenticated user visits our Service Portal for the very first time (so no session language/profile language) with a different language than English selected in their browser.
2. Service Portal defaults to English
How it should work :
1. Unauthenticated user visits our Service Portal for the very first time (so no session language/profile language) with a different language than English selected in their browser.
2. Service Portal defaults to the language that they have selected in their browser if it is a language that we support. If not then we can default to English.
Does anyone have any ideas on how we could achieve something like this?
Thank you
Joey
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2024 12:44 PM
At a high level this is what we ended up implementing -
1. Replace the header widget in our service portal theme with a new 'wrapper' widget. This widget performs our language logic and then calls in the actual header widget. We perform the logic in the wrapper so that the rest of the site loads in with the language set in the header wrapper widget.
2. In the header wrapper widget server script we do a few things -
- Check whether or not the user is starting a new session. We do this with client data called "newSession". If that client data has not been set yet then we know the user is starting a new session.
gs.getSession().getClientData("newSession")
- Check the user's browser language using -
GlideTransaction.get().getRequest().getHeader("Accept-Language")
- If the user is starting a new session and their browser language matches a language that we have enabled in ServiceNow then we update their language preferences using the following lines -
gs.getUser().setPreference("user.language", langVariableHere);
gs.getUser().savePreferences();
var util = new global.I18nUtils();
util.setLanguage(langVariableHere);
- We just map the language codes manually with some if statements. There were a couple outliers where ServiceNow has a different language code than what we saw in the browser so we had to account for those.
- Remember at the end of your server script to set the 'newSession' client data so that this code does not run again as the user continues their session.
3. In the header wrapper widget client script we also added in some logic that updates the URL to have the correct language language code if we are switching the site language. Without going line by line we essentially just use the $location object > find the language code in the URL > update it with the new language code found in the server script.
After that the user can use our language selector widget in the footer to switch to any other language if they didn't want to view the site in their browser language. I believe all of this logic gets overruled by ServiceNow's OOB logic if the user is signed in, though. I would need to double check that. It has been a while since we implemented this.
I took a lot of inspiration from this post that I found and then just tweaked the logic to fit our specific needs : https://www.servicenow.com/community/developer-articles/howto-automatic-server-side-language-selecti...
Hopefully this helps. Also, sorry the format of this post got a little weird, the code snippets broke up some bullet points.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2024 06:16 AM
I posted this idea so we could have this behaviour OotB : https://support.servicenow.com/ideas?id=view_idea&sysparm_idea_id=4c517bb347dd1e1048cb2920326d43fb&s...