Python 实现C、C++程序注释英文翻译插件
1.安装必要依赖包
2.安装核心功能包translators 查看
3.编写文本输入输出函数
4.编写正则表达式分析文本内容
5.实现替换式翻译 或 跟随式翻译
6.展示案例
7.Keil实践提示
【实现】
import sys
import uuid
import requests
import hashlib
import time
import re
from importlib import reload
import chardet
import json
import codecs
import time
import translators as ts
def connect(q):
return str(ts.translate_text(q,'bing','auto','cn'))
def readtext(addr):
global content
content = str(open(addr, 'r',encoding='utf-8',errors="ignore").read())
#print("read: ",content)
def savetext(addr,str0):
open(addr, 'w',encoding='UTF-8',errors="ignore").write(str0)
def test_replace_0(match):
if match.group(1)!="null":
matchAllStr = match.group(0)
matchSubStr0 = match.group(1)
matchSubStr1 = match.group(2)
index0_start = match.span(0)[0]
index0_end = match.span(0)[1]
index1_start = match.span(1)[0]
index1_end = match.span(1)[1]
index2_start = match.span(2)[0]
index2_end = match.span(2)[1]
print("匹配内容:\t",matchAllStr,":",index0_start,":",index0_end,"\n翻译内容:\t",matchSubStr1,":",index2_start,":",index2_end)
#formatEndStr=str(matchAllStr[index_n:index2_start-index0_start] + connect(matchSubStr1) + matchAllStr[index2_end-index0_start:])
#formatEndStr=str(connect(matchAllStr,matchSubStr1,index2_start-index0_start,index2_end-index0_start))
formatEndStr=str(matchAllStr[0:index2_start-index0_start] + connect(matchSubStr1) + matchAllStr[index2_end-index0_start:])
print("翻译结果:\t",formatEndStr,"\r\n\r\n")
return formatEndStr
def test_add_0(match):
if match.group(1)!="null":
matchAllStr = match.group(0)
matchSubStr0 = match.group(1)
matchSubStr1 = match.group(2)
index0_start = match.span(0)[0]
index0_end = match.span(0)[1]
index1_start = match.span(1)[0]
index1_end = match.span(1)[1]
index2_start = match.span(2)[0]
index2_end = match.span(2)[1]
width = index2_start - index0_start - 2
if matchAllStr.find('\n',0,width) == -1:
index_n = 0
else:
index_n = matchAllStr.find('\n',0,width) + 1
index_temp = width - index_n
print("匹配内容:\t",matchAllStr,":",index0_start,":",index0_end,"\n翻译内容:\t",matchSubStr1,":",index2_start,":",index2_end)
formatEndStr=str(matchAllStr[index1_end - index0_start:index2_start-index0_start+1] + connect(matchSubStr1) + matchAllStr[index2_end-index0_start:])
print("翻译结果:\t",formatEndStr,"\r\n\r\n")
return matchAllStr + "\n" + (" " * index_temp) + formatEndStr
if __name__ == "__main__":
if len(sys.argv)>3:
old_content = open(sys.argv[1], 'rb').read()
print(chardet.detect(old_content))
source_encoding = chardet.detect(old_content).get('encoding')
if source_encoding != None:
if source_encoding != 'utf-8' and source_encoding != 'utf-8-SIG':
new_content = old_content.decode(source_encoding, 'ignore')
codecs.open(sys.argv[1], 'w', encoding='UTF-8').write(str(new_content))
#print("转换utf-8编码成功de-en")
readtext(sys.argv[1])
if sys.argv[3] == '1':
print("Content:\n",content,"\n***********************************************************\n")
FormatStr0 =re.sub(r'(\s*)//([^\r\n]*)',test_replace_0,content)
print("FormatStr0:\n",FormatStr0,"\n***********************************************************\n")
FormatStr1=re.sub(r'(\s)*/\*{1}([^/]*)\*/',test_replace_0,FormatStr0)
print("FormatStr1:\n",FormatStr1,"\n***********************************************************\n")
savetext(sys.argv[2],FormatStr1)
else:
print("Content:\n",content,"\n***********************************************************\n")
FormatStr0 =re.sub(r'(\s*)//([^\r\n]*)',test_add_0,content)
print("FormatStr0:\n",FormatStr0,"\n***********************************************************\n")
FormatStr1=re.sub(r'(\s)*/\*{1}([^/]*)\*/',test_add_0,FormatStr0)
print("FormatStr1:\n",FormatStr1,"\n***********************************************************\n")
savetext(sys.argv[2],FormatStr1)
【展示】
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
****************************************************************************
* 版权所有 (c) 2019, Nations Technologies Inc.
*
* 保留所有权利。
* ****************************************************************************
*
* 以源代码和二进制形式重新分发和使用,有或没有
* 修改,只要满足以下条件,就允许:
*
* - 重新分发源代码必须保留上述版权声明,
* 此条件列表和免责声明如下。
*
* 国家名称不得用于认可或推广源自
* 本软件未经事先明确书面许可。
*
*免责声明:本软件由各国“按原样”提供,任何明示或
* 默示保证,包括但不限于以下默示保证:
* 适销性、特定用途的适用性和不侵权是
*否认。在任何情况下,各国均不对任何直接、间接、
* 附带、特殊、惩戒性或后果性损害(包括但不限于
* 仅限于采购替代商品或服务;使用、数据、
* 或利润;或业务中断),但原因和任何理论
* 责任,无论是合同、严格责任还是侵权行为(包括
* 疏忽或其他)因使用本软件而以任何方式产生,
*即使被告知这种损害的可能性。
* ****************************************************************************/
/*------------------- IWDG registers bit mask ----------------------------*/
/*-------------------- IWDG 寄存器位掩码----------------------------*/
/* KEY register bit mask */
/* 按键寄存器位掩码*/
#define KEY_ReloadKey ((uint16_t)0xAAAA)
#define KEY_EnableKey ((uint16_t)0xCCCC)
//Enables or disables write access to IWDG_PR and IWDG_RLR registers.
//E启用或禁用对IWDG_PR和IWDG_RLR寄存器的写访问。
// IWDG_WriteAccess new state of write access to IWDG_PR and IWDG_RLR registers.
// IWDG_WriteAccess对IWDG_PR和IWDG_RLR寄存器的新写访问状态。
// This parameter can be one of the following values:
// 此参数可以是以下值之一:
/* Check the parameters */
/* 检查参数*/
/* Check the parameters */
/* 检查参数*/
/* Check the parameters */
/* 检查参数*/
/**
* @brief Checks whether the specified IWDG flag is set or not.
* @param IWDG_FLAG specifies the flag to check.
* This parameter can be one of the following values:
* @arg IWDG_PVU_FLAG Prescaler Value Update on going
* @arg IWDG_CRVU_FLAG Reload Value Update on going
* @return The new state of IWDG_FLAG (SET or RESET).
*/
/***
* @brief 检查是否设置了指定的 IWDG 标志。
* @param IWDG_FLAG指定要检查的标志。
* 此参数可以是以下值之一:
* @arg IWDG_PVU_FLAG 预分频器价值更新正在进行中
* @arg IWDG_CRVU_FLAG 重新加载值更新正在进行中
* @return IWDG_FLAG的新状态(设置或重置)。*/
/*****************************************************************************
* 版权所有 (c) 2019, Nations Technologies Inc.
*
* 保留所有权利。
* ****************************************************************************
*
* 以源代码和二进制形式重新分发和使用,有或没有
* 修改,只要满足以下条件,就允许:
*
* - 重新分发源代码必须保留上述版权声明,
* 此条件列表和免责声明如下。
*
* 国家名称不得用于认可或推广源自
* 本软件未经事先明确书面许可。
*
*免责声明:本软件由各国“按原样”提供,任何明示或
* 默示保证,包括但不限于以下默示保证:
* 适销性、特定用途的适用性和不侵权是
*否认。在任何情况下,各国均不对任何直接、间接、
* 附带、特殊、惩戒性或后果性损害(包括但不限于
* 仅限于采购替代商品或服务;使用、数据、
* 或利润;或业务中断),但原因和任何理论
* 责任,无论是合同、严格责任还是侵权行为(包括
* 疏忽或其他)因使用本软件而以任何方式产生,
*即使被告知这种损害的可能性。
* ****************************************************************************/
/*------------------- IWDG 寄存器位掩码----------------------------*/
/*按键寄存器位掩码*/
#define KEY_ReloadKey ((uint16_t)0xAAAA)
#define KEY_EnableKey ((uint16_t)0xCCCC)
//启用或禁用对IWDG_PR和IWDG_RLR寄存器的写访问。
//IWDG_WriteAccess对IWDG_PR和IWDG_RLR寄存器的新写访问状态。
//此参数可以是以下值之一:
/*检查参数*/
/*检查参数*/
/*检查参数*/
/**
* @brief 检查是否设置了指定的 IWDG 标志。
* @param IWDG_FLAG指定要检查的标志。
* 此参数可以是以下值之一:
* @arg IWDG_PVU_FLAG 预分频器价值更新正在进行中
* @arg IWDG_CRVU_FLAG 重新加载值更新正在进行中
* @return IWDG_FLAG的新状态(设置或重置)。*/
【提示】
1.参数3个,源文件、目标文件、翻译模式
2.Keil软件使用:查看
3.此文缺少访问超时等待续翻译代码段,暂时没空添加