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