The Japanese translation has a number of plurals with "one" quantities

but no "other", which crashes. "Fix" these by making the copy script
turn one into other, which may well make sense given Japanese
grammer. At any rate it prevents crashes until I can work it out with
the translator and/or weblate.
This commit is contained in:
Eric House 2016-01-23 21:59:31 -08:00
parent b9b9038c66
commit 5ef7495d51

View file

@ -9,7 +9,9 @@ import re, sys, os, getopt
from lxml import etree from lxml import etree
s_prefix = 'XLATE ME: ' s_prefix = 'XLATE ME: '
# languages in which it's ok to make a standalone quantity="one" into
# quantity="other"
g_oneToOthers = ['values-ja']
sComment = """ sComment = """
DO NOT EDIT THIS FILE!!!! DO NOT EDIT THIS FILE!!!!
@ -31,34 +33,90 @@ def sameAsEnglishPlural(engNames, strElem):
and sameOrSameWithPrefix( str, strs['other'] ) and sameOrSameWithPrefix( str, strs['other'] )
return result return result
def pluralsIsSame(engNames, plurals): # If has a one and no (or empty) other, convert the one to other
different = False # all children duplicates of English def tryConvertOne( plurals ):
strings = engNames[plurals.get('name')]['strings'] quantities = {}
for item in plurals.getchildren():
quantities[item.get("quantity")] = item
use = False
if "one" in quantities:
if "other" in quantities:
text = quantities['other'].text
if not text or 0 == len(text):
use = True
else:
use = True
if use:
print "converting", plurals.get('name')
plurals.remove(quantities['other'])
quantities['one'].set('quantity', 'other')
def pluralsIsBogus(engNames, plurals, verbose):
haveOther = False # will crash without one
bogus = False
for item in plurals.getchildren(): for item in plurals.getchildren():
quantity = item.get('quantity')
text = item.text text = item.text
if not text or 0 == len(text): if not text or 0 == len(text):
print "bogus empty item in", plurals.get('name') bogus = True
if verbose:
quantity = item.get("quantity")
print 'dropping plurals {name} because of empty/missing \"{quantity}\"' \
.format(name=plurals.get("name"), quantity=quantity )
break
if item.get("quantity") == "other":
haveOther = True
if verbose and not bogus and not haveOther:
print "dropping plurals {name} because no \"other\" quantity" \
.format(name=plurals.get("name"))
return bogus or not haveOther
def pluralsIsSame(engNames, plurals):
different = False # all children duplicates of English
engItem = engNames[plurals.get('name')]
strings = engItem['strings']
for item in plurals.getchildren():
text = item.text
if not text or 0 == len(text):
print "bogus empty plurals item in", plurals.get('name')
engItem = engItem
sys.exit(1) sys.exit(1)
quantity = item.get('quantity')
if quantity in strings: if quantity in strings:
if sameOrSameWithPrefix( strings[quantity], text ): if sameOrSameWithPrefix( strings[quantity], text ):
different = True different = True
return different return different
def checkPlurals( engNames, elem ): # path will be something like res_src/values-pt/strings.xml. We want
# the next-to-last entry.
def valuesDir( path ):
splits = path.split('/')
return splits[-2]
def checkPlurals( engNames, elem, src, verbose ):
name = elem.get('name') name = elem.get('name')
ok = True
if not name in engNames or not 'plurals' == engNames[name]['type']: if not name in engNames or not 'plurals' == engNames[name]['type']:
print 'plurals', name, 'not in engNames or not a plurals there' print 'plurals', name, 'not in engNames or not a plurals there'
ok = False ok = False
elif pluralsIsSame(engNames, elem):
if ok and valuesDir(src) in g_oneToOthers:
tryConvertOne( elem )
if ok and pluralsIsBogus(engNames, elem, verbose):
ok = False ok = False
else: if ok and pluralsIsSame(engNames, elem):
ok = False
if ok:
for item in elem.getchildren(): for item in elem.getchildren():
if 0 == len(item.text): if 0 == len(item.text):
ok = False ok = False
print 'bad empty item', name print 'bad empty item', name
sys.exit(1) sys.exit(1)
ok = True
return ok return ok
def loadPlural(plural): def loadPlural(plural):
@ -78,7 +136,10 @@ def writeDoc(doc, src, dest):
def checkOrConvertString(engNames, elem, verbose): def checkOrConvertString(engNames, elem, verbose):
name = elem.get('name') name = elem.get('name')
if not name in engNames or elem.text.startswith(s_prefix): if not elem.text:
print "elem", name, "is empty"
sys.exit(1)
elif not name in engNames or elem.text.startswith(s_prefix):
ok = False ok = False
elif not 'string' == engNames[name]['type']: elif not 'string' == engNames[name]['type']:
if 'plurals' == engNames[name]['type']: if 'plurals' == engNames[name]['type']:
@ -111,7 +172,7 @@ def checkAndCopy( parser, engNames, src, dest, verbose ):
elem.getparent().remove(elem) elem.getparent().remove(elem)
for elem in doc.findall('plurals'): for elem in doc.findall('plurals'):
if not checkPlurals(engNames, elem): if not checkPlurals(engNames, elem, src, verbose):
elem.getparent().remove(elem) elem.getparent().remove(elem)
writeDoc(doc, src, dest) writeDoc(doc, src, dest)
@ -119,7 +180,7 @@ def checkAndCopy( parser, engNames, src, dest, verbose ):
def main(): def main():
# add these via params later # add these via params later
excepts = ['values-ca_PS', 'values-ba_CK'] excepts = ['values-ca_PS', 'values-ba_CK']
verboses = ['values-fr'] verboses = ['values-ja']
# summarize the english file # summarize the english file
wd = os.path.dirname(sys.argv[0]) wd = os.path.dirname(sys.argv[0])
@ -128,10 +189,10 @@ def main():
engFormats = {} engFormats = {}
parser = etree.XMLParser(remove_blank_text=True, encoding="utf-8") parser = etree.XMLParser(remove_blank_text=True, encoding="utf-8")
doc = etree.parse(path, parser) engDoc = etree.parse(path, parser)
pat = re.compile( '(%\d\$[sd])', re.DOTALL | re.MULTILINE ) pat = re.compile( '(%\d\$[sd])', re.DOTALL | re.MULTILINE )
for typ in ['string', 'plurals']: for typ in ['string', 'plurals']:
for elem in doc.findall(typ): for elem in engDoc.findall(typ):
name = elem.get('name') name = elem.get('name')
item = { 'type' : typ } item = { 'type' : typ }
if typ == 'string': if typ == 'string':