def getKeysAndValuesByPath(path): if path is None: Log.error('file path is None') return Log.info('parse %s' % path) keys = [] values = [] file = codecs.open(path, mode='r', encoding='utf-8') while True: lines = file.readlines(sizehint=1000) if not lines: break for line in lines: retvalue = re.split(r'"[ ]*=[ ]*"', line, maxsplit=1) if len(retvalue) > 1: keys.append(retvalue[0].lstrip()[1:]) values.append(retvalue[1].rstrip().rstrip(' ;')[:-1]) else: result = re.split(r'/\*.+\*/\n', line, maxsplit=1) if len(result) == 2: keys.append(Contant.KEY_DEV_COMMENT) values.append(line.strip().replace('/*', '').replace( '*/', '')) return keys, values
def writeToFile(keys, baseValues, values, directory): if not os.path.exists(directory): os.makedirs(directory) stringsPath = os.path.join(directory, 'Language.strings') Log.info("Creating iOS strings file:" + stringsPath) filestream = open(stringsPath, "wb") for x in range(len(keys)): key = keys[x].strip() if key is None or key == '': continue if key == Contant.KEY_DEV_COMMENT: comment = baseValues[x].strip() filestream.write('/*' + comment + '*/\n') continue if values[x] is None or values[x] == '': # Log.error("Key:" + keys[x] + "\'s value is None. Index:" + str(x)) continue value = values[x].strip() filestream.write('"' + key + '"' + ' = ' + '"' + value + '";\n') filestream.close()
def getKeysAndValuesByPath(path): if path is None: Log.error('file path is None') return Log.info('parse %s' % path) keys = [] values = [] doc = minidom.parse(path) root = doc.documentElement for node in root.childNodes: if node.nodeType == Node.TEXT_NODE: pass elif node.nodeType == Node.COMMENT_NODE: keys.append(Contant.KEY_DEV_COMMENT) values.append(node.nodeValue) elif node.nodeType == Node.ELEMENT_NODE: retValue, ok = _stringElementKeyValue(node) if ok: keys.append(retValue[0]) values.append(retValue[1]) else: Log.error('not support node: %s %d %s' % (node.nodeName, node.nodeType, node.nodeValue)) return keys, values
def startConvert(xlsPath, targetFolder): if xlsPath is not None: if targetFolder is None: Log.error("targetFolder is None!use -h for help.") return Log.info("read xls file from" + xlsPath) reader = ExcelReader(xlsPath) table = reader.getTableByIndex(0) convertExcelTableToStringsXml(table, targetFolder) Log.info("Finished,go to see it -> " + targetFolder) else: Log.error("file path is None!use -h for help.")
def writeToFile(keys, baseValues, values, directory, isBase): if not os.path.exists(directory): os.makedirs(directory) xmlPath = os.path.join(directory, 'strings.xml') Log.info("Creating android xml file:" + xmlPath) doc = minidom.Document() resourcesNode = doc.createElement('resources') if isBase: resourcesNode.setAttribute('xmlns:tools', 'http://schemas.android.com/tools') resourcesNode.setAttribute('tools:ignore', 'MissingTranslation') doc.appendChild(resourcesNode) for x in range(len(keys)): key = keys[x] if key is None or key == '': continue key = keys[x].strip() if key == Contant.KEY_DEV_COMMENT: comment = doc.createComment( baseValues[x].strip().decode('utf-8')) resourcesNode.appendChild(comment) continue if values[x] is None or values[x].strip() == '': # Log.error("Key:" + keys[x] + "\'s value is None. Index:" + str(x)) continue stringEle = doc.createElement('string') stringEle.setAttribute('name', key) resourcesNode.appendChild(stringEle) textNodeParent = stringEle value = values[x].strip() result = re.split(Contant.ELEMENT_TAG_DEVIDER, value, maxsplit=1) if len(result) == 2 and result[0] == 'u': value = result[1] uEle = doc.createElement('u') stringEle.appendChild(uEle) textNodeParent = uEle value = re.sub(r'(%\d\$)(@)', r'\1s', value) textNode = doc.createTextNode(value.decode('utf-8')) textNodeParent.appendChild(textNode) filestream = open(xmlPath, "wb") filestream.write(doc.toprettyxml(encoding='utf-8')) filestream.close()
def startConvert(sourceFolder, xlsFolder, apptype): if sourceFolder is not None: if xlsFolder is not None: workbook = pyExcelerator.Workbook() ws = workbook.add_sheet('Localizable.strings') # init keyName and standard language value (en) stKeys, stValues = _getStandardKeyValuesListFrom( sourceFolder, apptype) if len(stKeys) <= 0: Log.error('The language files is empty') return ws.write(0, 0, 'keyName') ws.write(0, 1, 'en') for row in range(len(stKeys)): ws.write(row + 1, 0, stKeys[row]) ws.write(row + 1, 1, stValues[row]) # append other language value index = 2 for parent, dirnames, filenames in os.walk(sourceFolder): for dirname in dirnames: conturyCode = _getCountryCode(dirname, apptype) if conturyCode == 'en': continue ws.write(0, index, conturyCode) otherLanguage = _getKeyValuesDictFrom( os.path.join(sourceFolder, dirname), apptype) for row in range(len(stKeys)): if stKeys[row] in otherLanguage: ws.write(row + 1, index, otherLanguage[stKeys[row]]) index += 1 filePath = os.path.join(xlsFolder, "Localizable.xls") workbook.save(filePath) Log.info("Convert successfully! you can see xls file in %s" % (filePath)) else: Log.error("xls folder path can not be empty! try -h for help.") else: Log.error( "strings or xml files filder can not be empty! try -h for help.")
def getKeyValueDictByPath(path): if path is None: Log.error('file path is None') return Log.info('parse %s' % path) keyvalue = {} file = codecs.open(path, mode='r', encoding='utf-8') while True: lines = file.readlines(sizehint=1000) if not lines: break for line in lines: retvalue = re.split(r'"[ ]*=[ ]*"', line, maxsplit=1) if len(retvalue) > 1: k = retvalue[0].lstrip()[1:] v = retvalue[1].rstrip().rstrip(' ;')[:-1] keyvalue[k] = v return keyvalue
def getKeyValueDictByPath(path): if path is None: Log.error('file path is None') return Log.info('parse %s' % path) languageDict = {} doc = minidom.parse(path) root = doc.documentElement nodeList = root.getElementsByTagName('string') for node in nodeList: if node.nodeType == Node.ELEMENT_NODE: retValue, ok = _stringElementKeyValue(node) if ok: languageDict[retValue[0]] = retValue[1] else: Log.error('not support node: %s %d %s' % (node.nodeName, node.nodeType, node.nodeValue)) return languageDict
def parseXMLToCheckKeyUniqueAt(filepath): if filepath is None or filepath == '': Log.error('filepath is empty') return Log.info('parsing %s' % filepath) doc = minidom.parse(filepath) root = doc.documentElement nodeList = root.getElementsByTagName('string') keyDict = dict() for node in nodeList: if node.nodeType == Node.ELEMENT_NODE: key = node.getAttribute('name').strip() count = keyDict.get(key, 0) keyDict[key] = count + 1 for k, v in keyDict.iteritems(): if v > 1: Log.error('%s appear %d!!!' % (k, v))
def parseStringsToCheckKeyUniqueAt(filepath): if filepath is None: Log.error('file path is None') return Log.info('parsing %s' % filepath) keyDict = {} file = codecs.open(filepath, mode='r', encoding='utf-8') while True: lines = file.readlines(sizehint=1000) if not lines: break for line in lines: retvalue = re.split(r'"[ ]*=[ ]*"', line, maxsplit=1) if len(retvalue) > 1: k = retvalue[0].lstrip()[1:] count = keyDict.get(k, 0) keyDict[k] = count + 1 for k, v in keyDict.iteritems(): if v > 1: Log.error('%s appear %d!!!' % (k, v))