[Kimchi-devel] [PATCH V7 1/3] add a make check-local command to verify the i18n string formatting

shaohef at linux.vnet.ibm.com shaohef at linux.vnet.ibm.com
Mon Apr 14 05:38:40 UTC 2014


From: ShaoHe Feng <shaohef at linux.vnet.ibm.com>

When I do i18n translation, I find some string formattings are wrong.
So I add a check-local command to help the developer to check their
string formatting.

Every developers please run this command before submit your patch.

After you run this command, it may report some invalid string
formattings as follow.
$ make check-local
Checking for invalid i18n string...
bad i18n string formatting:
  KCHAPI0005E: Create is not allowed for %(resource).
make: *** [check-local] Error 1

You should check %(resource) is what you want.

Ref:
https://docs.python.org/2/library/stdtypes.html#string-formatting-operations

Signed-off-by: ShaoHe Feng <shaohef at linux.vnet.ibm.com>
Signed-off-by: Zhou Zheng Sheng <zhshzhou at linux.vnet.ibm.com>
Signed-off-by: Christy Perez <christy at linux.vnet.ibm.com>
---
 Makefile.am           |  5 +++++
 contrib/check_i18n.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+)
 create mode 100755 contrib/check_i18n.py

diff --git a/Makefile.am b/Makefile.am
index 6831b5d..ca5623d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -76,7 +76,12 @@ PEP8_WHITELIST = \
 
 SKIP_PYFLAKES_ERR = "\./src/kimchi/websocket\.py"
 
+I18N_FILES = plugins/*/i18n.py \
+	src/kimchi/i18n.py \
+	$(NULL)
+
 check-local:
+	contrib/check_i18n.py $(I18N_FILES)
 	find . -path './.git' -prune -type f -o \
 		-name '*.py' -o -name '*.py.in'  | xargs $(PYFLAKES) | \
 		grep -w -v $(SKIP_PYFLAKES_ERR) | \
diff --git a/contrib/check_i18n.py b/contrib/check_i18n.py
new file mode 100755
index 0000000..bb33ddb
--- /dev/null
+++ b/contrib/check_i18n.py
@@ -0,0 +1,62 @@
+#!/usr/bin/python
+#
+# Project Kimchi
+#
+# Copyright IBM, Corp. 2014
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+
+import imp
+import os
+import re
+import sys
+
+
+# Match all conversion specifier with mapping key
+PATTERN = re.compile(r'''%\([^)]+\)  # Mapping key
+                         [#0\-+]?  # Conversion flags (optional)
+                         (\d+|\*)?  # Minimum field width (optional)
+                         (\.(\d+|\*))?  # Precision (optional)
+                         [lLh]?  # Length modifier (optional)
+                         [cdeEfFgGioursxX%]  # Conversion type''',
+                     re.VERBOSE)
+BAD_PATTERN = re.compile(r"%\([^)]*\)")
+
+
+def load_i18n_module(i18nfile):
+    path = os.path.dirname(i18nfile)
+    mname = i18nfile.replace("/", "_").rstrip(".py")
+    mobj = imp.find_module("i18n", [path])
+    return imp.load_module(mname, *mobj)
+
+
+def check_string_formatting(messages):
+    for k, v in messages.iteritems():
+        if BAD_PATTERN.findall(PATTERN.sub(" ", v)):
+            print "bad i18n string formatting:"
+            print "  %s: %s" % (k, v)
+            exit(1)
+
+
+def main():
+    print "Checking for invalid i18n string..."
+    for f in sys.argv[1:]:
+        messages = load_i18n_module(f).messages
+        check_string_formatting(messages)
+    print "Checking for invalid i18n string successfully"
+
+
+if __name__ == '__main__':
+    main()
-- 
1.8.5.3




More information about the Kimchi-devel mailing list