
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
08-01-2024 01:05 PM - edited 05-14-2025 07:49 AM
Dynamic Translate Use Case
I recently had a customer where there was a transition from catalog item to incident. The variables were sent into the work notes. However, they also had people across the world putting data in different lanugages. The english speaking fulfillers couldn't read the concatinated worknotes. As a result, I came up with a utility Script Include to take any string and run the dynamic translation API calls over it to:
- Detect any language of the string passed in
- Translate the string to desired language if not already in that language
Dynamic Translate Script Include
var DynamicTranslateStringUtil = Class.create();
DynamicTranslateStringUtil.prototype = {
initialize: function() {
},
translateString: function(stringText, translateTo){
try{
//translation answer.name into translateTo language (e.g. "en") no matter what the orginal language was using Dynamic Translation API
//using API: https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server/sn_dt_api-namespace/DynamicTranslation#DynTran-getDetectedLanguage_S_O?navFilter=dynamicTran
var translationCodeResponse = sn_dt_api.DynamicTranslation.getDetectedLanguage(stringText, {"translator": "Microsoft"});
//example from API documentation just prints this - however I wanted to crawl to specific values and needed to parse it
var parser = new JSONParser();
//parsing was failing so I had to JSON.stringify the response from the API, which seems to have just added double quotes around some values, but allowed the parsing
var parsedData = parser.parse(JSON.stringify(translationCodeResponse));
var code = parsedData.detectedLanguage.code;
//if language code is translateTo don't need to translate
if(code!=translateTo){
var translatedText = sn_dt_api.DynamicTranslation.getTranslation(stringText, {
"targetLanguages": [code, translateTo],
"additionalParameters": [{
"parameterName": "texttype",
"parameterValue": "plain"
}],
"translator": "Microsoft"
});
//again had to parse and stringify the results in order to parse the JSON coming back
translatedParsed = new JSONParser().parse(JSON.stringify(translatedText));
//first record is the orginal text so skipping to index 1 with translated text
translatedTextToLanguage = translatedParsed.translations[1].translatedText;
stringText=translatedTextToLanguage;
}
}catch(e){
//I like to just append the class name in so if there is an error its obvious where it came from
gs.error("DynamicTranslateStringUtil: " + e.toString());
}
//end translation to toLanguage
return stringText;
},
type: 'DynamicTranslateStringUtil'
};
and then to call this script we simply do something like this:
var translationUtil = new DynamicTranslateStringUtil();
var englishString = translationUtil.translateString("<some text in here>", "en");
I commented where I ran into some issues that took time to figure out and figured I'll post it and see if anyone else needs something like this. Attached is the update set with the script include!
- 1,092 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
love it, thanks for this awesome idea @Andrew Swallows 👍