~launchpad-pqm/launchpad/devel

11662.2.2 by Nicolas Delvaux
Fix all 'make lint' warnings.
1
Translation message helper functions
2
====================================
3691.189.4 by kiko
Add the tests for the POMsgSet functions
3
5106.2.75 by Carlos Perello Marin
Even more tests fixed
4
For rendering translations in the TranslationMessageView a number of
5
helper functions exist. The following sections cover them in detail.
3691.189.4 by kiko
Add the tests for the POMsgSet functions
6
11662.2.2 by Nicolas Delvaux
Fix all 'make lint' warnings.
7
contract_rosetta_escapes
8
------------------------
3691.189.4 by kiko
Add the tests for the POMsgSet functions
9
9315.1.3 by Henning Eggers
Fixed test for browser helper functions.
10
    >>> from lp.translations.browser.browser_helpers import (
3691.344.1 by Danilo Šegan
Add [nbsp] tag for translations.
11
    ...     contract_rosetta_escapes)
3691.189.4 by kiko
Add the tests for the POMsgSet functions
12
13
Normal strings get passed through unmodified.
14
3691.344.1 by Danilo Šegan
Add [nbsp] tag for translations.
15
    >>> contract_rosetta_escapes('foo')
3691.189.4 by kiko
Add the tests for the POMsgSet functions
16
    'foo'
3691.344.1 by Danilo Šegan
Add [nbsp] tag for translations.
17
    >>> contract_rosetta_escapes('foo\\nbar')
3691.189.4 by kiko
Add the tests for the POMsgSet functions
18
    'foo\\nbar'
19
3691.344.1 by Danilo Šegan
Add [nbsp] tag for translations.
20
The string '[tab]' gets converted to a tab character.
3691.189.4 by kiko
Add the tests for the POMsgSet functions
21
3691.344.1 by Danilo Šegan
Add [nbsp] tag for translations.
22
    >>> contract_rosetta_escapes('foo[tab]bar')
3691.189.4 by kiko
Add the tests for the POMsgSet functions
23
    'foo\tbar'
24
25
The string '\[tab]' gets converted to a literal '[tab]'.
26
3691.344.1 by Danilo Šegan
Add [nbsp] tag for translations.
27
    >>> contract_rosetta_escapes('foo\\[tab]bar')
3691.189.4 by kiko
Add the tests for the POMsgSet functions
28
    'foo[tab]bar'
29
30
The string '\\[tab]' gets converted to a literal '\[tab]'.
31
3691.344.1 by Danilo Šegan
Add [nbsp] tag for translations.
32
    >>> contract_rosetta_escapes('foo\\\\[tab]bar')
3691.189.4 by kiko
Add the tests for the POMsgSet functions
33
    'foo\\[tab]bar'
34
35
And so on...
36
3691.344.1 by Danilo Šegan
Add [nbsp] tag for translations.
37
    >>> contract_rosetta_escapes('foo\\\\\\[tab]bar')
3691.189.4 by kiko
Add the tests for the POMsgSet functions
38
    'foo\\\\[tab]bar'
39
3691.344.2 by Danilo Šegan
Implement post-review suggestions by flacoste.
40
Similarly, string '[nbsp]' gets converted to no-break space character.
3691.344.1 by Danilo Šegan
Add [nbsp] tag for translations.
41
42
    >>> contract_rosetta_escapes('foo[nbsp]bar')
43
    u'foo\xa0bar'
44
45
The string '\[nbsp]' gets converted to a literal '[nbsp]'.
46
47
    >>> contract_rosetta_escapes('foo\\[nbsp]bar')
48
    'foo[nbsp]bar'
49
11662.2.3 by Nicolas Delvaux
Change [nbthin] to [nnbsp]
50
Similarly, string '[nnbsp]' gets converted to narrow no-break space
11662.2.2 by Nicolas Delvaux
Fix all 'make lint' warnings.
51
character.
11662.2.1 by Nicolas Delvaux
Implement the '[nbthin]' tag which correspond to the narrow no-break space char (\u202F)
52
11662.2.3 by Nicolas Delvaux
Change [nbthin] to [nnbsp]
53
    >>> contract_rosetta_escapes('foo[nnbsp]bar')
