Python Examples

From Pangeanic
Revision as of 16:11, 3 December 2021 by Admin (talk | contribs) (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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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