summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShaun McCance <shaunm@gnome.org>2011-06-25 14:49:53 -0400
committerShaun McCance <shaunm@gnome.org>2011-06-25 14:49:53 -0400
commit8d97758ac43f2079d252fedf2a9666b33a751fd0 (patch)
tree6a5926ddcaf575c9bb7f5792ecbfb4a548b57c28
parente84f296dd21de75b9244896be9d2acc6aeed4dea (diff)
downloaditstool-8d97758ac43f2079d252fedf2a9666b33a751fd0.tar.gz
Added itst:context to specify a msgctxt for a node
-rwxr-xr-xitstool.in35
-rw-r--r--tests/Context.ll.po35
-rw-r--r--tests/Context.ll.xml15
-rw-r--r--tests/Context.pot35
-rw-r--r--tests/Context.xml14
-rw-r--r--tests/run_tests.py3
6 files changed, 136 insertions, 1 deletions
diff --git a/itstool.in b/itstool.in
index 229cfd1..54117a5 100755
--- a/itstool.in
+++ b/itstool.in
@@ -319,6 +319,7 @@ class Document (object):
self._its_loc_notes = {}
self._itst_preserve_space_nodes = {}
self._itst_drop_nodes = {}
+ self._itst_contexts = {}
self._its_lang = {}
self._itst_lang_attr = {}
self._itst_credits = None
@@ -343,6 +344,25 @@ class Document (object):
if rule.prop('selector') is not None:
for node in self._try_xpath_eval(xpath, rule.prop('selector')):
self._itst_drop_nodes[node] = rule.prop('drop')
+ elif xml_is_ns_name(rule, NS_ITST, 'contextRule'):
+ if rule.prop('selector') is not None:
+ for node in self._try_xpath_eval(xpath, rule.prop('selector')):
+ if rule.hasProp('context'):
+ self._itst_contexts[node] = rule.prop('context')
+ elif rule.hasProp('contextPointer'):
+ try:
+ oldnode = xpath.contextNode()
+ except:
+ oldnode = None
+ xpath.setContextNode(node)
+ ctxt = self._try_xpath_eval(xpath, rule.prop('contextPointer'))
+ if isinstance(ctxt, basestring):
+ self._itst_contexts[node] = ctxt
+ else:
+ for ctxt in ctxt:
+ self._itst_contexts[node] = ctxt.content
+ break
+ xpath.setContextNode(oldnode)
elif xml_is_ns_name(rule, NS_ITS, 'locNoteRule'):
locnote = None
for child in xml_child_iter(rule):
@@ -632,7 +652,13 @@ class Document (object):
msg = self._msgs.get_message_by_node(node)
if msg is None:
return node
- trans = translations.ugettext(msg.get_string())
+ msgstr = msg.get_string()
+ # Dear Python, please implement pgettext.
+ # http://bugs.python.org/issue2504
+ # Sincerely, Shaun
+ if msg.get_context() is not None:
+ msgstr = msg.get_context() + '\x04' + msgstr
+ trans = translations.ugettext(msgstr)
if trans is None:
return node
nss = {}
@@ -727,6 +753,13 @@ class Document (object):
if msg is not None:
msg.add_placeholder(node)
msg = Message()
+ ctxt = None
+ if node.hasNsProp('context', NS_ITST):
+ ctxt = node.nsProp('context', NS_ITST)
+ if ctxt is None:
+ ctxt = self._itst_contexts.get(node)
+ if ctxt is not None:
+ msg.set_context(ctxt)
if self.get_preserve_space(node):
msg.set_preserve_space()
msg.add_source('%s:%i(%s/%s)' % (self._doc.name, node.lineNo(), node.parent.name, node.name))
diff --git a/tests/Context.ll.po b/tests/Context.ll.po
new file mode 100644
index 0000000..6ea88ff
--- /dev/null
+++ b/tests/Context.ll.po
@@ -0,0 +1,35 @@
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"POT-Creation-Date: 2011-06-25 14:23-0400\n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: LANGUAGE <LL@li.org>\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+
+#: tests/Context.xml:8(info/title)
+msgctxt "[[sort]]"
+msgid "Title"
+msgstr "Sort"
+
+#: tests/Context.xml:9(info/title)
+msgctxt "link"
+msgid "Link title"
+msgstr "Linky"
+
+#: tests/Context.xml:10(info/title)
+msgctxt "link:topic"
+msgid "Topic link title"
+msgstr "Topics"
+
+#: tests/Context.xml:12(page/title)
+msgid "Title"
+msgstr "Main title"
+
+#: tests/Context.xml:13(page/p)
+msgctxt "paragraph"
+msgid "Context is paragraph"
+msgstr "This is a paragraph"
+
diff --git a/tests/Context.ll.xml b/tests/Context.ll.xml
new file mode 100644
index 0000000..16e122c
--- /dev/null
+++ b/tests/Context.ll.xml
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="utf-8"?>
+<page xmlns:its="http://www.w3.org/2005/11/its" xmlns:itst="http://itstool.org/extensions/" its:version="1.0">
+ <info>
+ <its:rules version="1.0">
+ <itst:contextRule selector="//title[@type]" contextPointer="@type"/>
+ <itst:contextRule selector="//title[@type = 'sort']" context="[[sort]]"/>
+ <itst:contextRule selector="//title[@type and @role]" contextPointer="concat(@type, ':', @role)"/>
+ </its:rules>
+ <title type="sort">Sort</title>
+ <title type="link">Linky</title>
+ <title type="link" role="topic">Topics</title>
+ </info>
+ <title>Main title</title>
+ <p itst:context="paragraph">This is a paragraph</p>
+</page>
diff --git a/tests/Context.pot b/tests/Context.pot
new file mode 100644
index 0000000..26e078c
--- /dev/null
+++ b/tests/Context.pot
@@ -0,0 +1,35 @@
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"POT-Creation-Date: 2011-06-25 14:23-0400\n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: LANGUAGE <LL@li.org>\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+
+#: tests/Context.xml:8(info/title)
+msgctxt "[[sort]]"
+msgid "Title"
+msgstr ""
+
+#: tests/Context.xml:9(info/title)
+msgctxt "link"
+msgid "Link title"
+msgstr ""
+
+#: tests/Context.xml:10(info/title)
+msgctxt "link:topic"
+msgid "Topic link title"
+msgstr ""
+
+#: tests/Context.xml:12(page/title)
+msgid "Title"
+msgstr ""
+
+#: tests/Context.xml:13(page/p)
+msgctxt "paragraph"
+msgid "Context is paragraph"
+msgstr ""
+
diff --git a/tests/Context.xml b/tests/Context.xml
new file mode 100644
index 0000000..390ebc8
--- /dev/null
+++ b/tests/Context.xml
@@ -0,0 +1,14 @@
+<page xmlns:its="http://www.w3.org/2005/11/its" xmlns:itst="http://itstool.org/extensions/" its:version="1.0">
+ <info>
+ <its:rules version="1.0">
+ <itst:contextRule selector="//title[@type]" contextPointer="@type"/>
+ <itst:contextRule selector="//title[@type = 'sort']" context="[[sort]]"/>
+ <itst:contextRule selector="//title[@type and @role]" contextPointer="concat(@type, ':', @role)"/>
+ </its:rules>
+ <title type="sort">Title</title>
+ <title type="link">Link title</title>
+ <title type="link" role="topic">Topic link title</title>
+ </info>
+ <title>Title</title>
+ <p itst:context="paragraph">Context is paragraph</p>
+</page>
diff --git a/tests/run_tests.py b/tests/run_tests.py
index 21c594f..e8b009e 100644
--- a/tests/run_tests.py
+++ b/tests/run_tests.py
@@ -117,6 +117,9 @@ class ItstoolTests(unittest.TestCase):
def test_attributes1(self):
self._test_translation_process('Attributes1.xml')
+ def test_context(self):
+ self._test_translation_process('Context.xml')
+
class ITSTestRunner(unittest.TextTestRunner):
def run(self, test):