2015-03-27 15:26:51 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# Go through all the res_src strings.xml files, and copy them over
|
|
|
|
# into the world where they'll get used in a build. This is meant to
|
|
|
|
# allow them to be built-in as an alternative to the
|
|
|
|
# locutils/downloadable system.
|
|
|
|
|
|
|
|
import re, sys, os, getopt
|
|
|
|
from lxml import etree
|
|
|
|
|
2015-03-28 05:37:29 +01:00
|
|
|
s_prefix = 'XLATE ME: '
|
|
|
|
|
|
|
|
|
|
|
|
sComment = """
|
|
|
|
DO NOT EDIT THIS FILE!!!!
|
|
|
|
It was generated (from %s).
|
|
|
|
Any changes you make to it will be lost.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def sameOrSameWithPrefix( str1, str2 ):
|
|
|
|
result = str1 == str2
|
|
|
|
if not result:
|
|
|
|
if str1.startswith(s_prefix):
|
|
|
|
result = str1[len(s_prefix):] == str2
|
|
|
|
return result
|
|
|
|
|
|
|
|
def sameAsEnglishPlural(engNames, strElem):
|
|
|
|
strs = engNames[strElem.get('name')]['strings']
|
|
|
|
str = strElem.text
|
|
|
|
result = 1 == len(strs) and 'other' in strs \
|
|
|
|
and sameOrSameWithPrefix( str, strs['other'] )
|
|
|
|
return result
|
|
|
|
|
2015-08-27 15:41:47 +02:00
|
|
|
def pluralsIsSame(engNames, plurals):
|
|
|
|
different = False # all children duplicates of English
|
|
|
|
strings = engNames[plurals.get('name')]['strings']
|
|
|
|
for item in plurals.getchildren():
|
|
|
|
quantity = item.get('quantity')
|
|
|
|
text = item.text
|
|
|
|
if not text or 0 == len(text):
|
|
|
|
print "bogus empty item in", plurals.get('name')
|
|
|
|
sys.exit(1)
|
|
|
|
if quantity in strings:
|
|
|
|
if sameOrSameWithPrefix( strings[quantity], text ):
|
|
|
|
different = True
|
|
|
|
return different
|
|
|
|
|
|
|
|
def checkPlurals( engNames, elem ):
|
|
|
|
name = elem.get('name')
|
|
|
|
if not name in engNames or not 'plurals' == engNames[name]['type']:
|
|
|
|
print 'plurals', name, 'not in engNames or not a plurals there'
|
|
|
|
ok = False
|
|
|
|
elif pluralsIsSame(engNames, elem):
|
|
|
|
ok = False
|
|
|
|
else:
|
|
|
|
for item in elem.getchildren():
|
|
|
|
if 0 == len(item.text):
|
|
|
|
ok = False
|
|
|
|
print 'bad empty item', name
|
|
|
|
sys.exit(1)
|
|
|
|
ok = True
|
|
|
|
return ok
|
|
|
|
|
2015-03-28 05:37:29 +01:00
|
|
|
def loadPlural(plural):
|
|
|
|
items = {}
|
|
|
|
for child in plural.getchildren():
|
|
|
|
items[child.get('quantity')] = child.text
|
|
|
|
return items
|
|
|
|
|
2015-08-27 15:41:47 +02:00
|
|
|
def writeDoc(doc, src, dest):
|
|
|
|
comment = etree.Comment(sComment % (src))
|
|
|
|
doc.getroot().insert( 0, comment )
|
|
|
|
dir = os.path.dirname( dest )
|
|
|
|
try: os.makedirs( dir )
|
|
|
|
except: pass
|
|
|
|
out = open( dest, "w" )
|
|
|
|
out.write( etree.tostring( doc, pretty_print=True, encoding="utf-8", xml_declaration=True ) )
|
|
|
|
|
|
|
|
def checkOrConvertString(engNames, elem, verbose):
|
|
|
|
name = elem.get('name')
|
|
|
|
if not name in engNames or elem.text.startswith(s_prefix):
|
|
|
|
ok = False
|
|
|
|
elif not 'string' == engNames[name]['type']:
|
|
|
|
if 'plurals' == engNames[name]['type']:
|
|
|
|
if sameAsEnglishPlural( engNames, elem ):
|
|
|
|
ok = False
|
|
|
|
else:
|
|
|
|
elem.tag = 'plurals'
|
|
|
|
item = etree.Element("item")
|
|
|
|
item.text = elem.text
|
|
|
|
elem.text = None
|
|
|
|
item.set('quantity', 'other')
|
|
|
|
elem.append( item )
|
|
|
|
if verbose: print 'translated string', name, 'to plural'
|
|
|
|
ok = True
|
2015-03-27 15:26:51 +01:00
|
|
|
else:
|
2015-08-27 15:41:47 +02:00
|
|
|
ok = False
|
|
|
|
elif sameOrSameWithPrefix(engNames[name]['string'], elem.text ):
|
|
|
|
if verbose: print "Same as english: name: %s; text: %s" % (name, elem.text)
|
|
|
|
ok = False
|
|
|
|
else:
|
|
|
|
ok = True
|
|
|
|
return ok
|
2015-03-28 05:53:37 +01:00
|
|
|
|
2015-08-27 15:41:47 +02:00
|
|
|
def checkAndCopy( parser, engNames, src, dest, verbose ):
|
|
|
|
doc = etree.parse(src, parser)
|
|
|
|
|
|
|
|
# strings
|
|
|
|
for elem in doc.findall('string'):
|
|
|
|
if not checkOrConvertString(engNames, elem, verbose):
|
|
|
|
elem.getparent().remove(elem)
|
|
|
|
|
|
|
|
for elem in doc.findall('plurals'):
|
|
|
|
if not checkPlurals(engNames, elem):
|
|
|
|
elem.getparent().remove(elem)
|
|
|
|
|
|
|
|
writeDoc(doc, src, dest)
|
2015-03-27 15:26:51 +01:00
|
|
|
|
|
|
|
def main():
|
|
|
|
# add these via params later
|
|
|
|
excepts = ['values-ca_PS', 'values-ba_CK']
|
2015-03-28 05:37:29 +01:00
|
|
|
verboses = ['values-fr']
|
2015-03-27 15:26:51 +01:00
|
|
|
|
|
|
|
# summarize the english file
|
|
|
|
wd = os.path.dirname(sys.argv[0])
|
|
|
|
path = wd + '/../XWords4/res/values/strings.xml'
|
|
|
|
engNames = {}
|
|
|
|
|
|
|
|
engFormats = {}
|
|
|
|
parser = etree.XMLParser(remove_blank_text=True, encoding="utf-8")
|
|
|
|
doc = etree.parse(path, parser)
|
|
|
|
pat = re.compile( '(%\d\$[sd])', re.DOTALL | re.MULTILINE )
|
|
|
|
for typ in ['string', 'plurals']:
|
|
|
|
for elem in doc.findall(typ):
|
|
|
|
name = elem.get('name')
|
2015-03-28 05:37:29 +01:00
|
|
|
item = { 'type' : typ }
|
|
|
|
if typ == 'string':
|
|
|
|
item['string'] = elem.text
|
|
|
|
else:
|
|
|
|
item['strings'] = loadPlural(elem)
|
|
|
|
engNames[name] = item
|
2015-03-27 15:26:51 +01:00
|
|
|
# print engNames
|
|
|
|
|
|
|
|
# iterate over src files
|
|
|
|
for subdir, dirs, files in os.walk('res_src'):
|
|
|
|
for file in [file for file in files if file == "strings.xml"]:
|
|
|
|
path = "%s/%s" % (subdir, file)
|
|
|
|
for excpt in excepts:
|
|
|
|
if excpt in path :
|
|
|
|
path = None
|
|
|
|
break
|
|
|
|
if path:
|
2015-08-27 15:41:47 +02:00
|
|
|
verbose = 0 == len(verboses) or 0 < len([verb for verb in verboses if verb in path])
|
2015-03-28 05:37:29 +01:00
|
|
|
print "*** looking at %s ***" % (path)
|
2015-03-27 15:26:51 +01:00
|
|
|
dest = path.replace( 'res_src', 'res', 1 )
|
2015-08-27 15:41:47 +02:00
|
|
|
checkAndCopy( parser, engNames, path, dest, verbose )
|
2015-03-27 15:26:51 +01:00
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|