MCPcopy Create free account
hub / github.com/comaps/comaps / google_translate

Function google_translate

tools/python/translate.py:116–143  ·  view source on GitHub ↗
(text, source_language)

Source from the content-addressed store, hash-verified

114 return key
115
116def google_translate(text, source_language):
117 fromTo = source_language.lower() + ':'
118 # Translate all languages with Google to replace failed DeepL translations.
119 for lang in GOOGLE_TARGET_LANGUAGES:
120 fromTo += lang + '+'
121 # Remove last +
122 fromTo = fromTo[:-1]
123 res = subprocess.run([TRANS_CMD, '-b', '-no-bidi', fromTo, text], text=True, capture_output=True)
124 if res.returncode != 0:
125 print(f'Error running {TRANS_CMD} program:')
126 print(res.stderr)
127 exit(1)
128
129 print('\nGoogle translations:')
130 translations = {}
131 i = 0
132 for line in res.stdout.splitlines():
133 lang = GOOGLE_TARGET_LANGUAGES[i]
134 if lang == 'zh-TW':
135 lang = 'zh-Hant'
136 elif lang == 'pt-PT':
137 lang = 'pt'
138 elif lang == 'zh-CN' or lang == 'zh':
139 lang = 'zh-Hans'
140 translations[lang] = line
141 i = i + 1
142 print(lang + ' = ' + line)
143 return translations
144
145def google_translate_one(text, source_language, target_language):
146 fromTo = source_language.lower() + ':' + target_language.lower()

Callers 1

translate.pyFile · 0.85

Calls 1

runMethod · 0.45

Tested by

no test coverage detected