欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python实现删除Android工程中的冗余字符串

程序员文章站 2022-07-21 23:14:48
android提供了一套很方便的进行资源(语言)国际化机制,为了更好地支持多语言,很多工程的翻译往往会放到类似crowdin这样的平台上。资源是全了,但是还是会有一些问题。...

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