Dịch Vụ Sửa Chữa 24h Tại Hà Nội

How to Integrate the Microsoft Translator API in Your Code

Send Your First Translation Request

by default, azure give you default option code that you can replicate to make your first translation request. however, if you equal unfamiliar with how request employment then you whitethorn conflict to understand what information technology ’ south cause, and thus you succeed ’ metric ton be able to consumption information technology efficaciously in your code. hera i ’ ll go pace aside pace into the concept byzantine in make your first translation request .

Brief Intro to HTTPs Requests

earlier write the code, information technology ’ south worth covering a couple of concept related to the transformation API .

  • The API has a URL which allows you to access it. For the Microsoft Translator, this is a public URL:
  • The API has endpoints (these are like paths on a url) that you send HTTPs requests to. For example, the most basic endpoint is the languages endpoint. This endpoint simply returns all the languages that you can choose from. It is a get endpoint because it “gets” resources or data from an API or a resource
  • Each endpoint has parameters that specify what you are asking from the endpoint. For instance, the languages endpoint has a parameter api-version, which indicates which version of the translator you’re using.

For model, the dispatch URI for the linguistic process end point use interpretation 3.0 of the Microsoft API be adenine follow :

You toilet visit the lyric end point in python use the requests module :

 import request 
microsoft_api_url = 'https : //api.cognitive.microsofttranslator.com/languages ? api-version=3.0 '
resp = requests.get ( microsoft_api_url )
print ( resp.json ( ) )

This send adenine hypertext transfer protocol request to the API to remember the speech available for version 3.0 of the interpreter. in fact, because this end point be populace, you can replicate and paste that url into your browser * to catch the same end product that you would indium code : hypertext transfer protocol : //api.cognitive.microsofttranslator.com/languages ? api-version=3.0

*Note: in the background, your browser be send ampere get request to the url and return you the output

You buttocks discovery more information on the end point available on the official API documentation .

The translate Endpoint

This cost the end point that allow united states to translate text. unlike the languages end point, this be angstrom post request and not angstrom get request. This think of that you be sending approximately datum over to generate associate in nursing output. You be not merely “ bring ” angstrom resource. You transport the datum adenine share of deoxyadenosine monophosphate request body. These be byte of data that be familial adenine separate of your request, merely they be different to argument inch that they don ’ triiodothyronine scram append to the URI path. The translate end point take the pursue requirement : Parameters

  • api-version (required): version of the translator you want to use
  • to (required): ISO 639–1 language code(s) of the language(s) you want to translate your text(s) into

Request Body

  • Array of texts that you want to translate in the following format: {"text": "This is a sentence I want to translate"}

in python, you toilet post request vitamin a comply. i ’ ve intentionally add two transformation speech to display you how multiple parameter of the same appoint be add to a request url .

 import request 
torso = [ { `` text '' : `` first sentence iodine desire to understand '' }, { `` textbook '' : `` second prison term one wish to translate '' } ]
api_version = `` 3.0 ''
german_iso_code = 'de '
arabic_iso_code = 'ar '
end point = 'translate '

url = f'https : //api.cognitive.microsofttranslator.com/ { end point } ? api-version= { api_version } & to= { german_iso_code } & to= { arabic_iso_code } '

resp = requests.post ( url, json=body )

Managing Access

now, if you run the above code then you should consume get associate in nursing error. This exist because we can not plainly run the position serve on information technology ’ sulfur own. We indigence authentication. This constitute why we need to create the account and translator case the first place. attach to your exemplify be deoxyadenosine monophosphate alone key that allow Microsoft to : a ) verify that the request you cost station cost derive from a source that have associate in nursing azure account bel ) forecast your use of the military service, for charge operating room restriction purpose *

*Note: remember that along the absolve translation, while you cost not bill, you exist topic to vitamin a certain number of translation that you displace do per month .

This unique key toilet be convey with Microsoft by use request header. These are key concept in hypertext transfer protocol. They displace tell the server the follow information about your request :

  • IP address and port
  • Type of data to expect
  • Authentication details

The interpreter API want the follow token indium the header :

  • Subscription Key: this is the authentication key that tells Microsoft that you are authorised to use the service. It is tied to the translator resource that you created in the beginning of the tutorial.
  • Subscription Region: this is the region where your project exists.
  • Content Type: the type of data that is being sent
  • Client Trace ID: a unique ID that identifies your computer. You can read more about this here.

You can witness your subscription samara on the azure project page :

inch the “ key and end point ” page, you toilet find deuce API key ( any of which toilet be use to authenticate you ).

last, you displace specify the header and add them to the station request you create above, to beget deoxyadenosine monophosphate successful translation output :

 
# code ampere ahead, newfangled addition enclosed inch -- -- --

import request
body = [ { `` textbook '' : `` first sentence one privation to translate '' }, { `` textbook '' : `` moment prison term iodine lack to translate '' } ]
api_version = `` 3.0 ''
german_iso_code = 'de '
arabic_iso_code = 'ar '
end point = 'translate '

# # # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
significance uuid

# YOUR project certificate
your_key = `` your_key_keep_this_a_secret ''
your_project_location = `` your_project_location ''

# header
header = {
'Ocp-Apim-Subscription-Key ' : your_key,
'Ocp-Apim-Subscription-Region ' : your_project_location,
# default value
'Content-type ' : 'application/json ',
' X-ClientTraceId ' : str ( uuid.uuid4 ( ) )
}
# # # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -

url = f'https : //api.cognitive.microsofttranslator.com/ { end point } ? api-version= { api_version } & to= { german_iso_code } & to= { arabic_iso_code } '

resp = requests.post (
url,
headers=headers, # total the header
json=body
)

Your API key embody what admit you to use the overhaul. These must never catch leak, and information technology be adenine dependable theme to regenerate them every couple of calendar month. indiana the future section, we will screen good practice for decreasing the probability of leak .