MCPcopy
hub / github.com/emacs-eaf/emacs-application-framework / TranslateThread

Class TranslateThread

core/webengine.py:1888–1925  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1886 return name + "+" + domain + "+" + encode_path
1887
1888class TranslateThread(QThread):
1889
1890 fetch_result = QtCore.pyqtSignal(list)
1891
1892 def __init__(self, texts):
1893 QThread.__init__(self)
1894
1895 self.texts = texts
1896
1897 def get_command_result(self, command_string):
1898 import subprocess
1899 process = subprocess.Popen(command_string, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
1900 process.wait()
1901 result = process.stdout.readlines()
1902 return result
1903
1904 def run(self):
1905 if self.texts is None:
1906 message_to_emacs("Not fetch words, please try agian.")
1907 else:
1908 separator = "<meta name='google' content='notranslate'/>"
1909 text = ''.join(list(map(lambda t: f"{separator}{t}\n", self.texts)))
1910 self.cache_file = tempfile.NamedTemporaryFile(mode="w", delete=False)
1911 self.cache_file_path = self.cache_file.name
1912
1913 with open(self.cache_file_path, "w") as f:
1914 f.write(text)
1915
1916 result = self.get_command_result("crow -t 'zh-CN' --j -e 'google' -f {}".format(self.cache_file_path))
1917 translation = json.loads(''.join(list(map(lambda b: b.decode("utf-8"), result))))["translation"]
1918
1919 if os.path.exists(self.cache_file_path):
1920 os.remove(self.cache_file_path)
1921
1922 if len(translation) > 0:
1923 translates = translation.split(separator)[1:]
1924
1925 self.fetch_result.emit(translates)

Callers 1

immersive_translationMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected