Skip to page contentSkip to chat
ServiceNow support
    • Community
      Ask questions, give advice, and connect with fellow ServiceNow professionals.
      Developer
      Build, test, and deploy applications
      Documentation
      Find detailed information about ServiceNow products, apps, features, and releases.
      Impact
      Accelerate ROI and amplify your expertise.
      Learning
      Build skills with instructor-led and online training.
      Partner
      Grow your business with promotions, news, and marketing tools
      ServiceNow
      Learn about ServiceNow products & solutions.
      Store
      Download certified apps and integrations that complement ServiceNow.
      Support
      Manage your instances, access self-help, and get technical support.
Internationalise text inside a UI Script when it is a Widget Dependency - Known Error
  • >
  • Knowledge Base
  • >
  • Support and Troubleshooting (Knowledge Base)
  • >
  • Internationalise text inside a UI Script when it is a Widget Dependency
KB0727719

Internationalise text inside a UI Script when it is a Widget Dependency


1153 Views Last updated : Apr 7, 2024 public Copy Permalink
KB Summary by Now Assist

Issue

Description


This article explains how to internationalise a string inside a UI Script when that UI Script is being used as a dependency for a Service Portal widget.

The documented getMessage(messageKey, callback) method is not available on Service Portal, so instead we will use the i18n Angular module.

Please make sure you have followed the proper steps for creating a widget dependency and a Service Portal complaint UI Script by following these documentations first:

  • Create a widget dependency
  • Supported client script types and APIs

Procedure


UI Script

  1. Add a UI Script as a widget dependency.
  2. Create a function in the UI Script which accepts i18n as a parameter.
  3. Then you can use the getMessage(messageKey, callback) method from i18n to internationalise any strings that are defined in the sys_ui_message table.

Example:

function translateAlert(i18n) {
  text = 'Request Cancelled';    // message key

  i18n.getMessage('Request Cancelled', function(response) {
    alert(response);             // translated string
  });
}

 Client Script (Widget)

  1. In the Client Script section of the widget, add i18n as a service.
  2. Then call the function defined in the UI Script and pass the i18n service to it.

Example:

function(i18n) {         // added i18n as a service
  /* widget controller */
  var c = this;

  translateAlert(i18n);  // passing i18n service to the function in UI Script
}

Applicable Versions


Helsinki and above

Additional Information


Please note, that i18n.getMessage(messageKey, callback) is an asynchronous function, so something like the following example will not work:

function translateAlert(i18n) {
  text = 'Request Cancelled';
  translatedText = '';          // will store the response

  i18n.getMessage('Request Cancelled', function(response) {
    translatedText = response;  // try to store the response
  });

  console.log(translatedText);  // will print an empty string, expected the translated string
}

 In order to understand why this happens and how to get around this, please refer to the following links:

  • [StackOverflow] How do I return the response from an asynchronous call?
  • [StackOverflow] How to return value from an asynchronous callback function? [duplicate]
  • [MDN web docs] Callback function

The world works with ServiceNow.

Sign in for more! There's more content available only to authenticated users Sign in for more!
Did this KB article help you?
Did this KB article help you?

How would you rate your Now Support digital experience?

*

Very unsatisfied

Unsatisfied

Neutral

Satisfied

Very satisfied

Very unsatisfied

Unsatisfied

Neutral

Satisfied

Very satisfied

What can we improve? Please select all that apply.

What are we doing well? Please select all that apply.

Tell us more

*

Do you expect a response from this feedback?

  • Terms and conditions
  • Privacy statement
  • GDPR
  • Cookie policy
  • © 2025 ServiceNow. All rights reserved.