How do I get the language the user has selected?

Wayne Richmond
Tera Guru

I need to translate the text used in HTML/XML fields and there doesn't appear to be a way to do this via the normal methods. I therefore want to include an if statement within the HTML/XML to change the text, however I don't know the function for getting the language the user has logged in with (via the language picker). I'm not a coder at all, sadly, so please excuse me, but the logic I want is this:

get the users/sessions language

if language is french display 'bonjour'

if language is spanish display 'hola'

else display 'hello'


Thanks

1 ACCEPTED SOLUTION

Hi Wayne,



Try this:


<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">


<p style="padding-left:13px; padding-top:10px;"><a onclick="saveCatAttachment(gel('sysparm_item_guid').value, 'incident')">




    <b>${gs.getMessage('Attach files')}</b> <img width="16" height="16" border="0" src="images/icons/attachment.gifx" title="${gs.getMessage('Attach any documents relating to your request')}"></img></a></p>


</j:jelly>



Then create 2 or more messages in the sys_ui_message table like this


Screen Shot 2015-08-22 at 11.50.21.png



The translation will be automatic based on the user language, as long as you have created the messages for each language. In the picture above i've added dutch translations so you can see how it works.


View solution in original post

10 REPLIES 10

Thijs Daemen
Mega Guru

Hi Wayne,



You can user gs.getSession().getLanguage() to retrieve the language of the current logged in user.


In order to translate specific pieces of your code (like error messages or information messages) you can use gs.getMessage() in server side scripts, or getMessage() in client side scripts.



HTML and text fields can be translated (if they are translated_html / translated_text types) by selecting another language in your UI, changing the contents of the field and saving the record. You can also directly go to the sys_translated_text table and make your modifications there.



There is no way to automatically translate fields that have been filled in by the user, like additional comments, work notes or other similar fields.




If you are trying to write code that automatically translates a greeting for the user that is logged in; gs.getMessage() and/or getMessage() are the way to go. You can use the sys_ui_message table to store the different translations.



Some interesting info on this: gs.getMessage() - The Second Parameter


Link to the wiki: GlideSystem - ServiceNow Wiki


Hi



Great reply, thank you. The field type is XML, not translated_html or translated_text (it's a UI Page I created for adding attachments to Record Producers).


Hi Wayne,



So does this answer your question or do you need further assistance? If your answer has been answered, could you mark this discussion as such, and if you need further assistance, could you maybe show us a screenshot or a snippet of what you are trying to achieve?



Thanks!


Hi



Well, you've told me how to get the language so that's answered



I will work on the script myself and see how I get on.



Thanks very much