This patch adds testing support to our i18n translations.

Read the file test_i18n.py to see how this is done.

Makefile now supports a test_lang and test target "make test" creates the test language, installs it in under a temporary locale directory in the install/po subdir and then runs test_i18n.py.

At some point in the future we need to add a real test for translations but that is dependent on Jason finishing the LazyText work. That test will follow the logic in test_i18n.py.

--
John Dennis <jden...@redhat.com>

Looking to carve out IT costs?
www.redhat.com/carveoutcosts/
From 0fac8ff30f71ce92e8e3b496e6b8836fdfba2478 Mon Sep 17 00:00:00 2001
From: John Dennis <jden...@redhat.com>
Date: Tue, 9 Feb 2010 20:35:13 -0500
Subject: [PATCH] Add i18n test

---
 install/po/Makefile.in  |   12 ++++++-
 install/po/test_i18n.py |   85 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 96 insertions(+), 1 deletions(-)
 create mode 100755 install/po/test_i18n.py

diff --git a/install/po/Makefile.in b/install/po/Makefile.in
index 7a8631a..bd1f8f9 100644
--- a/install/po/Makefile.in
+++ b/install/po/Makefile.in
@@ -154,6 +154,7 @@ POTFILES = $(PYTHON_POTFILES) $(C_POTFILES)
 
 .SUFFIXES:
 .SUFFIXES: .po .mo
+.PHONY: all create-po update-po update-pot install mostlyclean clean distclean test_lang test
 
 all: $(po_files)
 	@
@@ -208,10 +209,19 @@ install: $(mo_files)
 	done
 
 mostlyclean:
-	rm -f *.mo
+	rm -rf *.mo test.po test_locale
 
 clean: mostlyclean
 
 distclean: clean
 	rm -f Makefile
 
+test_lang:
+	rm -rf test.po test_locale
+	$(MSGINIT) --no-translator -i $(DOMAIN).pot -o test.po
+	sed -i -r -e 's/^msgstr[ \t]+"(.*)"[ \t]*$$/msgstr "\xe2\x86\x92\1\xe2\x86\x90"/' test.po
+	$(MKDIR_P) test_locale/en_US/LC_MESSAGES
+	$(MSGFMT) -o test_locale/en_US/LC_MESSAGES/ipa.mo test.po
+
+test: test_lang
+	./test_i18n.py
diff --git a/install/po/test_i18n.py b/install/po/test_i18n.py
new file mode 100755
index 0000000..8560fc8
--- /dev/null
+++ b/install/po/test_i18n.py
@@ -0,0 +1,85 @@
+#!/usr/bin/python
+
+import sys
+import gettext
+import locale
+import re
+
+def get_msgid(po_file):
+    'Get the first non-empty msgid from the po file'
+    msgid_re = re.compile(r'^\s*msgid\s+"(.+)"\s*$')
+    f = open(po_file)
+    for line in f.readlines():
+        match = msgid_re.search(line)
+        if match:
+            msgid = match.group(1)
+            f.close()
+            return msgid
+    f.close()
+    raise ValueError('No msgid found in %s' % po_file)
+
+# We test our translations by taking the original untranslated string
+# (e.g. msgid) and prepend a prefix character and then append a suffix
+# character. The test consists of asserting that the first character in the
+# translated string is the prefix, the last character in the translated string
+# is the suffix and the everything between the first and last character exactly
+# matches the original msgid.
+#
+# We use unicode characters not in the ascii character set for the prefix and
+# suffix to enhance the test. To make reading the translated string easier the
+# prefix is the unicode right pointing arrow and the suffix left pointing arrow,
+# thus the translated string looks like the original string enclosed in
+# arrows. In ASCII art the string "foo" would render as:
+# -->foo<--
+
+# Unicode right pointing arrow
+prefix = u'\u2192'               # utf-8 == '\xe2\x86\x92'
+# Unicode left pointing arrow
+suffix = u'\u2190'               # utf-8 == '\xe2\x86\x90'
+
+def main():
+
+    try:
+
+        # The test installs the test message catalog under the en_US (e.g. U.S. English)
+        # language. It would be nice to use a dummy language not associated with any
+        # real language, but the setlocale function demands the locale be a valid known
+        # locale, U.S. English is a reasonable choice.
+        locale.setlocale(locale.LC_MESSAGES, 'en_US.UTF-8')
+
+        # Tell gettext that our domain is 'ipa', that locale_dir is 'test_locale'
+        # (i.e. where to look for the message catalog) and that we want the translations
+        # returned as unicode from the _() function
+        gettext.install('ipa', 'test_locale', unicode=1)
+
+        # We need a translatable string to test with, read one from the test po file
+        msgid = get_msgid('test.po')
+
+        # Get the translated version of the msgid string by invoking _()
+        translated = _(msgid)
+
+        # Verify the first character is the test prefix
+        if translated[0] != prefix:
+            raise ValueError("first char in (%s) not equal to prefix (%s)" % \
+                                 (translated.encode('utf-8'), prefix.encode('utf-8')))
+
+        # Verify the last character is the test suffix
+        if translated[-1] != suffix:
+            raise ValueError("last char in (%s) not equal to suffix (%s)" % \
+                                 (translated.encode('utf-8'), suffix.encode('utf-8')))
+
+        # Verify everything between the first and last character is the
+        # original untranslated string
+        if translated[1:-1] != msgid:
+            raise ValueError("interior of (%s) not equal to msgid (%s)" % \
+                                 (translated.encode('utf-8'), msgid))
+
+        print "success: %s = %s" % (msgid, _(msgid).encode('utf-8'))
+    except Exception, e:
+        print >> sys.stderr, "ERROR: %s" % e
+        return 1
+
+    return 0
+
+if __name__ == "__main__":
+    sys.exit(main())
-- 
1.6.6

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to