11662.2.1 by Nicolas Delvaux
Implement the '[nbthin]' tag which correspond to the narrow no-break space char (\u202F)
54
    u'foo\u202fbar'
55
11662.2.3 by Nicolas Delvaux
Change [nbthin] to [nnbsp]
56
The string '\[nnbsp]' gets converted to a literal '[nnbsp]'.
11662.2.1 by Nicolas Delvaux
Implement the '[nbthin]' tag which correspond to the narrow no-break space char (\u202F)
57
11662.2.3 by Nicolas Delvaux
Change [nbthin] to [nnbsp]
58
    >>> contract_rosetta_escapes('foo\\[nnbsp]bar')
59
    'foo[nnbsp]bar'
11662.2.1 by Nicolas Delvaux
Implement the '[nbthin]' tag which correspond to the narrow no-break space char (\u202F)
60
3691.344.1 by Danilo Šegan
Add [nbsp] tag for translations.
61
11662.2.2 by Nicolas Delvaux
Fix all 'make lint' warnings.
62
expand_rosetta_escapes
63
----------------------
3691.344.1 by Danilo Šegan
Add [nbsp] tag for translations.
64
9315.1.3 by Henning Eggers
Fixed test for browser helper functions.
65
    >>> from lp.translations.browser.browser_helpers import (
5106.4.52 by Jeroen Vermeulen
Functions moved from pomsgset to translationmessage. Passes.
66
    ...     expand_rosetta_escapes)
3691.189.4 by kiko
Add the tests for the POMsgSet functions
67
68
Normal strings get passed through unmodified.
69
3691.344.1 by Danilo Šegan
Add [nbsp] tag for translations.
70
    >>> expand_rosetta_escapes(u'foo')
3691.189.4 by kiko
Add the tests for the POMsgSet functions
71
    u'foo'
3691.344.1 by Danilo Šegan
Add [nbsp] tag for translations.
72
    >>> expand_rosetta_escapes(u'foo\\nbar')
3691.189.4 by kiko
Add the tests for the POMsgSet functions
73
    u'foo\\nbar'
74
3691.189.21 by kiko
Fix more test bustage
75
Tabs get converted to a special constant TranslationConstants.TAB_CHAR
76
which renders as below:
3691.189.4 by kiko
Add the tests for the POMsgSet functions
77
3691.344.1 by Danilo Šegan
Add [nbsp] tag for translations.
78
    >>> expand_rosetta_escapes(u'foo\tbar')
3691.189.21 by kiko
Fix more test bustage
79
    u'foo<code>[tab]</code>bar'
3691.189.4 by kiko
Add the tests for the POMsgSet functions
80
3691.189.21 by kiko
Fix more test bustage
81
Literal occurrences of u'[tab]' get escaped to a special constant
82
TranslationConstants.TAB_CHAR_ESCAPED which renders them as below:
3691.189.4 by kiko
Add the tests for the POMsgSet functions
83
3691.344.1 by Danilo Šegan
Add [nbsp] tag for translations.
84
    >>> expand_rosetta_escapes(u'foo[tab]bar')
3691.189.21 by kiko
Fix more test bustage
85
    u'foo<code>\\[tab]</code>bar'
3691.189.4 by kiko
Add the tests for the POMsgSet functions
86
87
Escaped ocurrences themselves get escaped.
88
3691.344.1 by Danilo Šegan
Add [nbsp] tag for translations.
89
    >>> expand_rosetta_escapes(u'foo\\[tab]bar')
3691.189.21 by kiko
Fix more test bustage
90
    u'foo\\<code>\\[tab]</code>bar'
3691.189.4 by kiko
Add the tests for the POMsgSet functions
91
92
And so on...
93
3691.344.1 by Danilo Šegan
Add [nbsp] tag for translations.
94
    >>> expand_rosetta_escapes(u'foo\\\\[tab]bar')
