<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>post [HowTo] Automatic Server Side Language Selection for the Login Page (internal + Portal) in Developer articles</title>
    <link>https://www.servicenow.com/community/developer-articles/howto-automatic-server-side-language-selection-for-the-login/ta-p/2301146</link>
    <description>&lt;P&gt;&lt;STRONG&gt;Business Case:&lt;/STRONG&gt; All ServiceNow Users should be presented with the Login Page in their native language&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Why this article:&lt;/STRONG&gt; All implementations i have found so far work with client side scripting and page reloads. This is a really bad user experience.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Why use the method shown in this article:&lt;/STRONG&gt; We simply set the guest-User language server side and do this by parsing the 'Accept-Language' header which the browser sends. This is done all known and relevant browsers (Source: &lt;A href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language" target="_blank" rel="noopener noreferrer"&gt;mdn&lt;/A&gt;).&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;How this method works:&lt;/STRONG&gt; There are two entry points to get to the login page:&amp;nbsp;&lt;BR /&gt;A) Access via "internal" link (e.g. /incident_list.do)&lt;BR /&gt;B) Access via portal link (e.g. /sp?id=approvals)&lt;/P&gt;&lt;P&gt;Setting the language for A) is easy, it only requires a small modification to the &lt;EM&gt;SPEntryPage&lt;/EM&gt; Script Include (and if not already in use, requires a certain System Property to be set). Modifying this Script Include is supported and recommended by ServiceNow (in short: &lt;EM&gt;Best Practice&lt;/EM&gt;).&lt;/P&gt;&lt;P&gt;Setting the language for B) is a little bit more difficult and might need more modifications, as every portal theme in-use has to be modified. In detail, we are going to replace the header used by the theme and will execute the "set-language" function *before* any other code is executed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Scoped App which includes all necessary scripts and has a built-in check for all required settings to implement this feature:&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/kr4uzi/ServiceNow-Automatic-Login-Language.git" target="_blank"&gt;https://github.com/kr4uzi/ServiceNow-Automatic-Login-Language.git&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;A) (Internal) modification:&lt;/STRONG&gt;&lt;BR /&gt;1.) Make sure that the sys_properties &lt;EM&gt;glide.entry.page.script&lt;/EM&gt; is set to &lt;EM&gt;new SPEntryPage().getLoginURL();&lt;BR /&gt;&lt;/EM&gt;2.) Modify the SPEntryPage Script Include like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;...
	getLoginURL: function() {
		var user = gs.getUser();
		if (user.getName() == 'guest') {
			var acceptLanguage = GlideTransaction.get().getRequest().getHeader("Accept-Language");
			if (acceptLanguage) {
				var isoLanguage = acceptLanguage.substring(0, 2);
				var choiceGr = new GlideRecord('sys_choice');
				choiceGr.addEncodedQuery('name=sys_user^element=preferred_language');
				choiceGr.addQuery('language', isoLanguage);
				choiceGr.setLimit(1);
				choiceGr.query();
				if (choiceGr.hasNext()) {
					user.setPreference("user.language", isoLanguage);
					user.savePreferences();
				}
			}
		}
...&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;B) (External) modification:&lt;/STRONG&gt;&lt;BR /&gt;1.) Identify the used theme:&lt;BR /&gt;a) Navigate to the Portal Record (sp_portal.list)&lt;BR /&gt;b) Open the Theme Record&lt;BR /&gt;c) Open the Header Record and save the Sys Id of it&lt;/P&gt;&lt;P&gt;2.) Create a new Header Widget (sp_header_footer.do)&lt;BR /&gt;a) Switch the the &lt;STRONG&gt;Global&lt;/STRONG&gt; &lt;STRONG&gt;Scope&lt;/STRONG&gt;, or any global scoped App&lt;BR /&gt;b) &lt;STRONG&gt;Save the record with the following values&lt;/STRONG&gt; (note: now you need the Sys Id of the OOTB Header Record):&lt;BR /&gt;&lt;STRONG&gt;Body HTML Template:&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;sp-widget widget="data.widget" /&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Server script:&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;(function() {
	var user = gs.getUser();
	if (user.getName() == 'guest') {
		var acceptLanguage = GlideTransaction.get().getRequest().getHeader("Accept-Language");
		if (acceptLanguage) {
			var isoLanguage = acceptLanguage.substring(0, 2);
			var choiceGr = new GlideRecord('sys_choice');
			choiceGr.addQuery('name', 'sys_user');
			choiceGr.addQuery('element', 'preferred_language');
			choiceGr.addQuery('language', isoLanguage);
			choiceGr.setLimit(1);
			choiceGr.query();
			if (choiceGr.hasNext()) {
				user.setPreference("user.language", isoLanguage);
				user.savePreferences();
			}
		}
	}
	
	data.widget = $sp.getWidget('&amp;lt;Sys ID of the OOTB Header Widget&amp;gt;', options);
})();&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3.) Modify the Theme Record from Step 1)&lt;BR /&gt;a) Replace the Header with the newly created Header Widget in Step 2)&lt;BR /&gt;b) Save the Record&lt;/P&gt;&lt;P&gt;4.) Done&lt;/P&gt;</description>
    <pubDate>Sat, 11 Nov 2023 23:02:18 GMT</pubDate>
    <dc:creator>Markus Kraus</dc:creator>
    <dc:date>2023-11-11T23:02:18Z</dc:date>
    <item>
      <title>[HowTo] Automatic Server Side Language Selection for the Login Page (internal + Portal)</title>
      <link>https://www.servicenow.com/community/developer-articles/howto-automatic-server-side-language-selection-for-the-login/ta-p/2301146</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Business Case:&lt;/STRONG&gt; All ServiceNow Users should be presented with the Login Page in their native language&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Why this article:&lt;/STRONG&gt; All implementations i have found so far work with client side scripting and page reloads. This is a really bad user experience.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Why use the method shown in this article:&lt;/STRONG&gt; We simply set the guest-User language server side and do this by parsing the 'Accept-Language' header which the browser sends. This is done all known and relevant browsers (Source: &lt;A href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language" target="_blank" rel="noopener noreferrer"&gt;mdn&lt;/A&gt;).&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;How this method works:&lt;/STRONG&gt; There are two entry points to get to the login page:&amp;nbsp;&lt;BR /&gt;A) Access via "internal" link (e.g. /incident_list.do)&lt;BR /&gt;B) Access via portal link (e.g. /sp?id=approvals)&lt;/P&gt;&lt;P&gt;Setting the language for A) is easy, it only requires a small modification to the &lt;EM&gt;SPEntryPage&lt;/EM&gt; Script Include (and if not already in use, requires a certain System Property to be set). Modifying this Script Include is supported and recommended by ServiceNow (in short: &lt;EM&gt;Best Practice&lt;/EM&gt;).&lt;/P&gt;&lt;P&gt;Setting the language for B) is a little bit more difficult and might need more modifications, as every portal theme in-use has to be modified. In detail, we are going to replace the header used by the theme and will execute the "set-language" function *before* any other code is executed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Scoped App which includes all necessary scripts and has a built-in check for all required settings to implement this feature:&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/kr4uzi/ServiceNow-Automatic-Login-Language.git" target="_blank"&gt;https://github.com/kr4uzi/ServiceNow-Automatic-Login-Language.git&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;A) (Internal) modification:&lt;/STRONG&gt;&lt;BR /&gt;1.) Make sure that the sys_properties &lt;EM&gt;glide.entry.page.script&lt;/EM&gt; is set to &lt;EM&gt;new SPEntryPage().getLoginURL();&lt;BR /&gt;&lt;/EM&gt;2.) Modify the SPEntryPage Script Include like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;...
	getLoginURL: function() {
		var user = gs.getUser();
		if (user.getName() == 'guest') {
			var acceptLanguage = GlideTransaction.get().getRequest().getHeader("Accept-Language");
			if (acceptLanguage) {
				var isoLanguage = acceptLanguage.substring(0, 2);
				var choiceGr = new GlideRecord('sys_choice');
				choiceGr.addEncodedQuery('name=sys_user^element=preferred_language');
				choiceGr.addQuery('language', isoLanguage);
				choiceGr.setLimit(1);
				choiceGr.query();
				if (choiceGr.hasNext()) {
					user.setPreference("user.language", isoLanguage);
					user.savePreferences();
				}
			}
		}
...&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;B) (External) modification:&lt;/STRONG&gt;&lt;BR /&gt;1.) Identify the used theme:&lt;BR /&gt;a) Navigate to the Portal Record (sp_portal.list)&lt;BR /&gt;b) Open the Theme Record&lt;BR /&gt;c) Open the Header Record and save the Sys Id of it&lt;/P&gt;&lt;P&gt;2.) Create a new Header Widget (sp_header_footer.do)&lt;BR /&gt;a) Switch the the &lt;STRONG&gt;Global&lt;/STRONG&gt; &lt;STRONG&gt;Scope&lt;/STRONG&gt;, or any global scoped App&lt;BR /&gt;b) &lt;STRONG&gt;Save the record with the following values&lt;/STRONG&gt; (note: now you need the Sys Id of the OOTB Header Record):&lt;BR /&gt;&lt;STRONG&gt;Body HTML Template:&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;sp-widget widget="data.widget" /&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Server script:&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;(function() {
	var user = gs.getUser();
	if (user.getName() == 'guest') {
		var acceptLanguage = GlideTransaction.get().getRequest().getHeader("Accept-Language");
		if (acceptLanguage) {
			var isoLanguage = acceptLanguage.substring(0, 2);
			var choiceGr = new GlideRecord('sys_choice');
			choiceGr.addQuery('name', 'sys_user');
			choiceGr.addQuery('element', 'preferred_language');
			choiceGr.addQuery('language', isoLanguage);
			choiceGr.setLimit(1);
			choiceGr.query();
			if (choiceGr.hasNext()) {
				user.setPreference("user.language", isoLanguage);
				user.savePreferences();
			}
		}
	}
	
	data.widget = $sp.getWidget('&amp;lt;Sys ID of the OOTB Header Widget&amp;gt;', options);
})();&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3.) Modify the Theme Record from Step 1)&lt;BR /&gt;a) Replace the Header with the newly created Header Widget in Step 2)&lt;BR /&gt;b) Save the Record&lt;/P&gt;&lt;P&gt;4.) Done&lt;/P&gt;</description>
      <pubDate>Sat, 11 Nov 2023 23:02:18 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-articles/howto-automatic-server-side-language-selection-for-the-login/ta-p/2301146</guid>
      <dc:creator>Markus Kraus</dc:creator>
      <dc:date>2023-11-11T23:02:18Z</dc:date>
    </item>
  </channel>
</rss>

