annotate babel/messages/checkers.py @ 220:97b4b289e792 trunk

Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
author cmlenz
date Mon, 16 Jul 2007 16:57:49 +0000
parents
children 38053412171b
rev   line source
220
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
2 #
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
3 # Copyright (C) 2007 Edgewall Software
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
4 # All rights reserved.
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
5 #
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
8 # are also available at http://babel.edgewall.org/wiki/License.
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
9 #
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://babel.edgewall.org/log/.
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
13
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
14 """Various routines that help with validation of translations."""
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
15
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
16 from babel.messages.catalog import TranslationError, PYTHON_FORMAT
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
17
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
18 def num_plurals(catalog, message):
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
19 """Verify the number of plurals in the translation."""
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
20 if not message.pluralizable:
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
21 if not isinstance(message.string, basestring):
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
22 raise TranslationError("Found plural forms for non-pluralizable "
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
23 "message")
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
24 return
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
25
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
26 msgstrs = message.string
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
27 if not isinstance(msgstrs, (list, tuple)):
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
28 msgstrs = (msgstrs,)
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
29 if len(msgstrs) != catalog.num_plurals:
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
30 raise TranslationError("Wrong number of plural forms (expected %d)" %
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
31 catalog.num_plurals)
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
32
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
33 def python_format(catalog, message):
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
34 if 'python-format' in message.flags:
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
35 msgids = message.id
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
36 if not isinstance(msgids, (list, tuple)):
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
37 msgids = (msgids,)
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
38 msgstrs = message.string
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
39 if not isinstance(msgstrs, (list, tuple)):
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
40 msgstrs = (msgstrs,)
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
41 for idx, msgid in enumerate(msgids):
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
42 if not msgstrs[idx]:
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
43 continue # no translation
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
44 for match in PYTHON_FORMAT.finditer(msgid):
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
45 param = match.group(0)
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
46 if param not in msgstrs[idx]:
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
47 raise TranslationError("Python parameter %s not found in "
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
48 "translation" % param)
Copyright (C) 2012-2017 Edgewall Software