3691.189.21 by kiko
Fix more test bustage
95
    u'foo\\\\<code>\\[tab]</code>bar'
3691.189.4 by kiko
Add the tests for the POMsgSet functions
96
3691.344.1 by Danilo Šegan
Add [nbsp] tag for translations.
97
Similarly, no-break spaces get converted to a special constant
98
TranslationConstants.NO_BREAK_SPACE_CHAR which renders as below:
99
100
    >>> expand_rosetta_escapes(u'foo\u00a0bar')
101
    u'foo<code>[nbsp]</code>bar'
102
103
Literal occurrences of u'[nbsp]' get escaped to a special constant
104
TranslationConstants.NO_BREAK_SPACE_CHAR_ESCAPED which renders them
105
as below:
106
107
    >>> expand_rosetta_escapes(u'foo[nbsp]bar')
108
    u'foo<code>\\[nbsp]</code>bar'
109
11662.2.1 by Nicolas Delvaux
Implement the '[nbthin]' tag which correspond to the narrow no-break space char (\u202F)
110
Similarly, narrow no-break spaces get converted to a special constant
111
TranslationConstants.NARROW_NO_BREAK_SPACE_CHAR which renders as below:
112
113
    >>> expand_rosetta_escapes(u'foo\u202fbar')
11662.2.3 by Nicolas Delvaux
Change [nbthin] to [nnbsp]
114
    u'foo<code>[nnbsp]</code>bar'
11662.2.1 by Nicolas Delvaux
Implement the '[nbthin]' tag which correspond to the narrow no-break space char (\u202F)
115
11662.2.3 by Nicolas Delvaux
Change [nbthin] to [nnbsp]
116
Literal occurrences of u'[nnbsp]' get escaped to a special constant
11662.2.1 by Nicolas Delvaux
Implement the '[nbthin]' tag which correspond to the narrow no-break space char (\u202F)
117
TranslationConstants.NARROW_NO_BREAK_SPACE_CHAR_ESCAPED which renders them
118
as below:
119
11662.2.3 by Nicolas Delvaux
Change [nbthin] to [nnbsp]
120
    >>> expand_rosetta_escapes(u'foo[nnbsp]bar')
121
    u'foo<code>\\[nnbsp]</code>bar'
11662.2.1 by Nicolas Delvaux
Implement the '[nbthin]' tag which correspond to the narrow no-break space char (\u202F)
122
3691.189.4 by kiko
Add the tests for the POMsgSet functions
123
11662.2.2 by Nicolas Delvaux
Fix all 'make lint' warnings.
124
parse_cformat_string
125
--------------------
3691.189.4 by kiko
Add the tests for the POMsgSet functions
126
9315.1.3 by Henning Eggers
Fixed test for browser helper functions.
127
    >>> from lp.translations.browser.browser_helpers import (
5106.4.52 by Jeroen Vermeulen
Functions moved from pomsgset to translationmessage. Passes.
128
    ...     parse_cformat_string)
3691.189.4 by kiko
Add the tests for the POMsgSet functions
129
    >>> parse_cformat_string('')
130
    []
131
    >>> parse_cformat_string('foo')
132
    [('string', 'foo')]
133
    >>> parse_cformat_string('blah %d blah')
134
    [('string', 'blah '), ('interpolation', '%d'), ('string', ' blah')]
135
    >>> parse_cformat_string('%sfoo%%bar%s')
136
    [('interpolation', '%s'), ('string', 'foo%%bar'), ('interpolation', '%s')]
137
    >>> parse_cformat_string('%')
138
    Traceback (most recent call last):
139
    ...
140
    UnrecognisedCFormatString: %
141
142
11662.2.2 by Nicolas Delvaux
Fix all 'make lint' warnings.
143
text_to_html
144
------------
3691.189.4 by kiko
Add the tests for the POMsgSet functions
145
9315.1.3 by Henning Eggers
Fixed test for browser helper functions.
146
    >>> from lp.translations.browser.browser_helpers import (
5106.4.52 by Jeroen Vermeulen
Functions moved from pomsgset to translationmessage. Passes.
147
    ...     text_to_html)
