~launchpad-pqm/launchpad/devel

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Copyright 2009-2010 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Implementation for `ITranslatableMessage`."""

__metaclass__ = type

__all__ = [
    'TranslatableMessage',
    ]

from zope.interface import implements

from lp.translations.interfaces.translatablemessage import (
    ITranslatableMessage,
    )


class TranslatableMessage(object):
    """See `ITranslatableMessage`."""
    implements(ITranslatableMessage)

    potmsgset = None
    pofile = None
    sequence = 0

    def __init__(self, potmsgset, pofile):
        """Create a new TranslatableMessage object.

        :param potmsgset: The `IPOTMsgSet` instance that this object refers
          to.
        :param pofile: The `IPOFile` instance that the potmsgset is used
          with.

        Both potmsgset and pofile must be related, meaning they refer to the
        same `IPOTemplate` instance.
        """
        assert pofile.potemplate.getPOTMsgSetByID(potmsgset.id) != None, (
          "POTMsgSet and POFile must refer to the same POTemplate.")

        self.potmsgset = potmsgset
        self.pofile = pofile
        self.sequence = potmsgset.getSequence(pofile.potemplate)
        self.potemplate = pofile.potemplate
        self.language = pofile.language

        self._current_translation = self.potmsgset.getCurrentTranslation(
            self.potemplate, self.language, self.potemplate.translation_side)

    @property
    def is_obsolete(self):
        """See `ITranslatableMessage`"""
        return self.sequence == 0

    @property
    def is_untranslated(self):
        """See `ITranslatableMessage`"""
        if self._current_translation is None:
            return True
        return self._current_translation.is_empty

    @property
    def is_current_diverged(self):
        """See `ITranslatableMessage`"""
        if self._current_translation is None:
            return False
        return self._current_translation.potemplate == self.potemplate

    @property
    def is_current_imported(self):
        """See `ITranslatableMessage`"""
        if self._current_translation is None:
            return False
        return self._current_translation.is_current_upstream

    @property
    def has_plural_forms(self):
        """See `ITranslatableMessage`"""
        return self.potmsgset.msgid_plural is not None

    @property
    def number_of_plural_forms(self):
        """See `ITranslatableMessage`"""
        if self.has_plural_forms:
            return self.pofile.plural_forms
        else:
            return 1

    def getCurrentTranslation(self):
        """See `ITranslatableMessage`"""
        return self._current_translation

    def getImportedTranslation(self):
        """See `ITranslatableMessage`"""
        return self.potmsgset.getOtherTranslation(
            self.language, self.potemplate.translation_side)

    def getSharedTranslation(self):
        """See `ITranslatableMessage`"""
        return self.potmsgset.getSharedTranslation(
            self.language, self.potemplate.translation_side)

    def getAllSuggestions(self):
        """See `ITranslatableMessage`"""
        return self.potmsgset.getLocalTranslationMessages(
            self.potemplate, self.language,
            include_dismissed=True, include_unreviewed=True)

    def getUnreviewedSuggestions(self):
        """See `ITranslatableMessage`"""
        return self.potmsgset.getLocalTranslationMessages(
            self.potemplate, self.language,
            include_dismissed=False, include_unreviewed=True)

    def getDismissedSuggestions(self):
        """See `ITranslatableMessage`"""
        return self.potmsgset.getLocalTranslationMessages(
            self.potemplate, self.language,
            include_dismissed=True, include_unreviewed=False)

    def getExternalTranslations(self):
        """See `ITranslatableMessage`"""
        lang = self.language
        return self.potmsgset.getExternallyUsedTranslationMessages(lang)

    def getExternalSuggestions(self):
        """See `ITranslatableMessage`"""
        lang = self.language
        return self.potmsgset.getExternallySuggestedTranslationMessages(lang)

    def dismissAllSuggestions(self, reviewer, lock_timestamp):
        """See `ITranslatableMessage`"""
        self.potmsgset.dismissAllSuggestions(
            self.pofile, reviewer, lock_timestamp)