| 
									
										
										
										
											2022-06-22 09:08:19 +02:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | """Lingva (alternative Google Translate frontend)""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | about = { | 
					
						
							|  |  |  |     "website": 'https://lingva.ml', | 
					
						
							|  |  |  |     "wikidata_id": None, | 
					
						
							|  |  |  |     "official_api_documentation": 'https://github.com/thedaviddelta/lingva-translate#public-apis', | 
					
						
							|  |  |  |     "use_official_api": True, | 
					
						
							|  |  |  |     "require_api_key": False, | 
					
						
							|  |  |  |     "results": 'JSON', | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | engine_type = 'online_dictionary' | 
					
						
							| 
									
										
										
										
											2024-04-25 19:48:37 +02:00
										 |  |  | categories = ['general', 'translate'] | 
					
						
							| 
									
										
										
										
											2022-06-22 09:08:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-10 10:46:52 +01:00
										 |  |  | url = "https://lingva.thedaviddelta.com" | 
					
						
							| 
									
										
										
										
											2022-06-22 09:08:19 +02:00
										 |  |  | search_url = "{url}/api/v1/{from_lang}/{to_lang}/{query}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def request(_query, params): | 
					
						
							|  |  |  |     params['url'] = search_url.format( | 
					
						
							|  |  |  |         url=url, from_lang=params['from_lang'][1], to_lang=params['to_lang'][1], query=params['query'] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def response(resp): | 
					
						
							|  |  |  |     results = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-15 15:39:14 +02:00
										 |  |  |     result = resp.json() | 
					
						
							| 
									
										
										
										
											2022-06-22 09:08:19 +02:00
										 |  |  |     info = result["info"] | 
					
						
							|  |  |  |     from_to_prefix = "%s-%s " % (resp.search_params['from_lang'][1], resp.search_params['to_lang'][1]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if "typo" in info: | 
					
						
							|  |  |  |         results.append({"suggestion": from_to_prefix + info["typo"]}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if 'definitions' in info:  # pylint: disable=too-many-nested-blocks | 
					
						
							|  |  |  |         for definition in info['definitions']: | 
					
						
							| 
									
										
										
										
											2024-10-15 15:39:14 +02:00
										 |  |  |             for item in definition.get('list', []): | 
					
						
							|  |  |  |                 for synonym in item.get('synonyms', []): | 
					
						
							|  |  |  |                     results.append({"suggestion": from_to_prefix + synonym}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     data = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for definition in info['definitions']: | 
					
						
							|  |  |  |         for translation in definition['list']: | 
					
						
							|  |  |  |             data.append( | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     'text': result['translation'], | 
					
						
							|  |  |  |                     'definitions': [translation['definition']] if translation['definition'] else [], | 
					
						
							|  |  |  |                     'examples': [translation['example']] if translation['example'] else [], | 
					
						
							|  |  |  |                     'synonyms': translation['synonyms'], | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2022-06-22 09:08:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for translation in info["extraTranslations"]: | 
					
						
							|  |  |  |         for word in translation["list"]: | 
					
						
							| 
									
										
										
										
											2024-10-15 15:39:14 +02:00
										 |  |  |             data.append( | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     'text': word['word'], | 
					
						
							|  |  |  |                     'definitions': word['meanings'], | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2022-06-22 09:08:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-15 15:39:14 +02:00
										 |  |  |     if not data and result['translation']: | 
					
						
							|  |  |  |         data.append({'text': result['translation']}) | 
					
						
							| 
									
										
										
										
											2022-06-22 09:08:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     results.append( | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2024-10-15 15:39:14 +02:00
										 |  |  |             'answer': data[0]['text'], | 
					
						
							|  |  |  |             'answer_type': 'translations', | 
					
						
							|  |  |  |             'translations': data, | 
					
						
							| 
									
										
										
										
											2022-06-22 09:08:19 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return results |