File: l10nhandler.py

package info (click to toggle)
chemical-structures 2.2.dfsg.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,808 kB
  • sloc: xml: 7,224; javascript: 1,194; python: 993; sh: 51; makefile: 42
file content (98 lines) | stat: -rwxr-xr-x 2,780 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""L10N file handler for Python.

Exported classes:
    L10NHandler - L10N file handler.
"""

import xml.sax.handler

class L10NHandler(xml.sax.handler.ContentHandler):
    """Class for receiving logical L10N content events.

    It supports L10N entities as defined in the following file:
    xml/l10n.dtd

    The order of event in this class mirrors the order of the information in
    the document.
    """
    def __init__(self):
        """Creates an instance of the L10NHandler class.

        The function set the object attributes with default values.
        """
        self.msgDict = {}
        self.msgid = ""
        self.inMsgid = False
        self.msg = ""
        self.msgLang =""
        self.inMsg = False

    def startElement(self, name, attributes):
        """Signals the start of an element.

        The function set a variable depending on the element and the
        attribut.

        Parameters:
            name - contains the raw CML name of the element type as a string.
            attributes - contains an instance of the Attributes class.
        """
        if name == "msgid":
            self.inMsgid = True

        if name == "msg":
            if 'xml:lang' in attributes:
                self.msgLang = attributes["xml:lang"]
            else:
                self.msgLang = "en"
            self.inMsg = True
       
    def characters(self, data):
        """Receives notification of character data.

        The parser will call this method to report each chunk of character
        data.

        Parameters:
            data - contains the chunk of character data.
        """
        if self.inMsgid:
            self.msgid += data

        if self.inMsg:
            self.msg += data

    def endElement(self,name):
        """Signals the end of an element in non-namespace mode.

        Parameters:
            name - contains the name of the element type.
        """
        if name == "msgid":
            self.inMsgid = False
            self.msgDict[self.msgid] = {}

        if name == "msg":
            self.inMsg = False
            self.msgDict[self.msgid][self.msgLang] = self.msg
            self.msgLang = ""
            self.msg = ""

        if name == "msgset":
            self.msgid = ""

    def translate(self, msgid, lang):
        """Translates a string

        Parameters:
            msgid - contains the msgid to translate.
            lang - contains the destination language.
        
        Returns:
            a string - either the translated string if the translation is
                       available or the msgid in other cases.
        """
        if msgid in self.msgDict and lang in self.msgDict[msgid]:
            return self.msgDict[msgid][lang]
        else:
            return msgid