Conversion_Kitchen_Code/kitchen_counter/conversion/translation/ek_new_file.py

44 lines
1.4 KiB
Python
Raw Permalink Normal View History

2024-04-27 09:33:09 +00:00
import os
import sys
from conversion.translation.translation_function import all_translator
from google.cloud import translate_v2 as Translate
# google api creds and setup
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/home/user/mnf/project//MNF/MNF/json_keys/authentication.json"
translate_client = Translate.Client()
def google(text, source_lang, target_lang):
try:
result = translate_client.translate(
text, source_language=source_lang, target_language=target_lang)
return result['translatedText']
except:
print("The text unable to translate is",text)
return text
f = open("/home/user/mnf/project/MNF/django.po", "r")
original_stdout = sys.stdout
g = open("/home/user/mnf/project/MNF/django_.po","w")
sys.stdout = g
appender = ""
for line in f:
# print(line[0:5])
if line[0:5] == "msgid":
output = all_translator(line[7:-2], "en", "hi").replace(""", "\"")
# output = google(line[7:-2], "en", "hi").replace(""", "\"")
g.write(line)
appender = output
elif line[0:6] == "msgstr":
if appender != "":
g.write(rf'msgstr "{appender}"')
# g.write("\n")
appender = ""
else:
g.write(rf'msgstr ""')
g.write("\n")
else:
g.write(line)
# print(line)
g.close()
sys.stdout = original_stdout