3691.189.4 by kiko
Add the tests for the POMsgSet functions
148
149
First, do no harm.
150
3691.8.45 by Carlos Perello Marin
Removed action values for translation forms, and fixed the rendering of active translations so we show UI tags for it.
151
    >>> text_to_html(u'foo bar', [], '<sp>')
3691.189.4 by kiko
Add the tests for the POMsgSet functions
152
    u'foo bar'
153
154
Test replacement of leading and trailing spaces.
155
3691.8.45 by Carlos Perello Marin
Removed action values for translation forms, and fixed the rendering of active translations so we show UI tags for it.
156
    >>> text_to_html(u' foo bar', [], '<sp>')
3691.189.32 by kiko
Deal with first set of review comments from James.
157
    u'<sp>foo bar'
3691.8.45 by Carlos Perello Marin
Removed action values for translation forms, and fixed the rendering of active translations so we show UI tags for it.
158
    >>> text_to_html(u'foo bar ', [], '<sp>')
3691.189.32 by kiko
Deal with first set of review comments from James.
159
    u'foo bar<sp>'
3691.8.45 by Carlos Perello Marin
Removed action values for translation forms, and fixed the rendering of active translations so we show UI tags for it.
160
    >>> text_to_html(u'  foo bar  ', [], '<sp>')
3691.189.32 by kiko
Deal with first set of review comments from James.
161
    u'<sp><sp>foo bar<sp><sp>'
3691.189.4 by kiko
Add the tests for the POMsgSet functions
162
163
Test replacement of newlines.
164
3691.8.45 by Carlos Perello Marin
Removed action values for translation forms, and fixed the rendering of active translations so we show UI tags for it.
165
    >>> text_to_html(u'foo\nbar', [], newline='<cr>')
3691.189.32 by kiko
Deal with first set of review comments from James.
166
    u'foo<cr>bar'
3691.189.4 by kiko
Add the tests for the POMsgSet functions
167
168
And both together.
169
3691.8.45 by Carlos Perello Marin
Removed action values for translation forms, and fixed the rendering of active translations so we show UI tags for it.
170
    >>> text_to_html(u'foo \nbar', [], '<sp>', '<cr>')
3691.189.32 by kiko
Deal with first set of review comments from James.
171
    u'foo<sp><cr>bar'
3691.189.4 by kiko
Add the tests for the POMsgSet functions
172
173
Test treatment of tabs.
174
3691.8.45 by Carlos Perello Marin
Removed action values for translation forms, and fixed the rendering of active translations so we show UI tags for it.
175
    >>> text_to_html(u'foo\tbar', [])
3691.189.4 by kiko
Add the tests for the POMsgSet functions
176
    u'foo<code>[tab]</code>bar'
177
178
Test valid C format strings are formatted.
179
3691.8.45 by Carlos Perello Marin
Removed action values for translation forms, and fixed the rendering of active translations so we show UI tags for it.
180
    >>> text_to_html(u'foo %d bar', ['c-format'])
3691.189.4 by kiko
Add the tests for the POMsgSet functions
181
    u'foo <code>%d</code> bar'
182
3691.8.45 by Carlos Perello Marin
Removed action values for translation forms, and fixed the rendering of active translations so we show UI tags for it.
183
If we get None, we return None.
184
185
    >>> text_to_html(None, []) is None
186
    True
187
3691.189.4 by kiko
Add the tests for the POMsgSet functions
188
Test bad format strings are caught and passed through.
189
190
    >>> text = u'foo %z bar'
191
    >>> parse_cformat_string(text)
192
    Traceback (most recent call last):
193
    ...
194
    UnrecognisedCFormatString: foo %z bar
195
3691.8.45 by Carlos Perello Marin
Removed action values for translation forms, and fixed the rendering of active translations so we show UI tags for it.
196
    >>> text_to_html(text, ['c-format']) == text
