Helpers.translate function Ui Builder

Tejaswi25
ServiceNow Employee
ServiceNow Employee

Hi All,

 

Can some one help me how to do translation in UI builder.

I want to populate value from the databroker.

how to use helpers.translate function.

2 REPLIES 2

Johns Marokky
Tera Guru

Hi @Tejaswi25 ,

I was able to find this document on helpers.translate. Hoping that this might help you.

This is used to translate the message based on the user session.

if you are trying to get the Value from Data Broker then you can use the below script and use that in the translate.

 

var dataBroker = api.data.<data_broker_name>.

Then passing this value to the Translate

helpers.translate(dataBroker);

I think this would work. Please check

 

helpers.translate(String message, String tokens)

Asynchronously retrieves and translates the specified message based on the current user's session language.

You can use this method with the api - setState(String stateParam, Any value) to bind the translated value to other fields on the page.

 

Note: You can call this method using a promise or async and await. The code examples below show both implementations.

 

Parameters
Name Type Description
messageStringMessage to translate.
tokensStringOptional. Comma-separated list of parameters to use for replacing string variables.

For example:

 

helpers.translate('Text {0} {1}', 'to', 'translate'); 
Returns
Type Description
StringTranslated text string.

Example

The following shows how to pass in table field references to embed in the corresponding variables in a string, using a promise.

 

function handler ({api, helpers}) {
  helpers.translate(‘Welcome {0} {1}!’, user.firstName, user.lastName)
    .then((translatedText) => {
      api.setState('greeting', translatedText);
    });
}

Example

The following shows how to use async and await in your function instead of a promise.
async function handler ({api, helpers}) {
  const translatedText = await helpers.translate('Welcome to {0}', 'ServiceNow');
    api.setState('greeting', translatedText);
}

Tejaswi25
ServiceNow Employee
ServiceNow Employee

Can you tell me how to use Translate method in data binding.

Tejaswi25_0-1676464320136.png