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

Method translate

dialect/providers/modules/bing.py:92–132  ·  view source on GitHub ↗
(self, request)

Source from the content-addressed store, hash-verified

90 raise UnexpectedError("Could not get HTML from bing.com")
91
92 async def translate(self, request):
93 src, dest = self.denormalize_lang(request.src, request.dest)
94
95 # Increment requests count
96 self._count += 1
97
98 # Form data
99 data = {
100 "fromLang": "auto-detect" if src == "auto" else src,
101 "text": request.text,
102 "to": dest,
103 "token": self._token,
104 "key": self._key,
105 }
106
107 # Do request
108 response = await self.post(self.translate_url, data, self._headers, True)
109
110 try:
111 data = response[0]
112 detected = None
113 pronunciation = None
114
115 if "translations" in data:
116 if "detectedLanguage" in data:
117 detected = data["detectedLanguage"]["language"]
118
119 if "transliteration" in data["translations"][0]:
120 pronunciation = data["translations"][0]["transliteration"]["text"]
121
122 return Translation(
123 data["translations"][0]["text"],
124 request,
125 detected=detected,
126 pronunciation=TranslationPronunciation(None, pronunciation),
127 )
128 else:
129 raise UnexpectedError("Unexpected translation response")
130
131 except Exception as exc:
132 raise UnexpectedError from exc
133
134 def check_known_errors(self, status, data):
135 if not data:

Callers

nothing calls this directly

Calls 5

TranslationClass · 0.90
UnexpectedErrorClass · 0.90
denormalize_langMethod · 0.80
postMethod · 0.80

Tested by

no test coverage detected