1
# DistUpgradeGettext.py - safe wrapper around gettext
3
# Copyright (c) 2008 Canonical
5
# Author: Michael Vogt <michael.vogt@ubuntu.com>
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.
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.
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
23
import gettext as mygettext
25
def _verify(message, translated):
27
helper that verifies that the message and the translated
28
message have the same number (and type) of % args
30
arguments_in_message = message.count("%") - message.count("\%")
31
arguments_in_translation = translated.count("%") - translated.count("\%")
32
return arguments_in_message == arguments_in_translation
36
version of gettext that logs errors but does not crash on incorrect
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))
47
def ngettext(msgid1, msgid2, n):
49
version of ngettext that logs errors but does not crash on incorrect
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