The DynamicTranslation API provides methods that translate text, in real time, into multiple languages using translation service providers.

In addition, you can use this API to detect the language of a specific string and check whether the DynamicTranslation methods are enabled for a translation service. Use this API to create a seamless localization experience for your user interface, enabling one interface to service multiple countries.

Currently this API supports two translation service providers: Microsoft Azure Translator Service and Google Cloud Translator Service. You can also configure other translation services within your instance and then use the DynamicTranslation API to translate your text.

When using these methods in a server-side script, use the sn_dt_api namespace identifier. Before you are able to use this API, you must activate the DynamicTranslation (com.glide.dynamic_translation) plugin. For information on this plugin and additional information on Dynamic Translation, refer to Dynamic translation overview.

DynamicTranslation - getDetectedLanguage(String text, Object parms)

Detects the language of the passed in text.

If you pass in a translator, the method uses that translation service to detect the source language. Otherwise, the detection is performed by the default translation service. Ensure that the text strings that you provide contain enough verbiage to enable proper language detection.

In addition to the detected language, the response contains a confidence level of the detection, along with other possible language alternatives. If a translator is not passed in, the method also returns the default translation service used to detect the language.

Table 1. Parameters
Name Type Description
text String Text to use to detect the language.
parms Object Optional. JSON object that contains additional translation parameters.
parms.translator String Optional. Translation service to use to translate the text (not case-sensitive).

Valid values:

  • Google
  • Microsoft
  • IBM
  • <custom>
Note: To use custom translation services you must first configure the translation service in your instance. For details, see Integrate with a translation service provider.

Default: Translation service configured in the Translator Configuration [sn_dt_translator_configuration] table.

Example

This example shows code in a server-side script that detects a string in English using IBM's translation service.

var detectedResponse = sn_dt_api.DynamicTranslation.getDetectedLanguage('Please detect the language of this text', {"translator":'IBM'});
 gs.info(JSON.stringify(detectedResponse));

Output:

{
  detectedLanguage:
    { "code": "en", "confidence": "1", "name": "en" }
  alternatives: 
    [
      { "code": "vi", "confidence": "0.86", "name": "vi" },
      { "code": "id", "confidence": "0.86", "name": "id" }
    ]                  
 }

Example

This example shows a server script that throws an error when an invalid translation service is passed in.

var detectedResponse = sn_dt_api.DynamicTranslation.getDetectedLanguage('Please detect the language of this text', {"translator":123});
 gs.info(JSON.stringify(detectedResponse));

Output:

{"code":"40003","message":"Translator (\"translator\" field) is invalid"}

DynamicTranslation - getDetectedLanguages(Array texts, Object parms)

Detects the languages of the passed in text strings.

If you pass in a translator, the method uses that translation service to detect the source language. Otherwise, the detection is performed by the default translation service. Ensure that the text strings that you provide contain enough verbiage to enable proper language detection.

In addition to the detected language, the response contains a confidence level of the detection, along with other possible language alternatives. If a translator is not passed in, the method also returns the default translation service used to detect the language.

Table 3. Parameters
Name Type Description
texts Array List of text strings to use to detect the language(s).
parms Object Optional. JSON object that contains additional translation parameters.
"parms": {
  "translator": "String"
}
parms.translator String Translation service to use to detect the language of a string. Translation services are configured under the Translator Configuration menu and located in the Translator Configuration [sn_dt_translator_configuration] table.

Possible values - not case-sensitive:

  • Google
  • Microsoft
  • IBM
  • <custom>
Note: To use custom translation services you must first configure the translation service in your instance. For details, see Integrate with a translation service provider.

Default: Translation service configured in the Translator Configuration [sn_dt_translator_configuration] table.

Example

This example shows code in a server script that detects English as the language of the passed-in strings using the Microsoft translation service.

var detectedResponse = sn_dt_api.DynamicTranslation.getDetectedLanguages(["First text string language to detect", "Second text string language to detect"], {"translator": "Microsoft"});
gs.info(JSON.stringify(detectedResponse));

Output

