summaryrefslogtreecommitdiff
path: root/src/zope/i18n/gettextmessagecatalog.py
blob: 85b068734376a90c48b543a3aeb29aaa3b46a4fa (plain)
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
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""A simple implementation of a Message Catalog.
"""

from functools import wraps
from gettext import GNUTranslations

from zope.interface import implementer

from zope.i18n.interfaces import IGlobalMessageCatalog


class _KeyErrorRaisingFallback:
    def ugettext(self, message):
        raise KeyError(message)

    def ungettext(self, singular, plural, n):
        raise KeyError(singular)

    gettext = ugettext
    ngettext = ungettext


def plural_formatting(func):
    """This decorator interpolates the possible formatting marker.
    This interpolation marker is usally present for plurals.
    Example: `There are %d apples` or `They have %s pies.`

    Please note that the interpolation can be done, alternatively,
    using the mapping. This is only present as a conveniance.
    """
    @wraps(func)
    def pformat(catalog, singular, plural, n, *args, **kwargs):
        msg = func(catalog, singular, plural, n, *args, **kwargs)
        try:
            return msg % n
        except TypeError:
            # The message cannot be formatted : return it "raw".
            return msg
    return pformat


@implementer(IGlobalMessageCatalog)
class GettextMessageCatalog:
    """A message catalog based on GNU gettext and Python's gettext module."""

    _catalog = None

    def __init__(self, language, domain, path_to_file):
        """Initialize the message catalog"""
        self.language = (
            language.decode('utf-8') if isinstance(language, bytes)
            else language)
        self.domain = (
            domain.decode("utf-8") if isinstance(domain, bytes)
            else domain)
        self._path_to_file = path_to_file
        self.reload()
        catalog = self._catalog
        catalog.add_fallback(_KeyErrorRaisingFallback())
        self._gettext = (
            catalog.gettext if str is not bytes else catalog.ugettext)
        self._ngettext = (
            catalog.ngettext if str is not bytes else catalog.ungettext)

    def reload(self):
        'See IMessageCatalog'
        with open(self._path_to_file, 'rb') as fp:
            self._catalog = GNUTranslations(fp)

    def getMessage(self, id):
        'See IMessageCatalog'
        return self._gettext(id)

    @plural_formatting
    def getPluralMessage(self, singular, plural, n):
        'See IMessageCatalog'
        return self._ngettext(singular, plural, n)

    @plural_formatting
    def queryPluralMessage(self, singular, plural, n, dft1=None, dft2=None):
        'See IMessageCatalog'
        try:
            return self._ngettext(singular, plural, n)
        except KeyError:
            # Here, we use the catalog plural function to determine
            # if `n` triggers a plural form or not.
            if self._catalog.plural(n):
                return dft2
            return dft1

    def queryMessage(self, id, default=None):
        'See IMessageCatalog'
        try:
            return self._gettext(id)
        except KeyError:
            return default

    def getIdentifier(self):
        'See IMessageCatalog'
        return self._path_to_file