ios 应用程序,本地化脚本。

虽然apple提供了genstrings来根据宏定义找到,需要本地化的字符串。但是每次手工的去写文件是件很费神也很容易出错的事情。索性就拿python简单写了一个脚本。

  1 import codecs
  2 import os
  3 import traceback
  4 import sys
  5 reload(sys)
  6 
  7 def findFile(projectHome,fileName):
  8     aimPath = "xx"
  9     for i in os.listdir(projectHome):
 10      if not cmp(i,".git"):
 11          continue
 12      if not cmp(i,"WizIosSDK"):
 13          continue
 14      currentPath = projectHome + '/' + i 
 15      if not cmp(i,fileName):
 16          return currentPath
 17      if os.path.isdir(currentPath):
 18          path = findFile(currentPath,fileName)
 19          if cmp (path, "xx"):
 20           aimPath = path
 21     return aimPath
 22 
 23 sys.setdefaultencoding('utf-8')
 24 projectHome = os.getcwd()
 25 outPutDir = findFile(projectHome,"en.lproj") 
 26 
 27 def genstrings(level, path):
 28     for i in os.listdir(path):
 29         if not cmp (i, '.git'):
 30         continue
 31         if os.path.isdir(path+'/'+i):
 32             x = 'genstrings -a -o '+ outPutDir+' ' +path + '/'+i+'/*.m'
 33             print x
 34             os.system(x)
 35             os.system('genstrings -a -o '+ outPutDir+' ' +path + '/'+i+'/*.h')
 36             os.system('genstrings -a -o '+ outPutDir+' ' +path + '/'+i+'/*.mm')
 37             genstrings(level+1,path+'/'+i)
 38 genstrings(0,projectHome)
 39 
 40 def fileDictionary(filePath, isAddPlaceHolder,fileEncode):
 41     fileDic = {}
 42     fileContent = codecs.open(filePath,'r',fileEncode)
 43     for line in fileContent.readlines():
 44     try:
 45         if(len(line) == 0):
 46         continue
 47         strr = line.split('=')
 48         if(len(strr) == 1):
 49         continue
 50         en = strr[0]
 51         en = en[en.index('"')+1:len(en)]
 52         en = en[0:en.index('"')]
 53         zh = strr[1]
 54         zh = zh[zh.index('"')+1:len(zh)]
 55         zh = zh[0:zh.index('"')]
 56         if isAddPlaceHolder:
 57         fileDic[en] = ''
 58         else:
 59         fileDic[en] = zh
 60     except KeyError:
 61         print "aa"
 62     fileContent.close()
 63     return fileDic
 64 
 65 
 66 zhFilePath = findFile(projectHome,"zh.txt")
 67 englishFilePath = outPutDir+'/Localizable.strings'
 68 
 69 def printDic(dic):
 70     for key in dic.keys():
 71     print key
 72 
 73 zhDic = fileDictionary(zhFilePath, False, 'utf-8')
 74 enDic = fileDictionary(englishFilePath,True,'utf-16BE')
 75 
 76 print zhDic
 77 print '**************'
 78 
 79 for key in enDic.keys():
 80     try:
 81     zh = zhDic[key]
 82     enDic[key] = zhDic[key]
 83     except KeyError:
 84     transilation = raw_input('plasee input the translation of **'+key+'**:')
 85     print transilation
 86     enDic[key] = transilation
 87 
 88 def writeDicToFile(dic,filePath,isWriteValue):
 89     writeFile = codecs.open(filePath, 'w','utf-8')
 90     for key in dic.keys():
 91         try:
 92         keyStr = key.encode('utf-8')
 93         valueStr = dic[key].encode('utf-8')
 94         print keyStr + valueStr
 95         if isWriteValue:
 96             writeFile.write('"'+keyStr+'"="'+valueStr+'"'+';\n')
 97         else:
 98             writeFile.write('"'+keyStr+'"="'+keyStr+'"'+';\n')
 99         except UnicodeDecodeError:
100         continue
101         except TypeError:
102         continue
103         except KeyError:
104         continue
105     writeFile.close()
106 
107 enOutputFilePath = findFile(projectHome,"en.txt")
108 writeDicToFile(enDic,enOutputFilePath,False)
109 writeDicToFile(enDic,zhFilePath,True)
原文地址:https://www.cnblogs.com/yishuiliunian/p/2762403.html