Python Examples: Difference between revisions

From Pangeanic
Jump to navigation Jump to search
Created page with "==Translate Function== This Python snippet can be used to translate a texts array '''texts'' from '''src''' language code to '''tgt''' language code using the engine with id '''engine''' and using an '''apiKey''' <br/> <pre> def relay(texts, src, tgt, engine, apiKey): url = 'https://prod.pangeamt.com:8443/NexRelay/v1/translate' data = { "src":src, "tgt":tgt, "apikey":apiKey, "engine": engine, "text": texts } headers = {'Content-type': 'application/j..."
 
Line 1: Line 1:
==Translate Function==
==Translate Function==
This Python snippet can be used to translate a texts array '''texts'' from '''src''' language code to '''tgt''' language code using the engine with id '''engine''' and using an '''apiKey'''
This Python snippet can be used to translate a texts array '''texts''' from '''src''' language code to '''tgt''' language code using the engine with id '''engine''' and using an '''apiKey'''
<br/>
<br/>



Revision as of 16:11, 3 December 2021

Translate Function

This Python snippet can be used to translate a texts array texts from src language code to tgt language code using the engine with id engine and using an apiKey

def relay(texts, src, tgt, engine, apiKey):
	url = 'https://prod.pangeamt.com:8443/NexRelay/v1/translate'
	data = {
			"src":src,
			"tgt":tgt,
			"apikey":apiKey,
			"engine": engine,
			"text": texts
			}
	headers = {'Content-type': 'application/json'}
	print(data)
	r = requests.post(url, data=json.dumps(data), headers=headers)
	ans=r.text
	try:
		translationresponse = json.loads(ans, strict=False)
	except:
		print(translationresponse)
	return translationresponse