Spaces:
Sleeping
Sleeping
import json | |
from pathlib import Path | |
from errorlog.errorlog import log_error | |
from deep_translator import GoogleTranslator | |
def isSettingsFileAvailable(): | |
current_dir = Path(__file__).resolve().parent | |
file_path = current_dir.parent / 'settings.json' | |
try: | |
if file_path.exists() and file_path.is_file(): | |
with file_path.open('r') as file: | |
settings = json.load(file) | |
return settings | |
else: | |
return "Settings file is not found" | |
except Exception as err: | |
return "Issue reading the settings file" | |
finally: | |
if "file" in locals() and not file.closed: | |
file.close() | |
def isTargetLanguageSupported(target_langcode): | |
try: | |
settings_config = isSettingsFileAvailable() | |
language_config = settings_config.get('language_supported','') | |
if language_config and target_langcode in language_config: | |
return True | |
else: | |
log_error(f"Language ---{target_langcode}--- provided is not supported as per settings") | |
return False | |
except Exception as ex: | |
log_error(str(ex)) | |
return False | |
def translate_text(text, language): | |
try: | |
isLanguageSupported = isTargetLanguageSupported(language) | |
if isLanguageSupported: | |
translated_text = GoogleTranslator(source='auto', target=language).translate(text) | |
return translated_text | |
else: | |
return f"Language ---{language}--- provided is not supported as per settings" | |
except Exception as ex: | |
log_error(str(ex)) | |
return "Error processing the request" | |