[PATCH 1 of 3] check-po: Introduce check-po.py


Sun Jun 30 03:03:41 CDT 2013


# HG changeset patch
# User timeless at mozdev.org
# Date 1372564683 14400
#      Sat Jun 29 23:58:03 2013 -0400
# Node ID 4fbf69b0d21d0898096cae83aad33c84bec872a3
# Parent  77440de177f799f9f3bd6236c5b45fb8e15687e7
check-po: Introduce check-po.py

diff -r 77440de177f7 -r 4fbf69b0d21d i18n/check-po.py
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/i18n/check-po.py  Sat Jun 29 23:58:03 2013 -0400
@@ -0,0 +1,69 @@
+import polib
+import sys
+import re
+
+rstsection=re.compile(r"::\s*(?:\n|$)")
+breakafter=re.compile(r"\n$")
+replacements=re.compile("%\d*\.?\d*[dfirs]")
+paragraphs=re.compile(r"\n\n+")
+
+verbose = False
+stats = None
+tests = set()
+
+def report(fault, key):
+    global limit
+    global tests
+    if tests and not fault in tests:
+        return
+    if not fault in stats:
+        stats[fault] = 0
+    stats[fault] += 1
+    if not verbose:
+        return
+    print("%s? %s" % (fault, key))
+
+def main():
+    global stats
+    global tests
+    global verbose
+    limitingtest = False
+    for f in sys.argv[1:]:
+        stats = {}
+        if f == '-v':
+            verbose = True
+            continue
+        if f == '-t':
+            limitingtest = True
+            continue
+        if limitingtest:
+            tests.add(f)
+            limitingtest = False
+            continue
+        try:
+            po = polib.pofile(f)
+        except IOError:
+            print("Unhandled 'po' file: %s" % f)
+            continue
+        for record in po:
+            key = record.msgid
+            value = record.msgstr
+            # Matching translation or Untranslated
+            if key == value or value == '':
+                continue
+            # More sections in translation than in the original
+            if len(rstsection.findall(key)) > len(rstsection.findall(value)):
+                report("rstsection", key)
+            # Original and Translation should both end in a break or not end in a break
+            if (breakafter.search(key) != None) != (breakafter.search(value) != None):
+                report("breakafter", key)
+            # Original and Translation should use all of the same variable substitutions
+            if "".join(sorted(replacements.findall(key))) != "".join(sorted(replacements.findall(value))):
+                report("replacements", key)
+            if len(paragraphs.findall(key)) != len(paragraphs.findall(value)):
+                report("paragraphs", key)
+        if stats:
+            errors = ["%s: %s" % (k, stats[k]) for k in sorted(stats)]
+            print("%s errors: %s" % (f, ", ".join(errors)))
+
+main()


More information about the Mercurial-devel mailing list