1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """Module for handling Qt Phrase Book (.qph) files.
24
25 Extract from the U{Qt Linguist Manual:
26 Translators<http://doc.trolltech.com/4.3/linguist-translators.html>}:
27 .qph Qt Phrase Book Files are human-readable XML files containing standard
28 phrases and their translations. These files are created and updated by Qt
29 Linguist and may be used by any number of projects and applications.
30
31 A DTD to define the format does not seem to exist, but the following U{code
32 <http://www.google.com/codesearch?hl=en&q=show:gtsFsbhpVeE:KeGnQG0wDCQ:xOXsNYqccyE&sa=N&ct=rd&cs_p=ftp://ftp.trolltech.com/qt/source/qt-x11-opensource-4.0.0-b1.tar.gz&cs_f=qt-x11-opensource-4.0.0-b1/tools/linguist/linguist/phrase.cpp>}
33 provides the reference implementation for the Qt Linguist product.
34 """
35
36 from translate.storage import lisa
37 from lxml import etree
38
40 """A single term in the qph file."""
41
42 rootNode = "phrase"
43 languageNode = "source"
44 textNode = ""
45 namespace = ''
46
48 """Returns an xml Element setup with given parameters."""
49 assert purpose
50 langset = etree.Element(self.namespaced(purpose))
51 langset.text = text
52 return langset
53
56
59
61 """We override this to get source and target nodes."""
62 def not_none(node):
63 return not node is None
64 return filter(not_none, [self._getsourcenode(), self._gettargetnode()])
65
66 - def addnote(self, text, origin=None):
67 """Add a note specifically in a "definition" tag"""
68 assert isinstance(text, unicode)
69 current_notes = self.getnotes(origin)
70 self.removenotes()
71 note = etree.SubElement(self.xmlelement, self.namespaced("definition"))
72 note.text = "\n".join(filter(None, [current_notes, text.strip()]))
73
75
76 notenode = self.xmlelement.find(self.namespaced("definition"))
77 comment = ''
78 if not notenode is None:
79 comment = notenode.text
80 return comment
81
83 """Remove all the translator notes."""
84 note = self.xmlelement.find(self.namespaced("definition"))
85 if not note is None:
86 self.xmlelement.remove(note)
87
88
90 """Class representing a QPH file store."""
91 UnitClass = QphUnit
92 Name = _("Qt Phrase Book")
93 Mimetypes = ["application/x-qph"]
94 Extensions = ["qph"]
95 rootNode = "QPH"
96 bodyNode = "QPH"
97 XMLskeleton = '''<!DOCTYPE QPH>
98 <QPH>
99 </QPH>
100 '''
101 namespace = ''
102
103 - def initbody(self):
104 """Initialises self.body so it never needs to be retrieved from the XML again."""
105 self.namespace = self.document.getroot().nsmap.get(None, None)
106 self.body = self.document.getroot()
107
109 """Converts to a string containing the file's XML.
110
111 We have to override this to ensure mimic the Qt convention:
112 - no XML decleration
113 - plain DOCTYPE that lxml seems to ignore
114 """
115
116
117
118
119 output = etree.tostring(self.document, pretty_print=True,
120 xml_declaration=False, encoding='utf-8')
121 if not "<!DOCTYPE QPH>" in output[:30]:
122 output = "<!DOCTYPE QPH>" + output
123 return output
124