~ubuntu-core-dev/update-manager/main

« back to all changes in this revision

Viewing changes to DistUpgrade/DistUpgradeGettext.py

  • Committer: Michael Terry
  • Date: 2012-05-15 19:43:43 UTC
  • mto: This revision was merged to the branch mainline in revision 2428.
  • Revision ID: michael.terry@canonical.com-20120515194343-tocvo0awttmggie1
update several strings to match the software updater spec; most notably, rename from Update Manager to Software Updater

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
22
import logging
 
23
import gettext as mygettext
 
24
 
 
25
def _verify(message, translated):
 
26
    """ 
 
27
    helper that verifies that the message and the translated 
 
28
    message have the same number (and type) of % args
 
29
    """
 
30
    arguments_in_message = message.count("%") - message.count("\%")
 
31
    arguments_in_translation = translated.count("%") - translated.count("\%")
 
32
    return arguments_in_message == arguments_in_translation
 
33
 
 
34
def gettext(message):
 
35
    """
 
36
    version of gettext that logs errors but does not crash on incorrect
 
37
    number of arguments
 
38
    """
 
39
    if message == "":
 
40
        return ""
 
41
    translated_msg = mygettext.gettext(message)
 
42
    if not _verify(message, translated_msg):
 
43
        logging.error("incorrect translation for message '%s' to '%s' (wrong number of arguments)" % (message, translated_msg))
 
44
        return message
 
45
    return translated_msg
 
46
 
 
47
def ngettext(msgid1, msgid2, n):
 
48
    """
 
49
    version of ngettext that logs errors but does not crash on incorrect
 
50
    number of arguments
 
51
    """
 
52
    translated_msg = mygettext.ngettext(msgid1, msgid2, n)
 
53
    if not _verify(msgid1, translated_msg):
 
54
        logging.error("incorrect translation for ngettext message '%s' plural: '%s' to '%s' (wrong number of arguments)" % (msgid1, msgid2, translated_msg))
 
55
        # dumb fallback to not crash
 
56
        if n == 1:
 
57
            return msgid1
 
58
        return msgid2
 
59
    return translated_msg