{
  "translator":"Microsoft",
  "status":"Success",
  "detections":[
    {
      "isError":false,
      "detectedLanguage":{"name":"en", "code":"en", "confidence":"1"},
      "alternatives":[
        {"name":"id", "code":"id", "confidence":"0.83"},
        {"name":"ms", "code":"ms", "confidence":"0.83"}
      ]
    },
    {
      "isError":false,
      "detectedLanguage":{"name":"en", "code":"en", "confidence":"1"},
      "alternatives":[
        {"name":"fr", "code":"fr", "confidence":"0.83"},
        {"name":"id", "code":"id", "confidence":"0.83"}
      ]
    }
  ]
}

Example

This example shows code in a server script that returns a Partial status when two text strings are passed in and one of them is invalid.

var detectedResponse = sn_dt_api.DynamicTranslation.getDetectedLanguages(["First text string language to detect", ""], {"translator": "Microsoft"});
gs.info(JSON.stringify(detectedResponse));

Output

{
  "translator":"Microsoft",
  "status":"Partial",
  "detections":[
    {
      "isError":false,
      "detectedLanguage":{"name":"en", "code":"en", "confidence":"1"},
      "alternatives":[
        {"name":"id", "code":"id", "confidence":"0.83"},
        {"name":"ms", "code":"ms", "confidence":"0.83"}
      ]
    },
    {
      "isError":true,
      "code":"40000",
      "message":"Text is missing or invalid"
    }
  ]
}

Example

This example shows code in a server script that throws an error when an invalid translation service is passed in.

var detectedResponse = sn_dt_api.DynamicTranslation.getDetectedLanguages(["First text string language to detect", "Second text string language to detect"], {"translator": "123"});
gs.info(JSON.stringify(detectedResponse));

Output

{"code":"40003","message":"Translator (\"translator\" field) is invalid","status":"Error"}

DynamicTranslation - getTranslation(String textToTranslate, Object parms)

Translates the passed in text to one or more languages.

The method uses translation services, such as Microsoft Azure Translator Service and Google Cloud Translator Service, to perform the translation. If you do not pass in translation parameters, the method uses the system default.

Example

This example shows a server script translating plain text from English (detected) into French and Italian using Microsoft's translation service.

try {
  gs.info(JSON.stringify(sn_dt_api.DynamicTranslation.getTranslation("Translate this text using platform from server", {
    "targetLanguages": ["fr", "it"],
    "additionalParameters": [{
      "parameterName": "texttype",
      "parameterValue": "plain"
    }],
    "translator": "Microsoft"
  })));
} catch (error) {
    gs.info(error.message);
}

Response:

{"translations":[
    {
      "targetLanguage":"it",
      "translatedText":"Tradurre questo testo utilizzando la piattaforma dal server"
    },
    {
      "targetLanguage":"fr",
      "translatedText":"Traduire ce texte en utilisant la plate-forme à partir du serveur"
    }
  ],
  "translator":"Microsoft",
  "detectedLanguage":{"code":"en","name":"en"}
}

Example

This example shows a server script that throws an error when an invalid target language is passed in.

try {
    gs.info(JSON.stringify(sn_dt_api.DynamicTranslation.getTranslation("Translate this text using platform from server", {
        "targetLanguages": ["123"],
        "additionalParameters": [{
            "parameterName": "texttype",
            "parameterValue": "plain"
        }],
        "translator": "Microsoft"
    })));
} catch (error) {
    gs.info(error.message);
}

Response:

{"code":"40054","message":"Target language is invalid"}

DynamicTranslation - getTranslations(Array texts, Object parms)

Translates the passed in text strings to one or more languages.

The method uses translation services, such as Microsoft Azure Translator Service and Google Cloud Translator Service, to perform the translation. If you do not pass in translation parameters, the method uses the system default.

Example

This example shows code in a server-side script that translates a list of text strings into French and Italian using the Microsoft translation service.

try {
  gs.info(JSON.stringify(sn_dt_api.DynamicTranslation.getTranslations(["Translate first text using platform from server", "Translate second text using platform from server"], {
    "targetLanguages": ["fr", "it"],
    "additionalParameters": [{"parameterName": "texttype", "parameterValue": "plain"}],
    "translator": "Microsoft"
  })));
} catch (error) {
    gs.error(error.message);
}

