一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python實現刪除Android工程中的冗余字符串

Python實現刪除Android工程中的冗余字符串

2020-05-19 09:23腳本之家 Python

這篇文章主要介紹了Python實現刪除Android工程中的冗余字符串,本文實現的是刪除Android資源(語言)國際化機制中的一些冗余字符串,需要的朋友可以參考下

Android提供了一套很方便的進行資源(語言)國際化機制,為了更好地支持多語言,很多工程的翻譯往往會放到類似crowdin這樣的平臺上。資源是全了,但是還是會有一些問題。

哪些問題

以下使用一些語言進行舉例。其中values為工程默認的資源。

1.某語言的資源和某語言限定區域的資源之間。如values-fr-rCA存在于values-fr相同的字符串,這種表現最為嚴重。
2.某語言的資源和默認的資源之間。values-fr存在與values相同的字符串,可能原因是由于values-fr存在未翻譯字符串導致

為什么要去重

潔癖,容不下半點冗余。

解決思路

1.如果values-fr-rCA存在于values-fr相同的字符串,去除values-fr-rCA中的重復字符串,保留values-fr。這樣可以保證在values-fr-rCA下也可以正確讀取到資源。

2.如果values-fr存在與values相同的字符串。如去除values-fr中得重復字符串,保留values的條目。

Py腳本

 

復制代碼 代碼如下:


#!/usr/bin/env python
# coding=utf-8
from os import listdir,path, system
from sys import argv
try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET

 


def genRegionLangPair(filePath):
    basicLanguage = None
    if ('values' in filePath) :
        hasRegionLimit = ('r' == filePath[-3:-2])
        if (hasRegionLimit):
            basicLanguage = filePath[0:-4]
            if (not path.exists(basicLanguage)) :
                return None
            belongsToEnglish =  ("values-en" in basicLanguage)
            if (belongsToEnglish):
                #Compare with the res/values/strings.xml
                return (path.dirname(basicLanguage) + '/values/strings.xml', filePath + "/strings.xml")
            else:
                return (basicLanguage + '/strings.xml', filePath + "/strings.xml")
    return None

def genLangPair(filePath):
    def shouldGenLanPair(filePath):
        if (not 'values' in filePath ):
            return False
        if('dpi' in filePath):
            return False
        if ('dimes' in filePath):
            return False
        if ('large' in filePath):
            return False
        return True

    if(shouldGenLanPair(filePath)):
        basicLanguage = path.dirname(filePath) + '/values/strings.xml'
        targetLanguage = filePath + '/strings.xml'
        if (not path.exists(targetLanguage)):
           return None

        if (not path.samefile(basicLanguage,targetLanguage)) :
            return (basicLanguage, targetLanguage)
    return None

def genCompareList(filePath):
    compareLists = []
    for file in listdir(filePath):
        regionPair = genRegionLangPair(filePath + '/' + file)
        if (None != regionPair):
            compareLists.append(regionPair)

        languagePair = genLangPair(filePath + '/' + file)
        if (None != languagePair) :
            compareLists.append(languagePair)

    return compareLists

def getXmlEntries(filePath):
    root = ET.ElementTree(file=filePath).getroot()
    entries = {}
    for child in root:
        attrib = child.attrib
        if (None != attrib) :
            entries[attrib.get('name')] = child.text
    print 'xmlEntriesCount',len(entries)
    return entries

def rewriteRegionFile(sourceEntries, filePath):
    if (not path.exists(filePath)):
        return
    ET.register_namespace('xliff',"urn:oasis:names:tc:xliff:document:1.2")
    tree = ET.ElementTree(file=filePath)
    root = tree.getroot()
    print root
    totalCount = 0
    removeCount = 0
    unRemoveCount = 0
    print len(root)
    toRemoveList = []
    for child in root:
        totalCount = totalCount + 1
        attrib = child.attrib
        if (None == attrib):
            continue

        childName = attrib.get('name')

        if (sourceEntries.get(childName) == child.text):
            removeCount = removeCount + 1
            toRemoveList.append(child)
        else:
            unRemoveCount = unRemoveCount + 1
            print childName, sourceEntries.get(childName), child.text
    print filePath,totalCount, removeCount,unRemoveCount

    for aItem in toRemoveList:
        root.remove(aItem)

    if (len(root) != 0 ):
        tree.write(filePath, encoding="UTF-8")
    else:
        command = 'rm -rf %s'%(path.dirname(filePath))
        print command
        system(command)

 

def main(projectDir):
    lists = genCompareList(projectDir + "/res/")

    for item in lists:
        print item
        src = item[0]
        dest = item[1]
        rewriteRegionFile(getXmlEntries(src),dest)

if __name__ == "__main__":
    if (len(argv) == 2) :
        main(argv[1])

 

如何使用

 

復制代碼 代碼如下:

python removeRepeatedStrings.py your_android_project_root_dir

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 9久re热视频这里只有精品 | 欧美日韩在线观看区一二 | 亚洲国产精品成人综合久久久 | 黑人好大好硬好深好爽想要h | 免费一级毛片在级播放 | 99爱在线观看精品视频 | 久久亚洲精品AV成人无 | 日韩a级片视频 | 精品国产自在天天线2019 | 国产成人v爽在线免播放观看 | 窝窝影院午夜色在线视频 | www.尤物在线 | 91天堂国产在线 在线播放 | 精品国产品国语在线不卡丶 | 被强迫调教的高辣小说 | 国产日韩在线 | 男人天堂官方网站 | 久久久无码精品亚洲A片猫咪 | 毛片的网站 | 天作谜案免费完整版在线观看 | 国内精品伊人久久大香线焦 | 久久精品动漫99精品动漫 | 成人在线免费播放 | 99re在线精品视频免费 | 91久久国产青草亚洲 | 色老板成人永久免费视频 | 香艳69xxxxx有声小说 | 高清在线看 | 欧美日韩精品一区二区三区视频在线 | 特级夫妻大片免费在线播放 | 超91精品手机国产在线 | gay台湾无套男同志可播放 | 亚洲精品视频观看 | 国产一级真人毛爱做毛片 | 青青草综合网 | 国产香蕉国产精品偷在线观看 | 天天综合天天色 | ai换脸杨颖被啪在线观看 | 天天视频官网天天视频在线 | 被肉日常np高h | 亚洲六月丁香六月婷婷色伊人 |