diff options
author | Claude Paroz <claude@2xlibre.net> | 2011-06-12 21:40:36 +0200 |
---|---|---|
committer | Shaun McCance <shaunm@gnome.org> | 2011-06-23 16:04:51 -0400 |
commit | 7ecd70b27d5cc22f8d6b35fc68aa9fe9cd3a7def (patch) | |
tree | 48a51211d13ed7eba763e3e7d9b03675ab3a45f8 | |
parent | fadb43fc37dbe18d9b1204bb13fb5ccea492b649 (diff) | |
download | itstool-7ecd70b27d5cc22f8d6b35fc68aa9fe9cd3a7def.tar.gz |
Extract and translate node attributes
-rwxr-xr-x | itstool.in | 41 | ||||
-rw-r--r-- | tests/Translate1.ll.po | 4 | ||||
-rw-r--r-- | tests/Translate1.ll.xml | 2 | ||||
-rw-r--r-- | tests/Translate1.pot | 4 | ||||
-rw-r--r-- | tests/Translate2.ll.po | 4 | ||||
-rw-r--r-- | tests/Translate2.ll.xml | 2 | ||||
-rw-r--r-- | tests/Translate2.pot | 4 | ||||
-rw-r--r-- | tests/TranslateGlobal.ll.po | 23 | ||||
-rw-r--r-- | tests/TranslateGlobal.ll.xml | 13 | ||||
-rw-r--r-- | tests/TranslateGlobal.pot | 23 | ||||
-rw-r--r-- | tests/run_tests.py | 7 |
11 files changed, 116 insertions, 11 deletions
@@ -123,6 +123,11 @@ class Message (object): self._comments = [] self._preserve = False + def __repr__(self): + if self._empty: + return "Empty message" + return self.get_string() + class Placeholder (object): def __init__ (self, node): self.node = node @@ -187,13 +192,16 @@ class Message (object): self._ctxt = ctxt def add_source (self, source): - self._sources.append(unicode(source, 'utf-8')) + if not isinstance(source, unicode): + source = unicode(source, 'utf-8') + self._sources.append(source) def get_sources (self): return self._sources def add_comment (self, comment): - self._comments.append(comment) + if comment is not None: + self._comments.append(comment) def get_comments (self): return self._comments @@ -268,6 +276,12 @@ def xml_child_iter (node): yield child child = child.next +def xml_attr_iter (node): + attr = node.get_properties() + while attr is not None: + yield attr + attr = attr.next + def xml_is_ns_name (node, ns, name): if node.type != 'element': return False @@ -542,12 +556,14 @@ class Document (object): self.merge_credits(translations, language, node) msg = self._msgs.get_message_by_node(node) if msg is None: + self.translate_attrs(node, node) children = [child for child in xml_child_iter(node)] for child in children: self.merge_translations(translations, language, node=child) else: newnode = self.get_translated(node, translations) if newnode != node: + self.translate_attrs(node, newnode) node.replaceNode(newnode) if is_root: # Apply language attributes to untranslated nodes. We don't do @@ -591,6 +607,13 @@ class Document (object): fix_node_ns(child, childnsdefs) fix_node_ns(node, {}) + def translate_attrs(self, oldnode, newnode): + trans_attrs = [attr for attr in xml_attr_iter(oldnode) if self._its_translate_nodes.get(attr, 'no') == 'yes'] + for attr in trans_attrs: + newcontent = translations.ugettext(attr.get_content()) + if newcontent: + newnode.setProp(attr.name, translations.ugettext(attr.get_content())) + def get_translated (self, node, translations): msg = self._msgs.get_message_by_node(node) if msg is None: @@ -684,6 +707,16 @@ class Document (object): if is_unit: if msg is not None: msg.add_placeholder(node) + # Add msg for translatable node attributes + for attr in xml_attr_iter(node): + if self._its_translate_nodes.get(attr, 'no') == 'yes': + attr_msg = Message() + attr_msg.add_source('%s:%i(%s/%s@%s)' % ( + self._doc.name, node.lineNo(), node.parent.name, node.name, attr.name)) + attr_msg.add_text(attr.content) + if comments: + attr_msg.add_comment(self.get_its_loc_note(attr)) + self._msgs.add_message(attr_msg, attr) msg = Message() if self.get_preserve_space(node): msg.set_preserve_space() @@ -692,9 +725,7 @@ class Document (object): msg.add_start_tag(node) if comments and msg is not None: - comment = self.get_its_loc_note(node) - if comment is not None: - msg.add_comment(comment) + msg.add_comment(self.get_its_loc_note(node)) in_translatable = self._in_translatable self._in_translatable = (translate == 'yes') diff --git a/tests/Translate1.ll.po b/tests/Translate1.ll.po index 29cb646..8a43b60 100644 --- a/tests/Translate1.ll.po +++ b/tests/Translate1.ll.po @@ -13,6 +13,10 @@ msgstr "" msgid "Text with an image: <img alt=\"Image description\" src=\"img.png\"/>." msgstr "Texte avec une image : <img alt=\"Description d'image\" src=\"img.png\"/>." +#: tests/Translate1.xml:16(body/par@title) +msgid "Text" +msgstr "Texte" + #: tests/Translate1.xml:16(body/par) msgid "This is the first paragraph. It has some <_:verbatim-1/>." msgstr "C'est le premier paragraphe. Il contient du texte <_:verbatim-1/>." diff --git a/tests/Translate1.ll.xml b/tests/Translate1.ll.xml index bff12e0..72b30fe 100644 --- a/tests/Translate1.ll.xml +++ b/tests/Translate1.ll.xml @@ -12,7 +12,7 @@ </head> <body> <par>Texte avec une image : <img alt="Description d'image" src="img.png"/>.</par> - <par id="100" title="Text">C'est le premier paragraphe. Il contient du texte <verbatim>un-translatable + <par id="100" title="Texte">C'est le premier paragraphe. Il contient du texte <verbatim>un-translatable code with an image: <img src="test.png" alt="Image description"/></verbatim>.</par> <insert xmlns:z="myChineseMakupLanguage" xmlns:i="http://www.w3.org/2005/11/its"> <z:书籍> diff --git a/tests/Translate1.pot b/tests/Translate1.pot index 312e0d6..d89dc93 100644 --- a/tests/Translate1.pot +++ b/tests/Translate1.pot @@ -13,6 +13,10 @@ msgstr "" msgid "Text with an image: <img alt=\"Image description\" src=\"img.png\"/>." msgstr "" +#: tests/Translate1.xml:16(body/par@title) +msgid "Text" +msgstr "" + #: tests/Translate1.xml:16(body/par) msgid "This is the first paragraph. It has some <_:verbatim-1/>." msgstr "" diff --git a/tests/Translate2.ll.po b/tests/Translate2.ll.po index 9078ab4..0ccb8de 100644 --- a/tests/Translate2.ll.po +++ b/tests/Translate2.ll.po @@ -9,6 +9,10 @@ msgstr "" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" +#: tests/Translate2.xml:8(body/par@title) +msgid "Text" +msgstr "Texte" + #: tests/Translate2.xml:8(body/par) msgid "This is the first paragraph. It has some <_:code-1/>." msgstr "C'est le premier paragraphe. Il contient du texte <_:code-1/>." diff --git a/tests/Translate2.ll.xml b/tests/Translate2.ll.xml index 2092f86..959c733 100644 --- a/tests/Translate2.ll.xml +++ b/tests/Translate2.ll.xml @@ -4,7 +4,7 @@ <its:rules xmlns:its="http://www.w3.org/2005/11/its" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.0" xlink:type="simple" xlink:href="Translate2_LinkedRules.xml"/> </head> <body> - <par id="100" title="Text">C'est le premier paragraphe. Il contient du texte <code>un-translatable code</code>.</par> + <par id="100" title="Texte">C'est le premier paragraphe. Il contient du texte <code>un-translatable code</code>.</par> </body> </myDoc> <!-- timestamp $Id: Translate2.xml,v 1.4 2007/02/02 16:55:05 srahtz3 Exp $ --> diff --git a/tests/Translate2.pot b/tests/Translate2.pot index 85aac4f..0d21731 100644 --- a/tests/Translate2.pot +++ b/tests/Translate2.pot @@ -9,6 +9,10 @@ msgstr "" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" +#: tests/Translate2.xml:8(body/par@title) +msgid "Text" +msgstr "" + #: tests/Translate2.xml:8(body/par) msgid "This is the first paragraph. It has some <_:code-1/>." msgstr "" diff --git a/tests/TranslateGlobal.ll.po b/tests/TranslateGlobal.ll.po new file mode 100644 index 0000000..17e30d3 --- /dev/null +++ b/tests/TranslateGlobal.ll.po @@ -0,0 +1,23 @@ +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"POT-Creation-Date: 2011-06-04 17:32+0200\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/TranslateGlobal.xml:10(body/par@title) +msgid "Text" +msgstr "Texte" + +#: tests/TranslateGlobal.xml:10(body/par) +msgid "This is the first paragraph. It has some <_:code-1/>." +msgstr "Voici le premier paragraphe. Il contient du texte <_:code-1/>." + +#: tests/TranslateGlobal.xml:11(body/par) +msgid "Some additional text." +msgstr "Du texte supplémentaire." + diff --git a/tests/TranslateGlobal.ll.xml b/tests/TranslateGlobal.ll.xml new file mode 100644 index 0000000..eed9b8b --- /dev/null +++ b/tests/TranslateGlobal.ll.xml @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="utf-8"?> +<myDoc> + <head> + <its:rules xmlns:its="http://www.w3.org/2005/11/its" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.0" xlink:type="simple" xlink:href="TranslateGlobal_LinkedRules.xml"> + <its:translateRule selector="//par[@id='101']/@title" translate="no"/> + </its:rules> + </head> + <body> + <par id="100" title="Texte">Voici le premier paragraphe. Il contient du texte <code>un-translatable code</code>.</par> + <par id="101" title="Not extractable text">Du texte supplémentaire.</par> + </body> +</myDoc> +<!-- timestamp $Id: TranslateGlobal.xml,v 1.4 2007/02/02 16:55:05 srahtz3 Exp $ --> diff --git a/tests/TranslateGlobal.pot b/tests/TranslateGlobal.pot new file mode 100644 index 0000000..7ac794e --- /dev/null +++ b/tests/TranslateGlobal.pot @@ -0,0 +1,23 @@ +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"POT-Creation-Date: 2011-06-04 17:32+0200\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/TranslateGlobal.xml:10(body/par@title) +msgid "Text" +msgstr "" + +#: tests/TranslateGlobal.xml:10(body/par) +msgid "This is the first paragraph. It has some <_:code-1/>." +msgstr "" + +#: tests/TranslateGlobal.xml:11(body/par) +msgid "Some additional text." +msgstr "" + diff --git a/tests/run_tests.py b/tests/run_tests.py index fafc1cb..fe8727c 100644 --- a/tests/run_tests.py +++ b/tests/run_tests.py @@ -59,11 +59,11 @@ class ItstoolTests(unittest.TestCase): def test_locnotes(self): - # FIXME: only the third note appears currently, as attribute extraction is not yet implemented + # FIXME: only the third note appears currently, as notes on subnodes don't propagate to parent nodes self._test_pot_generation('LocNote1.xml') def test_locnotes_external(self): - # FIXME: only the third note appears currently, as attribute extraction is not yet implemented + # FIXME: only the third note appears currently, as notes on subnodes don't propagate to parent nodes self._test_pot_generation('LocNote2.xml') def test_locnotes_ontags(self): @@ -101,8 +101,7 @@ class ItstoolTests(unittest.TestCase): def test_last_rule_win(self): self._test_translation_process('Translate7.xml') - # FIXME: currently deactivated until attributes are translatable - def xx_test_attribute_selectable(self): + def test_attribute_selectable(self): self._test_translation_process('TranslateGlobal.xml') def test_withintext(self): |