Spaces:
Sleeping
Sleeping
File size: 1,639 Bytes
c732202 1d6b88f c732202 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
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"
|