From 7510d521118c131adcd9cea3861b40b4048e0e19 Mon Sep 17 00:00:00 2001 From: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> Date: Mon, 3 Jul 2023 04:48:50 -0400 Subject: [PATCH 01/12] Create annotated_translation Signed-off-by: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> --- annotated_translation | 1 + 1 file changed, 1 insertion(+) create mode 100644 annotated_translation diff --git a/annotated_translation b/annotated_translation new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/annotated_translation @@ -0,0 +1 @@ + From 07e384a06ba2d84d704d960fb190b8c802d64f00 Mon Sep 17 00:00:00 2001 From: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> Date: Mon, 3 Jul 2023 05:38:02 -0400 Subject: [PATCH 02/12] Update annotated_translation Signed-off-by: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> --- annotated_translation | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/annotated_translation b/annotated_translation index 8b13789179..e763c529d6 100644 --- a/annotated_translation +++ b/annotated_translation @@ -1 +1,8 @@ +安装说明: +1.下载安装包 https://github.com/MatoApps/CommentTranslateTool/raw/master/CTT/setup.zip +2.解压并双击 setup.exe 安装 + +3.在ApiKey.cs中替换成你的值 +public const string _appKey = "{your app key}"; +public const string _appSecret = "{your app secret}"; From bc4fa2f6f781db765aa5b163d9977cd24372b27a Mon Sep 17 00:00:00 2001 From: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> Date: Fri, 14 Jul 2023 21:12:50 +0800 Subject: [PATCH 03/12] Update annotated_translation Signed-off-by: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> --- annotated_translation | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/annotated_translation b/annotated_translation index e763c529d6..2c90c0ff64 100644 --- a/annotated_translation +++ b/annotated_translation @@ -1,8 +1,3 @@ -安装说明: -1.下载安装包 https://github.com/MatoApps/CommentTranslateTool/raw/master/CTT/setup.zip - -2.解压并双击 setup.exe 安装 - -3.在ApiKey.cs中替换成你的值 -public const string _appKey = "{your app key}"; -public const string _appSecret = "{your app secret}"; +在main.py中替换成你的百度翻译api值 +appid = 'appid' +secretKey = 'key' From 6104adc07e4765c25536b729dabb87c152b4f033 Mon Sep 17 00:00:00 2001 From: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> Date: Fri, 14 Jul 2023 21:20:59 +0800 Subject: [PATCH 04/12] Rename annotated_translation to annotated_translationqqq Signed-off-by: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> --- annotated_translation => annotated_translationqqq | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename annotated_translation => annotated_translationqqq (100%) diff --git a/annotated_translation b/annotated_translationqqq similarity index 100% rename from annotated_translation rename to annotated_translationqqq From a95a0fa30b04c5d7a03a31010c5a6cd687059b6a Mon Sep 17 00:00:00 2001 From: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> Date: Fri, 14 Jul 2023 21:21:54 +0800 Subject: [PATCH 05/12] Create README.md Signed-off-by: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> --- annotated_translation/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 annotated_translation/README.md diff --git a/annotated_translation/README.md b/annotated_translation/README.md new file mode 100644 index 0000000000..2c90c0ff64 --- /dev/null +++ b/annotated_translation/README.md @@ -0,0 +1,3 @@ +在main.py中替换成你的百度翻译api值 +appid = 'appid' +secretKey = 'key' From b291403d864d141a7f3b58d4e6e957f6e9ff3317 Mon Sep 17 00:00:00 2001 From: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> Date: Fri, 14 Jul 2023 21:23:54 +0800 Subject: [PATCH 06/12] Add files via upload Signed-off-by: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> --- annotated_translation/main.py | 81 +++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 annotated_translation/main.py diff --git a/annotated_translation/main.py b/annotated_translation/main.py new file mode 100644 index 0000000000..7863611d78 --- /dev/null +++ b/annotated_translation/main.py @@ -0,0 +1,81 @@ +import hashlib +import random +import re +import time +import requests +import os + +# 百度api +apiurl = 'http://api.fanyi.baidu.com/api/trans/vip/translate' +appid = '20230714001744725' # 百度翻译api的appid +secretKey = 'y9WUHMVjoliF29rDWCNV' # 百度翻译api的secretKey + +# 读取文件,翻译成英语,返回一个翻译好的文档的每行的list +def text_deal(filename, fromLang='zh', toLang='en'): + file = open(filename, encoding="utf-8") + while 1: + lines = file.readlines(10000) + if not lines: + break + # 遍历每一行代码 + i = 0 + newline = [] + for line in lines: + i += 1 + lineat = line.lstrip() # 去除了左方空行 + # 判断是否是注释,不是注释则跳过 + linea = lineat[lineat.find('//'):] + # 查找中文字符 + chineses = find_chinese(linea) + if chineses: + print('-----------------------------------------------------') + print("[" + str(i) + "]: ", linea) + # 查找到中文字符,并调用翻译api翻译进行字符串替换 + for c in chineses: + time.sleep(1) + translate_str = translateBaidu(c, fromLang, toLang) + line = line.replace(c, translate_str) + print(c + " ----> " + translate_str) + print('-----------------------------------------------------') + newline.append(line) + file.close() + return newline + +# 查找文本中的中文字符,并将连续的中文字符以列表返回 +def find_chinese(text): + regStr = ".*?([\u4E00-\u9FA5]+).*?" + aa = re.findall(regStr, text) + return aa + +# 翻译内容源语言翻译后的语言 +def translateBaidu(content, fromLang='zh', toLang='en'): + salt = str(random.randint(32768, 65536)) + sign = appid + content + salt + secretKey # appid+q+salt+密钥的MD5值 + sign = hashlib.md5(sign.encode("utf-8")).hexdigest() # 对sign做md5,得到32位小写的sign + try: + # 根据技术手册中的接入方式进行设定 + paramas = { + 'appid': appid, + 'q': content, + 'from': fromLang, + 'to': toLang, + 'salt': salt, + 'sign': sign + } + response = requests.get(apiurl, paramas) + jsonResponse = response.json() # 获得返回的结果,结果为json格式 + dst = str(jsonResponse["trans_result"][0]["dst"]) # 取得翻译后的文本结果 + return dst + except Exception as e: + print(e) + +# 调用函数处理文档 +files = os.listdir("./translate") +for file in files: + print("=====================") + print("正在处理文件:" + file) + newlines = text_deal("./translate/" + file, "zh", "en") + fw = open("./translated/" + file, "a",encoding="utf-8") + for l in newlines: + fw.write(l) + fw.close() \ No newline at end of file From c520eb0422bb418bb4d682ed6b43dccbd85eddc6 Mon Sep 17 00:00:00 2001 From: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> Date: Fri, 14 Jul 2023 21:24:19 +0800 Subject: [PATCH 07/12] Create translate Signed-off-by: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> --- annotated_translation/translate | 1 + 1 file changed, 1 insertion(+) create mode 100644 annotated_translation/translate diff --git a/annotated_translation/translate b/annotated_translation/translate new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/annotated_translation/translate @@ -0,0 +1 @@ + From cd852dc3ca43d05ab0bdb06265f6dc45db3c9c8b Mon Sep 17 00:00:00 2001 From: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> Date: Fri, 14 Jul 2023 21:25:44 +0800 Subject: [PATCH 08/12] Create README.md Signed-off-by: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> --- annotated_translation/translated/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 annotated_translation/translated/README.md diff --git a/annotated_translation/translated/README.md b/annotated_translation/translated/README.md new file mode 100644 index 0000000000..2c9067dd9a --- /dev/null +++ b/annotated_translation/translated/README.md @@ -0,0 +1 @@ +Store translated files From 1c48b5ec63d37c9d85f9ac1ca572c3ce32472be6 Mon Sep 17 00:00:00 2001 From: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> Date: Fri, 14 Jul 2023 21:27:34 +0800 Subject: [PATCH 09/12] Update and rename translate to q Signed-off-by: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> --- annotated_translation/{translate => q} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename annotated_translation/{translate => q} (100%) diff --git a/annotated_translation/translate b/annotated_translation/q similarity index 100% rename from annotated_translation/translate rename to annotated_translation/q From 1cb72e7b7b70b84a6cef58f7dfca7cc9fa0a2326 Mon Sep 17 00:00:00 2001 From: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> Date: Fri, 14 Jul 2023 21:28:15 +0800 Subject: [PATCH 10/12] Create README.md Signed-off-by: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> --- annotated_translation/translate/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 annotated_translation/translate/README.md diff --git a/annotated_translation/translate/README.md b/annotated_translation/translate/README.md new file mode 100644 index 0000000000..801d541faf --- /dev/null +++ b/annotated_translation/translate/README.md @@ -0,0 +1 @@ +Store files to be translated From 6784e27a941a0194a597c5f90567932bf51b19c4 Mon Sep 17 00:00:00 2001 From: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> Date: Fri, 14 Jul 2023 21:29:16 +0800 Subject: [PATCH 11/12] Delete q Signed-off-by: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> --- annotated_translation/q | 1 - 1 file changed, 1 deletion(-) delete mode 100644 annotated_translation/q diff --git a/annotated_translation/q b/annotated_translation/q deleted file mode 100644 index 8b13789179..0000000000 --- a/annotated_translation/q +++ /dev/null @@ -1 +0,0 @@ - From 1584998837bfeaf6e729bd69ebeca7eb5aa5ca77 Mon Sep 17 00:00:00 2001 From: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> Date: Fri, 14 Jul 2023 21:29:44 +0800 Subject: [PATCH 12/12] Delete annotated_translationqqq Signed-off-by: YLJMYZH <98971341+YLJMYZH@users.noreply.github.com> --- annotated_translationqqq | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 annotated_translationqqq diff --git a/annotated_translationqqq b/annotated_translationqqq deleted file mode 100644 index 2c90c0ff64..0000000000 --- a/annotated_translationqqq +++ /dev/null @@ -1,3 +0,0 @@ -在main.py中替换成你的百度翻译api值 -appid = 'appid' -secretKey = 'key'