Translating text on the fly

Using the translator text API, you can easily add translations to your application. The API allows you to automatically detect the language. This can be used to serve localized content, or to quickly translate content. It also allows us to look up alternative translations that can be used to translate words into different contexts.

In addition, the translator text API can be used to build customized translation systems. This means that you can improve the existing models. This can be done by adding existing human translations related to expressions and vocabulary in your industry.

The translator text API is available as a REST API. We will cover the four endpoints that you can reach. To use the API, the following root URL should be used:

https://api.cognitive.microsofttranslator.com

Sign up for an API key at Microsoft Azure Portal.

Translating text

To translate text from one language to another, you should call the following URL path:

/translate

The following parameters must be specified:

To - Language to translate to. Must be specified as two-letter language code.

This parameter can be specified multiple times.

The request body must contain the text that is to be translated.

A successful call will result in the following JSON output:

[
   {
      "detectedLanguage": {
         "language": "en",
         "score": 1.0
      },
      "translations": [
         "text": "Translated text",
         "to": "en"
      ]
   }
]

Converting text script

To translate text from one language script (such as Arabic) to another (such as Latin), you should call the following URL path:

/transliterate

The following parameters must be specified:

language - two-letter language code of language used in the language script.
fromScript - four-letter code for script language you are translating from.
toScript - four-letter code for script language you are translating to.

The request body must contain the text that is to be translated.

A successful call will result in the following JSON output:

[
   {
      "text": "translated text"
      "script": "latin"
   }
]

Working with languages

There are two paths that you can use when working with languages. The first one is used to detect language in a specific text. The second one is used to get a list of languages supported by the other APIs.

Detecting the language

To detect the language that a certain text uses, you should call the following URL path:

/detect

The request body must contain the text that is to be translated. No parameters are needed.

A successful call will result in the following JSON output:

[
   {
      "language": "en",
      "score": 1.0,
      "isTranslationSupported": true,
      "isTransliterationSupported": false,
      "alternatives": [
         {
            "language": "pt",
            "score": 0.8,
            "isTranslationSupported": false
            "isTransliterationSupported": false
         },
         {
            "language": "latn",
            "score": 0.7,
            "isTranslationSupported": true
            "isTransliterationSupported": true
         }
      ]
   }
]

Getting supported languages

To get a list of supported languages, you should call the following URL path:

/languages

No parameters or body are required for this call.

A successful call will result in the following JSON output:

[
   "translation": {
      ...
      "en": {
         "name": "English",
         "nativeName": "English",
         "dir": "ltr"
      },
      ...
   },
   "transliteration": {
      "ar": {
         "name": "Latin",
         "nativeName": "",
         "scripts": [
            {
               "code": "Arab",
               "name": "Arabic",
               "nativeName": "",
               "dir": "rtl",
               "toScripts": [
                  {
                     "code:" "Latn",
                     "name": "Latin",
                     "nativeName": "",
                     "dir": "ltr"
                  }
               ]
            },
            {
               "code": "Latn",
               "name": "Latin",
               "nativeName": "",
               "dir": "ltr",
               "toScripts": [
                  {
                     "code:" "Arab",
                     "name": "Arabic",
                     "nativeName": "",
                     "dir": "rtl"
                  }
               ]
            }
         ]
      },
      ...
   },
   "dictionary": {
      "af": {
         "name": "Afrikaans",
         "nativeName": "Afrikaans",
         "dir": "ltr",
         "translations": [
            {
               "name": "English",
               "nativeName": "English",
               "dir": "ltr",
               "code": "en"
            }
            ...
         ]
      }
      ...
   }
]

As you can see, the two-letter country code is the key for each entry. You can also find the four-letter code for each transliterate language. This API path can be used as a basis for the other API paths.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.103.154