import requests from skills.config import deepl_api_key from skills.config import google_api_key """ Reading material for this: https://www.deepl.com/en/docs-api https://cloud.google.com/translate/docs/overview """ class Translations: def __init__(self): self.trigger_phrase = "translate" # These vars would be good canidates for a future monitoring dashboard self.total_chars_translated = 0 self.deepl_chars_translated = 0 self.googl_chars_translated = 0 #only increment if api charged for the characters (read docs to figure this out) self.free_monthly_char_limit_googl = 500000 self.free_monthly_char_limit_deepl = 500000 self.supported_deepl_langs = ["list of supported languages"] def _get_language_code(self, query): return "FR" #TODO: Convert parse language from query and convert possibly into lang code depending on api requirements # Return None if language not recognized def _clean_up_text(self, query): return "cleaned up text" #TODO: remove characters that are not needed to translate text (apis charge per char sent, not per char translated) def translate_deepl(self, text, language): self.deepl_chars_translated += len(text) headers = { 'Authorization': 'DeepL-Auth-Key ' + deepl_api_key, 'Content-Type': 'application/json', } json_data = { 'text': [text], 'target_lang': language, } response = requests.post('https://api-free.deepl.com/v2/translate', headers=headers, json=json_data) return response def translate_google(self, text, language): self.google_chars_translated += len(text) return "" #TODO: add in google translate api, probably using python client library for google api def translate(self, text): parsed_text = self._clean_up_text(text) #TODO: Parse text and extract language and query from it, target_language = self._get_language_code(text) if target_language in self.supported_deepl_langs: return self.translate_deepl(parsed_text, target_language) if target_language is not None: return self.translate_google(text, target_language) return self.translate_google(text, "auto")