Python implementation to remove redundant strings in Android project _python

Source: Internet
Author: User

Android provides a very convenient resource (language) internationalization mechanism, and many engineering translations tend to be placed on platforms like Crowdin in order to better support multiple languages. The resources are complete, but there are still some problems.

What problems

Here are some languages to use for examples. Which values are the default resources for the project.

1. The resources of a language and the resources of a language-qualified region. This is most serious if values-fr-rca exists in the same string as VALUES-FR.
2. Between the resources of a language and the default resource. VALUES-FR exists a string that is the same as values, possibly due to the existence of an untranslated string values-fr

Why go to Heavy

Cleanliness, unable to tolerate the slightest redundancy.

Solving ideas

1. If Values-fr-rca exists in the same string as VALUES-FR, remove the duplicate string from the Values-fr-rca and preserve the values-fr. This ensures that the resource can be read correctly under Values-fr-rca.

2. If Values-fr has the same string as values. If you remove the duplicate string in the VALUES-FR, keep the entries for values.

PY Script

Copy Code code as follows:

#!/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:
& nbsp;       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)):
 & nbsp;      return
    et.register_namespace (' Xliff ', "Urn:oasis:names: tc:xliff:document:1.2 ")
    tree = ET. ElementTree (File=filepath)
    root = Tree.getroot ()
    print root
 & nbsp;  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])

How to use

Copy Code code as follows:

Python removerepeatedstrings.py Your_android_project_root_dir

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.