| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  | """MyMemory Translated
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 16:36:04 +02:00
										 |  |  | """
 | 
					
						
							| 
									
										
										
										
											2016-09-06 15:40:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # about | 
					
						
							|  |  |  | about = { | 
					
						
							|  |  |  |     "website": 'https://mymemory.translated.net/', | 
					
						
							|  |  |  |     "wikidata_id": None, | 
					
						
							|  |  |  |     "official_api_documentation": 'https://mymemory.translated.net/doc/spec.php', | 
					
						
							|  |  |  |     "use_official_api": True, | 
					
						
							|  |  |  |     "require_api_key": False, | 
					
						
							|  |  |  |     "results": 'JSON', | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 18:02:47 +02:00
										 |  |  | engine_type = 'online_dictionary' | 
					
						
							| 
									
										
										
										
											2024-04-25 19:48:37 +02:00
										 |  |  | categories = ['general', 'translate'] | 
					
						
							| 
									
										
										
										
											2020-08-06 17:42:46 +02:00
										 |  |  | url = 'https://api.mymemory.translated.net/get?q={query}&langpair={from_lang}|{to_lang}{key}' | 
					
						
							|  |  |  | web_url = 'https://mymemory.translated.net/en/{from_lang}/{to_lang}/{query}' | 
					
						
							| 
									
										
										
										
											2016-09-06 15:40:07 +02:00
										 |  |  | weight = 100 | 
					
						
							| 
									
										
										
										
											2020-12-09 17:33:18 +01:00
										 |  |  | https_support = True | 
					
						
							| 
									
										
										
										
											2016-09-06 15:40:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 16:12:34 +02:00
										 |  |  | api_key = '' | 
					
						
							| 
									
										
										
										
											2016-09-06 15:40:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 15:44:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  | def request(query, params):  # pylint: disable=unused-argument | 
					
						
							| 
									
										
										
										
											2016-09-06 16:12:34 +02:00
										 |  |  |     if api_key: | 
					
						
							|  |  |  |         key_form = '&key=' + api_key | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         key_form = '' | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |     params['url'] = url.format( | 
					
						
							|  |  |  |         from_lang=params['from_lang'][1], to_lang=params['to_lang'][1], query=params['query'], key=key_form | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-09-06 15:40:07 +02:00
										 |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def response(resp): | 
					
						
							| 
									
										
										
										
											2024-10-15 15:39:14 +02:00
										 |  |  |     json_resp = resp.json() | 
					
						
							|  |  |  |     text = json_resp['responseData']['translatedText'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     alternatives = [match['translation'] for match in json_resp['matches'] if match['translation'] != text] | 
					
						
							|  |  |  |     translations = [{'text': translation} for translation in [text] + alternatives] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     result = { | 
					
						
							|  |  |  |         'answer': translations[0]['text'], | 
					
						
							|  |  |  |         'answer_type': 'translations', | 
					
						
							|  |  |  |         'translations': translations, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return [result] |