annotate babel/messages/checkers.py @ 530:85e1beadacb0

Update the copyright line.
author jruigrok
date Sat, 05 Mar 2011 15:22:28 +0000
parents eef19ada4296
children
rev   line source
220
677147547e2d 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 -*-
677147547e2d Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
2 #
530
85e1beadacb0 Update the copyright line.
jruigrok
parents: 525
diff changeset
3 # Copyright (C) 2007-2011 Edgewall Software
220
677147547e2d 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.
677147547e2d Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
5 #
677147547e2d 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
677147547e2d 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
677147547e2d 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.
677147547e2d Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
9 #
677147547e2d 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
677147547e2d 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
677147547e2d 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/.
677147547e2d Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
13
234
ada322f472ca Add more `since` tags to stuff added in trunk.
cmlenz
parents: 220
diff changeset
14 """Various routines that help with validation of translations.
ada322f472ca Add more `since` tags to stuff added in trunk.
cmlenz
parents: 220
diff changeset
15
ada322f472ca Add more `since` tags to stuff added in trunk.
cmlenz
parents: 220
diff changeset
16 :since: version 0.9
ada322f472ca Add more `since` tags to stuff added in trunk.
cmlenz
parents: 220
diff changeset
17 """
220
677147547e2d Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
18
352
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
19 from itertools import izip
220
677147547e2d Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
20 from babel.messages.catalog import TranslationError, PYTHON_FORMAT
677147547e2d Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
21
352
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
22 #: list of format chars that are compatible to each other
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
23 _string_format_compatibilities = [
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
24 set(['i', 'd', 'u']),
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
25 set(['x', 'X']),
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
26 set(['f', 'F', 'g', 'G'])
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
27 ]
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
28
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
29
220
677147547e2d Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
30 def num_plurals(catalog, message):
677147547e2d Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
31 """Verify the number of plurals in the translation."""
677147547e2d 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 not message.pluralizable:
677147547e2d Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
33 if not isinstance(message.string, basestring):
677147547e2d Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
34 raise TranslationError("Found plural forms for non-pluralizable "
677147547e2d Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
35 "message")
677147547e2d Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
36 return
677147547e2d Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
37
355
e989a5f2fdff Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents: 353
diff changeset
38 # skip further tests if no catalog is provided.
e989a5f2fdff Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents: 353
diff changeset
39 elif catalog is None:
e989a5f2fdff Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents: 353
diff changeset
40 return
e989a5f2fdff Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents: 353
diff changeset
41
220
677147547e2d Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
42 msgstrs = message.string
677147547e2d Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
43 if not isinstance(msgstrs, (list, tuple)):
677147547e2d Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
44 msgstrs = (msgstrs,)
677147547e2d 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 len(msgstrs) != catalog.num_plurals:
677147547e2d Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
46 raise TranslationError("Wrong number of plural forms (expected %d)" %
677147547e2d Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
47 catalog.num_plurals)
677147547e2d Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
48
352
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
49
220
677147547e2d Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
50 def python_format(catalog, message):
353
991f435715e1 Fixed a small bug in the python format string checker that caused the wrong exception to be thrown.
aronacher
parents: 352
diff changeset
51 """Verify the format string placeholders in the translation."""
352
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
52 if 'python-format' not in message.flags:
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
53 return
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
54 msgids = message.id
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
55 if not isinstance(msgids, (list, tuple)):
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
56 msgids = (msgids,)
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
57 msgstrs = message.string
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
58 if not isinstance(msgstrs, (list, tuple)):
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
59 msgstrs = (msgstrs,)
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
60
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
61 for msgid, msgstr in izip(msgids, msgstrs):
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
62 if msgstr:
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
63 _validate_format(msgid, msgstr)
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
64
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
65
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
66 def _validate_format(format, alternative):
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
67 """Test format string `alternative` against `format`. `format` can be the
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
68 msgid of a message and `alternative` one of the `msgstr`\s. The two
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
69 arguments are not interchangeable as `alternative` may contain less
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
70 placeholders if `format` uses named placeholders.
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
71
416
31f2826d3fd6 Removed `ValueError` raising for string formatting message checkers if the string does not contain any string formattings. The new behavior is undefined. This fixes at least one of the problems of the #150 ticket.
aronacher
parents: 414
diff changeset
72 The behavior of this function is undefined if the string does not use
31f2826d3fd6 Removed `ValueError` raising for string formatting message checkers if the string does not contain any string formattings. The new behavior is undefined. This fixes at least one of the problems of the #150 ticket.
aronacher
parents: 414
diff changeset
73 string formattings.
352
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
74
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
75 If the string formatting of `alternative` is compatible to `format` the
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
76 function returns `None`, otherwise a `TranslationError` is raised.
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
77
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
78 Examples for compatible format strings:
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
79
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
80 >>> _validate_format('Hello %s!', 'Hallo %s!')
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
81 >>> _validate_format('Hello %i!', 'Hallo %d!')
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
82
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
83 Example for an incompatible format strings:
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
84
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
85 >>> _validate_format('Hello %(name)s!', 'Hallo %s!')
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
86 Traceback (most recent call last):
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
87 ...
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
88 TranslationError: the format strings are of different kinds
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
89
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
90 This function is used by the `python_format` checker.
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
91
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
92 :param format: The original format string
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
93 :param alternative: The alternative format string that should be checked
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
94 against format
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
95 :return: None on success
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
96 :raises TranslationError: on formatting errors
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
97 """
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
98
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
99 def _parse(string):
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
100 result = []
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
101 for match in PYTHON_FORMAT.finditer(string):
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
102 name, format, typechar = match.groups()
361
45e5e14bb429 Fixed logic error in the python format checker.
aronacher
parents: 360
diff changeset
103 if typechar == '%' and name is None:
352
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
104 continue
360
1c9b26fce1f2 Convert format strings to str for nicer error messages (no u prefix) in the python_format checker.
aronacher
parents: 355
diff changeset
105 result.append((name, str(typechar)))
352
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
106 return result
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
107
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
108 def _compatible(a, b):
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
109 if a == b:
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
110 return True
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
111 for set in _string_format_compatibilities:
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
112 if a in set and b in set:
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
113 return True
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
114 return False
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
115
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
116 def _check_positional(results):
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
117 positional = None
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
118 for name, char in results:
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
119 if positional is None:
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
120 positional = name is None
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
121 else:
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
122 if (name is None) != positional:
353
991f435715e1 Fixed a small bug in the python format string checker that caused the wrong exception to be thrown.
aronacher
parents: 352
diff changeset
123 raise TranslationError('format string mixes positional '
991f435715e1 Fixed a small bug in the python format string checker that caused the wrong exception to be thrown.
aronacher
parents: 352
diff changeset
124 'and named placeholders')
352
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
125 return bool(positional)
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
126
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
127 a, b = map(_parse, (format, alternative))
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
128
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
129 # now check if both strings are positional or named
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
130 a_positional, b_positional = map(_check_positional, (a, b))
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
131 if a_positional and not b_positional and not b:
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
132 raise TranslationError('placeholders are incompatible')
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
133 elif a_positional != b_positional:
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
134 raise TranslationError('the format strings are of different kinds')
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
135
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
136 # if we are operating on positional strings both must have the
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
137 # same number of format chars and those must be compatible
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
138 if a_positional:
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
139 if len(a) != len(b):
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
140 raise TranslationError('positional format placeholders are '
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
141 'unbalanced')
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
142 for idx, ((_, first), (_, second)) in enumerate(izip(a, b)):
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
143 if not _compatible(first, second):
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
144 raise TranslationError('incompatible format for placeholder '
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
145 '%d: %r and %r are not compatible' %
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
146 (idx + 1, first, second))
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
147
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
148 # otherwise the second string must not have names the first one
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
149 # doesn't have and the types of those included must be compatible
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
150 else:
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
151 type_map = dict(a)
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
152 for name, typechar in b:
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
153 if name not in type_map:
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
154 raise TranslationError('unknown named placeholder %r' % name)
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
155 elif not _compatible(typechar, type_map[name]):
364
4fbe85a6ffa4 Fixed a typo in the message checkers (tests will follow)
aronacher
parents: 361
diff changeset
156 raise TranslationError('incompatible format for '
4fbe85a6ffa4 Fixed a typo in the message checkers (tests will follow)
aronacher
parents: 361
diff changeset
157 'placeholder %r: '
4fbe85a6ffa4 Fixed a typo in the message checkers (tests will follow)
aronacher
parents: 361
diff changeset
158 '%r and %r are not compatible' %
4fbe85a6ffa4 Fixed a typo in the message checkers (tests will follow)
aronacher
parents: 361
diff changeset
159 (name, typechar, type_map[name]))
352
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
160
20d10066a42a The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
161
355
e989a5f2fdff Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents: 353
diff changeset
162 def _find_checkers():
e989a5f2fdff Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents: 353
diff changeset
163 try:
e989a5f2fdff Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents: 353
diff changeset
164 from pkg_resources import working_set
e989a5f2fdff Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents: 353
diff changeset
165 except ImportError:
e989a5f2fdff Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents: 353
diff changeset
166 return [num_plurals, python_format]
e989a5f2fdff Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents: 353
diff changeset
167 checkers = []
e989a5f2fdff Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents: 353
diff changeset
168 for entry_point in working_set.iter_entry_points('babel.checkers'):
e989a5f2fdff Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents: 353
diff changeset
169 checkers.append(entry_point.load())
e989a5f2fdff Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents: 353
diff changeset
170 return checkers
e989a5f2fdff Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents: 353
diff changeset
171
e989a5f2fdff Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents: 353
diff changeset
172
e989a5f2fdff Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents: 353
diff changeset
173 checkers = _find_checkers()
Copyright (C) 2012-2017 Edgewall Software