~ubuntu-core-dev/ubuntu-release-upgrader/trunk

1045 by Michael Vogt
* DistUpgrade/DistUpgradeGettext.py:
1
# DistUpgradeGettext.py - safe wrapper around gettext
2
#  
3
#  Copyright (c) 2008 Canonical
4
#  
5
#  Author: Michael Vogt <michael.vogt@ubuntu.com>
6
# 
7
#  This program is free software; you can redistribute it and/or 
8
#  modify it under the terms of the GNU General Public License as 
9
#  published by the Free Software Foundation; either version 2 of the
10
#  License, or (at your option) any later version.
11
# 
12
#  This program is distributed in the hope that it will be useful,
13
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
#  GNU General Public License for more details.
16
# 
17
#  You should have received a copy of the GNU General Public License
18
#  along with this program; if not, write to the Free Software
19
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
#  USA
21
1046 by Michael Vogt
DistUpgrade/DistUpgradeGettext.py: add _verify() method that checks for count of "%"
22
import logging
1045 by Michael Vogt
* DistUpgrade/DistUpgradeGettext.py:
23
import gettext as mygettext
2790 by Michael Vogt
* tests/test_pep8.py:
24
_gettext_method = "gettext"
25
_ngettext_method = "ngettext"
2478 by Colin Watson
Use the appropriate Unicode gettext methods in both Python 2 and 3, and
26
1045 by Michael Vogt
* DistUpgrade/DistUpgradeGettext.py:
27
1046 by Michael Vogt
DistUpgrade/DistUpgradeGettext.py: add _verify() method that checks for count of "%"
28
def _verify(message, translated):
29
    """ 
30
    helper that verifies that the message and the translated 
31
    message have the same number (and type) of % args
32
    """
33
    arguments_in_message = message.count("%") - message.count("\%")
34
    arguments_in_translation = translated.count("%") - translated.count("\%")
35
    return arguments_in_message == arguments_in_translation
36
2790 by Michael Vogt
* tests/test_pep8.py:
37
2478 by Colin Watson
Use the appropriate Unicode gettext methods in both Python 2 and 3, and
38
_translation_singleton = None
2790 by Michael Vogt
* tests/test_pep8.py:
39
40
2478 by Colin Watson
Use the appropriate Unicode gettext methods in both Python 2 and 3, and
41
def _translation():
42
    """Return a suitable gettext.*Translations instance."""
43
    global _translation_singleton
44
    if _translation_singleton is None:
45
        domain = mygettext.textdomain()
46
        _translation_singleton = mygettext.translation(
47
            domain, mygettext.bindtextdomain(domain), fallback=True)
48
    return _translation_singleton
49
2790 by Michael Vogt
* tests/test_pep8.py:
50
2478 by Colin Watson
Use the appropriate Unicode gettext methods in both Python 2 and 3, and
51
def unicode_gettext(translation, message):
52
    return getattr(translation, _gettext_method)(message)
53
2790 by Michael Vogt
* tests/test_pep8.py:
54
2478 by Colin Watson
Use the appropriate Unicode gettext methods in both Python 2 and 3, and
55
def unicode_ngettext(translation, singular, plural, n):
56
    return getattr(translation, _ngettext_method)(singular, plural, n)
57
2790 by Michael Vogt
* tests/test_pep8.py:
58
1045 by Michael Vogt
* DistUpgrade/DistUpgradeGettext.py:
59
def gettext(message):
60
    """
61
    version of gettext that logs errors but does not crash on incorrect
62
    number of arguments
63
    """
1100 by Michael Vogt
* DistUpgrade/DistUpgradeGettext.py:
64
    if message == "":
65
        return ""
2478 by Colin Watson
Use the appropriate Unicode gettext methods in both Python 2 and 3, and
66
    translated_msg = unicode_gettext(_translation(), message)
1046 by Michael Vogt
DistUpgrade/DistUpgradeGettext.py: add _verify() method that checks for count of "%"
67
    if not _verify(message, translated_msg):
2790 by Michael Vogt
* tests/test_pep8.py:
68
        logging.error(
69
            "incorrect translation for message '%s' to '%s' "
70
            "(wrong number of arguments)" % (message, translated_msg))
1045 by Michael Vogt
* DistUpgrade/DistUpgradeGettext.py:
71
        return message
1046 by Michael Vogt
DistUpgrade/DistUpgradeGettext.py: add _verify() method that checks for count of "%"
72
    return translated_msg
1045 by Michael Vogt
* DistUpgrade/DistUpgradeGettext.py:
73
2790 by Michael Vogt
* tests/test_pep8.py:
74
1045 by Michael Vogt
* DistUpgrade/DistUpgradeGettext.py:
75
def ngettext(msgid1, msgid2, n):
76
    """
77
    version of ngettext that logs errors but does not crash on incorrect
78
    number of arguments
79
    """
2478 by Colin Watson
Use the appropriate Unicode gettext methods in both Python 2 and 3, and
80
    translated_msg = unicode_ngettext(_translation(), msgid1, msgid2, n)
1046 by Michael Vogt
DistUpgrade/DistUpgradeGettext.py: add _verify() method that checks for count of "%"
81
    if not _verify(msgid1, translated_msg):
2790 by Michael Vogt
* tests/test_pep8.py:
82
        logging.error(
83
            "incorrect translation for ngettext message "
84
            "'%s' plural: '%s' to '%s' (wrong number of arguments)" % (
85
                msgid1, msgid2, translated_msg))
1045 by Michael Vogt
* DistUpgrade/DistUpgradeGettext.py:
86
        # dumb fallback to not crash
87
        if n == 1:
88
            return msgid1
89
        return msgid2
1046 by Michael Vogt
DistUpgrade/DistUpgradeGettext.py: add _verify() method that checks for count of "%"
90
    return translated_msg