MCPcopy Create free account
hub / github.com/dialect-app/dialect / translate

Method translate

dialect/providers/modules/lingva.py:82–105  ·  view source on GitHub ↗
(self, request)

Source from the content-addressed store, hash-verified

80 await self.init()
81
82 async def translate(self, request):
83 src, dest = self.denormalize_lang(request.src, request.dest)
84 # Format url query data
85 text = quote(request.text, safe="")
86 url = self.translate_url.format(text=text, src=src, dest=dest)
87
88 # Do request
89 response = await self.get(url)
90 try:
91 detected = response.get("info", {}).get("detectedSource", None)
92 mistakes = response.get("info", {}).get("typo", None)
93 src_pronunciation = response.get("info", {}).get("pronunciation", {}).get("query", None)
94 dest_pronunciation = response.get("info", {}).get("pronunciation", {}).get("translation", None)
95
96 return Translation(
97 response["translation"],
98 request,
99 detected,
100 TranslationMistake(mistakes, mistakes) if mistakes else None,
101 TranslationPronunciation(src_pronunciation, dest_pronunciation),
102 )
103
104 except Exception as exc:
105 raise UnexpectedError("Failed reading the translation data") from exc
106
107 async def speech(self, text, language):
108 (language,) = self.denormalize_lang(language)

Callers

nothing calls this directly

Calls 6

TranslationClass · 0.90
TranslationMistakeClass · 0.90
UnexpectedErrorClass · 0.90
denormalize_langMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected