標籤:
Android項目開發過程中,容易出現缺少對應中英文翻譯的情況,這個Python指令碼是用於檢查字串是否缺少了對應的翻譯
1 #!/usr/bin/env python 2 # encoding: utf-8 3 4 import os, sys, getopt 5 import xml.dom.minidom 6 import subprocess 7 from xml.dom.minidom import Node 8 9 # 判斷是否是App項目依據10 Axml=‘AndroidManifest.xml‘11 12 res_en_string="res/values/strings.xml"13 res_cn_string="res/values-zh-rCN/strings.xml"14 15 # 檢查資源檔列表16 res_string_files=[res_en_string, res_cn_string]17 18 # Java調用字串資源清單19 find_string_called_by_java=‘‘‘find . -name .repo -prune -o -name .git -prune -o -type f -name "*\.java" -print0 | xargs -0 grep --color -n -o ‘R.string[0-9A-Za-z_.-]\+‘|awk -F‘:‘ ‘{print $3}‘|sort|uniq|xargs echo‘‘‘20 21 def _check_string_res(path):22 """檢查字串資源調用情況23 24 :path: TODO25 :returns: TODO26 27 """28 os.chdir(path)29 if not os.path.exists(Axml):30 return31 32 # 輸出提示33 print "\n### Processing Project: %s ..\n" % path34 35 # 獲得字串資源調用情況36 find_string_called_by_java_array = subprocess.Popen(find_string_called_by_java, shell=True, stdout=subprocess.PIPE).stdout.read().split(‘ ‘)37 38 # 逐個檢查資源檔(目前檢查中文、英文)39 for res_string_file in res_string_files:40 print ">>> Checking %s file .." % res_string_file41 42 # 解析xml檔案,並儲存已有資源到 names_had43 doc = xml.dom.minidom.parse(res_string_file)44 strings = doc.getElementsByTagName(‘string‘)45 names_had = []46 for string in strings:47 name = string.getAttribute(‘name‘)48 names_had.append(name)49 50 # 逐個檢查被調用的字串資源,不存在此資源時報Warning51 for check in find_string_called_by_java_array:52 c=check[9:].strip()53 if c not in names_had:54 print " - Warning: string name ‘%s‘ not found!!!" % c55 56 def usage(exitval=0):57 print "\nUsage: %s project_dir1 project_dir2 ..\n" % sys.argv[0]58 59 if __name__ == ‘__main__‘:60 if len(sys.argv) == 1:61 if os.path.isfile(Axml):62 _check_string_res(os.path.abspath(‘.‘))63 else:64 usage()65 elif len(sys.argv) > 1:66 for path in sys.argv[1:]:67 if os.path.isdir(path):68 _check_string_res(os.path.abspath(path))69 else:70 print "### %s Not a directory, ignored." % path71 else:72 usage()
使用方法:
./check_string_res.py packages/apps/Settings/
./check_string_res.py packages/apps/Settings/ packages/apps/QuickSearchBox/ ..
把對應缺少的字串補上翻譯就OK,避免缺少翻譯導致Android在切換語言之後出現崩潰的問題;
〖Android〗Android App項目資源字串檢查(檢查是否缺少對應的翻譯,導致系統切換語言後崩潰)