annotate babel/messages/checkers.py @ 236:bf9579c4b0ee

Add more `since` tags to stuff added in trunk.
author cmlenz
date Wed, 01 Aug 2007 14:15:35 +0000
parents bd8b1301b27e
children 249aab27c4b3
rev   line source
222
bd8b1301b27e 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 -*-
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
2 #
bd8b1301b27e 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
bd8b1301b27e 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.
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
5 #
bd8b1301b27e 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
bd8b1301b27e 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
bd8b1301b27e 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.
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
9 #
bd8b1301b27e 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
bd8b1301b27e 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
bd8b1301b27e 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/.
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
13
236
bf9579c4b0ee Add more `since` tags to stuff added in trunk.
cmlenz
parents: 222
diff changeset
14 """Various routines that help with validation of translations.
bf9579c4b0ee Add more `since` tags to stuff added in trunk.
cmlenz
parents: 222
diff changeset
15
bf9579c4b0ee Add more `since` tags to stuff added in trunk.
cmlenz
parents: 222
diff changeset
16 :since: version 0.9
bf9579c4b0ee Add more `since` tags to stuff added in trunk.
cmlenz
parents: 222
diff changeset
17 """
222
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
18
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
19 from babel.messages.catalog import TranslationError, PYTHON_FORMAT
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
20
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
21 def num_plurals(catalog, message):
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
22 """Verify the number of plurals in the translation."""
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
23 if not message.pluralizable:
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
24 if not isinstance(message.string, basestring):
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
25 raise TranslationError("Found plural forms for non-pluralizable "
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
26 "message")
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
27 return
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
28
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
29 msgstrs = message.string
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
30 if not isinstance(msgstrs, (list, tuple)):
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
31 msgstrs = (msgstrs,)
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
32 if len(msgstrs) != catalog.num_plurals:
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
33 raise TranslationError("Wrong number of plural forms (expected %d)" %
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
34 catalog.num_plurals)
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
35
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
36 def python_format(catalog, message):
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
37 if 'python-format' in message.flags:
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
38 msgids = message.id
bd8b1301b27e 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(msgids, (list, tuple)):
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
40 msgids = (msgids,)
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
41 msgstrs = message.string
bd8b1301b27e 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 isinstance(msgstrs, (list, tuple)):
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
43 msgstrs = (msgstrs,)
bd8b1301b27e 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 idx, msgid in enumerate(msgids):
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
45 if not msgstrs[idx]:
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
46 continue # no translation
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
47 for match in PYTHON_FORMAT.finditer(msgid):
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
48 param = match.group(0)
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
49 if param not in msgstrs[idx]:
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
50 raise TranslationError("Python parameter %s not found in "
bd8b1301b27e Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
51 "translation" % param)
Copyright (C) 2012-2017 Edgewall Software