Response:

{
  "translations":[
    {
      "isError":false,
      "textTranslations":[
        {
          "targetLanguage":"it",
          "translatedText":"Traduci il primo testo utilizzando la piattaforma dal server"
        },
        {
          "targetLanguage":"fr",
          "translatedText":"Traduire le premier texte à l'aide de la plate-forme à partir du serveur"
        }
      ],
      "detectedLanguage": {"name":"en", "code":"en"}
    },
    {
      "isError":false,
      "textTranslations":[
        {
          "targetLanguage":"it",
          "translatedText":"Traduci il secondo testo utilizzando la piattaforma dal server"
        },
        {
          "targetLanguage":"fr",
          "translatedText":"Traduire le deuxième texte à l'aide de la plate-forme à partir du serveur"
        }
      ],
      "detectedLanguage": {"name":"en", "code":"en"}
    }
  ],
  "translator":"Microsoft",
  "status":"Success"
}

Example

This example shows a server script that returns a Partial status when one of the two passed in text strings is invalid.

try {
  gs.info(JSON.stringify(sn_dt_api.DynamicTranslation.getTranslations(["Translate first text using platform from server", ""], {
    "targetLanguages": ["fr", "it"],
    "additionalParameters": [{"parameterName": "texttype", "parameterValue": "plain"}],
    "translator": "Microsoft"
  })));
} catch (error) {
    gs.error(error.message);
}

Response:

{
  "translations":[
    {
      "isError":false,
      "textTranslations":[
        {
          "targetLanguage":"it",
          "translatedText":"Traduci il primo testo utilizzando la piattaforma dal server"
        },
        {
          "targetLanguage":"fr",
          "translatedText":"Traduire le premier texte à l'aide de la plate-forme à partir du serveur"
        }
      ],
      "detectedLanguage":{"name":"en", "code":"en"}
    },
    {
      "isError":true,
      "code":"40000",
      "message":"Text is missing or invalid"
    }
  ],
  "translator":"Microsoft",
  "status":"Partial"
}

Example

This example shows a server script that throws an error when an invalid translation service is passed in.

try {
  gs.info(JSON.stringify(sn_dt_api.DynamicTranslation.getTranslations(["Translate first text using platform from server", "Translate second text using platform from server"], {
    "targetLanguages": ["fr", "it"],
    "additionalParameters": [{"parameterName": "texttype", "parameterValue": "plain"}],
    "translator": 123
  })));
} catch (error) {
    gs.error(error.message);
}

Response:

{"code":"40003","message":"Translator (\"translator\" field) is invalid","status":"Error"}

DynamicTranslation - isEnabled(String translator)

Determines whether the various methods in the DynamicTranslation API are enabled for a translation service.

If you pass in a specific translation service, the method checks the method activation for that translation service; otherwise the method checks the default translation service.

Table 9. Parameters
Name Type Description
translator String Optional. Translation service to use to verify whether the methods are active. Translation services are configured under the Translator Configuration menu.

Possible values - not case-sensitive:

  • Google
  • Microsoft
  • IBM
  • <custom>
Note: To use custom translation services you must first configure the translation service in your instance. For details, see Integrate with a translation service provider.

Default: Default translation service.

Example

This example shows a server script that checks whether the DynamicTranslation methods are active for the Microsoft translator.

try {
  var response = sn_dt_api.DynamicTranslation.isEnabled('Microsoft');
  gs.info(JSON.stringify(response));
} catch(error) {
    gs.info(error.message);
}

Output:

{"detection":true,"batchTranslation":true,"batchDetection":true,"translation":true}

Example

This example shows a server script that throws an error when an invalid translation service is passed in.

try {
  var response = sn_dt_api.DynamicTranslation.isEnabled(123);
  gs.info(JSON.stringify(response));
} catch(error) {
    gs.info(error.message);
}

Output:

{"code":"40003","message":"Translator (\"translator\" field) is invalid"}