Python deletes redundant strings in the Android project.
Android provides a convenient resource (language) Internationalization mechanism. To better support multiple languages, many engineering translations are usually placed on platforms like crowdin. Resources are full, but there are still some problems.
What problems
The following uses some languages for example. Values is the default resource of the project.
1. Between resources in a language and resources in a language-specific region. For example, values-fr-rCA exists in the same string as values-fr, which is the most serious.
2. Between resources in a language and default resources. Values-fr has the same string as values, possibly because values-fr has a non-translated string.
Why do we need to duplicate
Jie Li, can not tolerate any redundancy.
Solution
1. If values-fr-rCA exists in the same values-fr string, remove the repeated strings in values-fr-rCA and retain values-fr. This ensures that resources can be correctly read under values-fr-rCA.
2. If values-fr has the same string as values. For example, remove the repeated strings in values-fr and keep the values entries.
Py script
Copy codeThe Code is 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
Failed t 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])
How to Use
Copy codeThe Code is as follows:
Python removeRepeatedStrings. py your_android_project_root_dir