背景
現(xiàn)在市面上大部分應(yīng)用都做了國(guó)際化,中英文雙語(yǔ),對(duì)于大型應(yīng)用可能有好幾百頁(yè)面够掠,成千上萬(wàn)種場(chǎng)景,僅靠功能測(cè)試去檢測(cè)英文翻譯的正確性可能覆蓋面不夠全茄菊,且人眼看大段英文極易看錯(cuò)疯潭。所以開(kāi)發(fā)腳本來(lái)做這個(gè)工作
方法
1.每個(gè)頁(yè)面遍歷UI控件獲取text檢測(cè),這種方式效率較低面殖,且也是很多場(chǎng)景異常提示等不容易觸發(fā)到而遺漏
2.直接檢測(cè)整個(gè)代碼文件夾竖哩,遍歷所有翻譯文件查看英文拼寫(xiě)錯(cuò)誤,這種方式效率高畜普,不容易遺漏期丰,但也有個(gè)缺點(diǎn)群叶,無(wú)法檢測(cè)到哪些沒(méi)有翻譯吃挑,只能檢測(cè)到已經(jīng)翻譯的是否拼寫(xiě)正確
其中Android:掃描String_XXX.xml文件,并解析xml
IOS:掃描.strings后綴文件街立,并讀取解析數(shù)據(jù)
腳本和代碼
庫(kù):python庫(kù) 安裝 pyenchant
pip install pyenchant 如果這種方式安裝不上舶衬,可嘗試直接在pycharm插件中安裝
主要腳本代碼SpellCheck.py
from enchant.checker import SpellChecker
import sys
import os
import xml.etree.cElementTree as eT
#cmd運(yùn)行時(shí)報(bào)模塊找不到,所以把當(dāng)前目錄的其它模塊導(dǎo)入
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)
from tools.Logger import Logger
def check_string(dir_path, src_type):
file_list = os.listdir(dir_path)
for cur_file in file_list:
# 獲取文件的絕對(duì)路徑
path = os.path.join(dir_path, cur_file)
if os.path.isfile(path):
if is_android_lang_file(cur_file): # 判斷是否是Android字符配置文件
check_android_english(path)
if os.path.isdir(path) and not cur_file.startswith('.'):
check_string(path, src_type) # 遞歸子目錄
def check_android_english(file_path):
if file_path.__contains__('values-zh'):
return
tree = eT.parse(file_path)
root = tree.getroot()
checker = SpellChecker('en_US')
strings = root.findall('.//string')
logger.info('開(kāi)始檢查 {0}-錯(cuò)誤如下:'.format(file_path))
for string in strings:
if not string.text or check_contain_chinese(string.text):
return
checker.set_text(string.text)
for error in checker:
logger.info(error.word)
def is_android_lang_file(cur_file):
file_name = os.path.splitext(cur_file)[0]
file_suffix = os.path.splitext(cur_file)[1]
if file_suffix == '.xml' and file_name.startswith('string'):
return True
else:
return False
def check_contain_chinese(check_str):
for ch in check_str:
if u'\u4e00' <= ch <= u'\u9fff':
return True
return False
if __name__ == '__main__':
logger = Logger(logger='CheckEnglishString').get_log()
check_string(sys.argv[1], 'android')
檢查IOS代碼及RN的代碼中英文配置文件拼寫(xiě)問(wèn)題
def check_ios_english(file_path):
# f = open(file_path, 'rb', encoding='UTF-8')
logger.info('開(kāi)始檢查 {0} 錯(cuò)誤如下:'.format(file_path))
with open(file_path, 'r', encoding='unicode_escape') as fp:
lines = fp.readlines()
checker = SpellChecker('en_US')
for string in lines:
words = string.split('=', 1)
if len(words) > 1:
word = words[1].replace('"', '')
if not word or check_contain_chinese(word):
return
checker.set_text(word)
for error in checker:
logger.info(error.word + ' 【' + word+'】')
def check_rn_english(file_path):
# f = open(file_path, 'rb', encoding='UTF-8')
logger.info('開(kāi)始檢查 {0} 錯(cuò)誤如下:'.format(file_path))
with open(file_path, 'r', encoding='unicode_escape') as fp:
lines = fp.readlines()
checker = SpellChecker('en_US')
for string in lines:
words = string.split(':', 1)
if len(words) > 1:
word = words[1].replace('"', '')
word = words[1].replace("'", "")
if not word or check_contain_chinese(word):
return
checker.set_text(word)
for error in checker:
logger.info(error.word + ' 【' + word + '】')
輸出日志及結(jié)果的Logger代碼
# coding : utf-8
import logging
import os
import time
class Logger(object):
def __init__(self, logger):
self.logger = logging.getLogger(logger)
self.logger.setLevel(logging.INFO)
current_time = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
cur_path = os.path.abspath(os.path.dirname(__file__))
root_path = os.path.split(cur_path)[0]
log_name = root_path + '\\result\\' + current_time + '.txt'
file_handler = logging.FileHandler(log_name)
file_handler.setLevel(logging.INFO)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
formatter = logging.Formatter()
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)
self.logger.addHandler(file_handler)
self.logger.addHandler(console_handler)
def get_log(self):
return self.logger;
代碼目錄
目錄
生成的結(jié)果放在result中赎离,會(huì)依次遍歷所有文件并打印出哪個(gè)文件下的錯(cuò)誤英文拼寫(xiě)
待優(yōu)化
1.需要加入過(guò)濾單詞庫(kù)逛犹,有些縮寫(xiě)及公司內(nèi)部專(zhuān)用英文縮寫(xiě)需要過(guò)濾