3691.189.4 by kiko
Add the tests for the POMsgSet functions
197
    True
198
3691.8.45 by Carlos Perello Marin
Removed action values for translation forms, and fixed the rendering of active translations so we show UI tags for it.
199
If we get '\r\n' as the new line mark, we should remove '\r':
200
201
    >>> text_to_html(u'foo\r\nbar', [])
202
    u'foo<img alt="" src="/@@/translation-newline" /><br/>\nbar'
203
204
And '\r' should be also handled:
205
206
    >>> text_to_html(u'foo\rbar', [])
207
    u'foo<img alt="" src="/@@/translation-newline" /><br/>\nbar'
208
3691.189.4 by kiko
Add the tests for the POMsgSet functions
209
11662.2.2 by Nicolas Delvaux
Fix all 'make lint' warnings.
210
convert_newlines_to_web_form
211
----------------------------
3691.189.4 by kiko
Add the tests for the POMsgSet functions
212
9315.1.3 by Henning Eggers
Fixed test for browser helper functions.
213
    >>> from lp.translations.browser.browser_helpers import (
5106.4.52 by Jeroen Vermeulen
Functions moved from pomsgset to translationmessage. Passes.
214
    ...     convert_newlines_to_web_form)
3691.189.4 by kiko
Add the tests for the POMsgSet functions
215
    >>> convert_newlines_to_web_form(u'foo')
216
    u'foo'
217
    >>> convert_newlines_to_web_form(u'foo\n')
218
    u'foo\r\n'
219
    >>> convert_newlines_to_web_form(u'foo\nbar\n\nbaz')
220
    u'foo\r\nbar\r\n\r\nbaz'
221
    >>> convert_newlines_to_web_form(u'foo\r\nbar')
222
    u'foo\r\nbar'
223
    >>> convert_newlines_to_web_form(u'foo\rbar')
224
    u'foo\r\nbar'
3691.276.48 by Carlos Perello Marin
Use input buttons instead of textareas when the string is a sigle line and has less than 60 characters but adding an option to get a textarea when the user wants to add more than one line
225
226
11662.2.2 by Nicolas Delvaux
Fix all 'make lint' warnings.
227
count_lines
228
-----------
3691.276.48 by Carlos Perello Marin
Use input buttons instead of textareas when the string is a sigle line and has less than 60 characters but adding an option to get a textarea when the user wants to add more than one line
229
9315.1.3 by Henning Eggers
Fixed test for browser helper functions.
230
    >>> from lp.translations.browser.browser_helpers import count_lines
9125.1.2 by Danilo Šegan
Reindent some old tests (actually, parts of them) to 4 spaces.
231
    >>> count_lines("foo")
232
    1
233
    >>> count_lines(
234
    ...     "123456789abc123456789abc123456789abc1234566789abc123456789abc")
235
    2
236
    >>> count_lines(
237
    ...     "123456789a123456789a123456789a1234566789a123456789")
238
    1
239
    >>> count_lines("a\nb")
240
    2
241
    >>> count_lines("a\nb\n")
242
    3
243
    >>> count_lines("a\nb\nc")
244
    3
245
    >>> count_lines(
246
    ...     "123456789abc123456789abc123456789abc123456789abc\n"
247
    ...     "1234566789a123456789a")
248
    2
249
    >>> count_lines(
11662.2.2 by Nicolas Delvaux
Fix all 'make lint' warnings.
250
    ...     "123456789abc123456789abc123456789abc123456789abc123456789abc"
251
    ...     "123456\n789a123456789a123456789a")
9125.1.2 by Danilo Šegan
Reindent some old tests (actually, parts of them) to 4 spaces.
252
    3
253
    >>> count_lines(
254
    ...     "123456789abc123456789abc123456789abc123456789abc123456789abc"
255
    ...     "123456789abc\n1234566789a123456789a123456789a")
256
    3
257
    >>> count_lines("foo bar\n")
258
    2