| 1 |
# |
| 2 |
# Copyright (C) 2007-2011 Edgewall Software, 2013-2025 the Babel team |
| 3 |
# All rights reserved. |
| 4 |
# |
| 5 |
# This software is licensed as described in the file LICENSE, which |
| 6 |
# you should have received as part of this distribution. The terms |
| 7 |
# are also available at https://github.com/python-babel/babel/blob/master/LICENSE. |
| 8 |
# |
| 9 |
# This software consists of voluntary contributions made by many |
| 10 |
# individuals. For the exact contribution history, see the revision |
| 11 |
# history and logs, available at https://github.com/python-babel/babel/commits/master/. |
| 12 |
|
| 13 |
from datetime import datetime |
| 14 |
from io import BytesIO |
| 15 |
|
| 16 |
from babel.messages import Catalog, Message, pofile |
| 17 |
|
| 18 |
|
| 19 |
def test_join_locations(): |
| 20 |
catalog = Catalog() |
| 21 |
catalog.add('foo', locations=[('main.py', 1)]) |
| 22 |
catalog.add('foo', locations=[('utils.py', 3)]) |
| 23 |
buf = BytesIO() |
| 24 |
pofile.write_po(buf, catalog, omit_header=True) |
| 25 |
assert buf.getvalue().strip() == b'''#: main.py:1 utils.py:3 |
| 26 |
msgid "foo" |
| 27 |
msgstr ""''' |
| 28 |
|
| 29 |
|
| 30 |
def test_write_po_file_with_specified_charset(): |
| 31 |
catalog = Catalog(charset='iso-8859-1') |
| 32 |
catalog.add('foo', 'äöü', locations=[('main.py', 1)]) |
| 33 |
buf = BytesIO() |
| 34 |
pofile.write_po(buf, catalog, omit_header=False) |
| 35 |
po_file = buf.getvalue().strip() |
| 36 |
assert b'"Content-Type: text/plain; charset=iso-8859-1\\n"' in po_file |
| 37 |
assert 'msgstr "äöü"'.encode('iso-8859-1') in po_file |
| 38 |
|
| 39 |
|
| 40 |
def test_duplicate_comments(): |
| 41 |
catalog = Catalog() |
| 42 |
catalog.add('foo', auto_comments=['A comment']) |
| 43 |
catalog.add('foo', auto_comments=['A comment']) |
| 44 |
buf = BytesIO() |
| 45 |
pofile.write_po(buf, catalog, omit_header=True) |
| 46 |
assert buf.getvalue().strip() == b'''#. A comment |
| 47 |
msgid "foo" |
| 48 |
msgstr ""''' |
| 49 |
|
| 50 |
|
| 51 |
def test_wrap_long_lines(): |
| 52 |
text = """Here's some text where |
| 53 |
white space and line breaks matter, and should |
| 54 |
|
| 55 |
not be removed |
| 56 |
|
| 57 |
""" |
| 58 |
catalog = Catalog() |
| 59 |
catalog.add(text, locations=[('main.py', 1)]) |
| 60 |
buf = BytesIO() |
| 61 |
pofile.write_po(buf, catalog, no_location=True, omit_header=True, |
| 62 |
width=42) |
| 63 |
assert buf.getvalue().strip() == b'''msgid "" |
| 64 |
"Here's some text where\\n" |
| 65 |
"white space and line breaks matter, and" |
| 66 |
" should\\n" |
| 67 |
"\\n" |
| 68 |
"not be removed\\n" |
| 69 |
"\\n" |
| 70 |
msgstr ""''' |
| 71 |
|
| 72 |
|
| 73 |
def test_wrap_long_lines_with_long_word(): |
| 74 |
text = """Here's some text that |
| 75 |
includesareallylongwordthatmightbutshouldnt throw us into an infinite loop |
| 76 |
""" |
| 77 |
catalog = Catalog() |
| 78 |
catalog.add(text, locations=[('main.py', 1)]) |
| 79 |
buf = BytesIO() |
| 80 |
pofile.write_po(buf, catalog, no_location=True, omit_header=True, |
| 81 |
width=32) |
| 82 |
assert buf.getvalue().strip() == b'''msgid "" |
| 83 |
"Here's some text that\\n" |
| 84 |
"includesareallylongwordthatmightbutshouldnt" |
| 85 |
" throw us into an infinite " |
| 86 |
"loop\\n" |
| 87 |
msgstr ""''' |
| 88 |
|
| 89 |
|
| 90 |
def test_wrap_long_lines_in_header(): |
| 91 |
""" |
| 92 |
Verify that long lines in the header comment are wrapped correctly. |
| 93 |
""" |
| 94 |
catalog = Catalog(project='AReallyReallyLongNameForAProject', |
| 95 |
revision_date=datetime(2007, 4, 1)) |
| 96 |
buf = BytesIO() |
| 97 |
pofile.write_po(buf, catalog) |
| 98 |
assert b'\n'.join(buf.getvalue().splitlines()[:7]) == b'''\ |
| 99 |
# Translations template for AReallyReallyLongNameForAProject. |
| 100 |
# Copyright (C) 2007 ORGANIZATION |
| 101 |
# This file is distributed under the same license as the |
| 102 |
# AReallyReallyLongNameForAProject project. |
| 103 |
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007. |
| 104 |
# |
| 105 |
#, fuzzy''' |
| 106 |
|
| 107 |
|
| 108 |
def test_wrap_locations_with_hyphens(): |
| 109 |
catalog = Catalog() |
| 110 |
catalog.add('foo', locations=[ |
| 111 |
('doupy/templates/base/navmenu.inc.html.py', 60), |
| 112 |
]) |
| 113 |
catalog.add('foo', locations=[ |
| 114 |
('doupy/templates/job-offers/helpers.html', 22), |
| 115 |
]) |
| 116 |
buf = BytesIO() |
| 117 |
pofile.write_po(buf, catalog, omit_header=True) |
| 118 |
assert buf.getvalue().strip() == b'''#: doupy/templates/base/navmenu.inc.html.py:60 |
| 119 |
#: doupy/templates/job-offers/helpers.html:22 |
| 120 |
msgid "foo" |
| 121 |
msgstr ""''' |
| 122 |
|
| 123 |
|
| 124 |
def test_no_wrap_and_width_behaviour_on_comments(): |
| 125 |
catalog = Catalog() |
| 126 |
catalog.add("Pretty dam long message id, which must really be big " |
| 127 |
"to test this wrap behaviour, if not it won't work.", |
| 128 |
locations=[("fake.py", n) for n in range(1, 30)]) |
| 129 |
buf = BytesIO() |
| 130 |
pofile.write_po(buf, catalog, width=None, omit_header=True) |
| 131 |
assert buf.getvalue().lower() == b"""\ |
| 132 |
#: fake.py:1 fake.py:2 fake.py:3 fake.py:4 fake.py:5 fake.py:6 fake.py:7 |
| 133 |
#: fake.py:8 fake.py:9 fake.py:10 fake.py:11 fake.py:12 fake.py:13 fake.py:14 |
| 134 |
#: fake.py:15 fake.py:16 fake.py:17 fake.py:18 fake.py:19 fake.py:20 fake.py:21 |
| 135 |
#: fake.py:22 fake.py:23 fake.py:24 fake.py:25 fake.py:26 fake.py:27 fake.py:28 |
| 136 |
#: fake.py:29 |
| 137 |
msgid "pretty dam long message id, which must really be big to test this wrap behaviour, if not it won't work." |
| 138 |
msgstr "" |
| 139 |
|
| 140 |
""" |
| 141 |
buf = BytesIO() |
| 142 |
pofile.write_po(buf, catalog, width=100, omit_header=True) |
| 143 |
assert buf.getvalue().lower() == b"""\ |
| 144 |
#: fake.py:1 fake.py:2 fake.py:3 fake.py:4 fake.py:5 fake.py:6 fake.py:7 fake.py:8 fake.py:9 fake.py:10 |
| 145 |
#: fake.py:11 fake.py:12 fake.py:13 fake.py:14 fake.py:15 fake.py:16 fake.py:17 fake.py:18 fake.py:19 |
| 146 |
#: fake.py:20 fake.py:21 fake.py:22 fake.py:23 fake.py:24 fake.py:25 fake.py:26 fake.py:27 fake.py:28 |
| 147 |
#: fake.py:29 |
| 148 |
msgid "" |
| 149 |
"pretty dam long message id, which must really be big to test this wrap behaviour, if not it won't" |
| 150 |
" work." |
| 151 |
msgstr "" |
| 152 |
|
| 153 |
""" |
| 154 |
|
| 155 |
|
| 156 |
def test_pot_with_translator_comments(): |
| 157 |
catalog = Catalog() |
| 158 |
catalog.add('foo', locations=[('main.py', 1)], |
| 159 |
auto_comments=['Comment About `foo`']) |
| 160 |
catalog.add('bar', locations=[('utils.py', 3)], |
| 161 |
user_comments=['Comment About `bar` with', |
| 162 |
'multiple lines.']) |
| 163 |
buf = BytesIO() |
| 164 |
pofile.write_po(buf, catalog, omit_header=True) |
| 165 |
assert buf.getvalue().strip() == b'''#. Comment About `foo` |
| 166 |
#: main.py:1 |
| 167 |
msgid "foo" |
| 168 |
msgstr "" |
| 169 |
|
| 170 |
# Comment About `bar` with |
| 171 |
# multiple lines. |
| 172 |
#: utils.py:3 |
| 173 |
msgid "bar" |
| 174 |
msgstr ""''' |
| 175 |
|
| 176 |
|
| 177 |
def test_po_with_obsolete_message(): |
| 178 |
catalog = Catalog() |
| 179 |
catalog.add('foo', 'Voh', locations=[('main.py', 1)]) |
| 180 |
catalog.obsolete['bar'] = Message('bar', 'Bahr', |
| 181 |
locations=[('utils.py', 3)], |
| 182 |
user_comments=['User comment']) |
| 183 |
buf = BytesIO() |
| 184 |
pofile.write_po(buf, catalog, omit_header=True) |
| 185 |
assert buf.getvalue().strip() == b'''#: main.py:1 |
| 186 |
msgid "foo" |
| 187 |
msgstr "Voh" |
| 188 |
|
| 189 |
# User comment |
| 190 |
#~ msgid "bar" |
| 191 |
#~ msgstr "Bahr"''' |
| 192 |
|
| 193 |
|
| 194 |
def test_po_with_multiline_obsolete_message(): |
| 195 |
catalog = Catalog() |
| 196 |
catalog.add('foo', 'Voh', locations=[('main.py', 1)]) |
| 197 |
msgid = r"""Here's a message that covers |
| 198 |
multiple lines, and should still be handled |
| 199 |
correctly. |
| 200 |
""" |
| 201 |
msgstr = r"""Here's a message that covers |
| 202 |
multiple lines, and should still be handled |
| 203 |
correctly. |
| 204 |
""" |
| 205 |
catalog.obsolete[msgid] = Message(msgid, msgstr, |
| 206 |
locations=[('utils.py', 3)]) |
| 207 |
buf = BytesIO() |
| 208 |
pofile.write_po(buf, catalog, omit_header=True) |
| 209 |
assert buf.getvalue().strip() == b'''#: main.py:1 |
| 210 |
msgid "foo" |
| 211 |
msgstr "Voh" |
| 212 |
|
| 213 |
#~ msgid "" |
| 214 |
#~ "Here's a message that covers\\n" |
| 215 |
#~ "multiple lines, and should still be handled\\n" |
| 216 |
#~ "correctly.\\n" |
| 217 |
#~ msgstr "" |
| 218 |
#~ "Here's a message that covers\\n" |
| 219 |
#~ "multiple lines, and should still be handled\\n" |
| 220 |
#~ "correctly.\\n"''' |
| 221 |
|
| 222 |
|
| 223 |
def test_po_with_obsolete_message_ignored(): |
| 224 |
catalog = Catalog() |
| 225 |
catalog.add('foo', 'Voh', locations=[('main.py', 1)]) |
| 226 |
catalog.obsolete['bar'] = Message('bar', 'Bahr', |
| 227 |
locations=[('utils.py', 3)], |
| 228 |
user_comments=['User comment']) |
| 229 |
buf = BytesIO() |
| 230 |
pofile.write_po(buf, catalog, omit_header=True, ignore_obsolete=True) |
| 231 |
assert buf.getvalue().strip() == b'''#: main.py:1 |
| 232 |
msgid "foo" |
| 233 |
msgstr "Voh"''' |
| 234 |
|
| 235 |
|
| 236 |
def test_po_with_previous_msgid(): |
| 237 |
catalog = Catalog() |
| 238 |
catalog.add('foo', 'Voh', locations=[('main.py', 1)], |
| 239 |
previous_id='fo') |
| 240 |
buf = BytesIO() |
| 241 |
pofile.write_po(buf, catalog, omit_header=True, include_previous=True) |
| 242 |
assert buf.getvalue().strip() == b'''#: main.py:1 |
| 243 |
#| msgid "fo" |
| 244 |
msgid "foo" |
| 245 |
msgstr "Voh"''' |
| 246 |
|
| 247 |
|
| 248 |
def test_po_with_previous_msgid_plural(): |
| 249 |
catalog = Catalog() |
| 250 |
catalog.add(('foo', 'foos'), ('Voh', 'Voeh'), |
| 251 |
locations=[('main.py', 1)], previous_id=('fo', 'fos')) |
| 252 |
buf = BytesIO() |
| 253 |
pofile.write_po(buf, catalog, omit_header=True, include_previous=True) |
| 254 |
assert buf.getvalue().strip() == b'''#: main.py:1 |
| 255 |
#| msgid "fo" |
| 256 |
#| msgid_plural "fos" |
| 257 |
msgid "foo" |
| 258 |
msgid_plural "foos" |
| 259 |
msgstr[0] "Voh" |
| 260 |
msgstr[1] "Voeh"''' |
| 261 |
|
| 262 |
|
| 263 |
def test_sorted_po(): |
| 264 |
catalog = Catalog() |
| 265 |
catalog.add('bar', locations=[('utils.py', 3)], |
| 266 |
user_comments=['Comment About `bar` with', |
| 267 |
'multiple lines.']) |
| 268 |
catalog.add(('foo', 'foos'), ('Voh', 'Voeh'), |
| 269 |
locations=[('main.py', 1)]) |
| 270 |
buf = BytesIO() |
| 271 |
pofile.write_po(buf, catalog, sort_output=True) |
| 272 |
value = buf.getvalue().strip() |
| 273 |
assert b'''\ |
| 274 |
# Comment About `bar` with |
| 275 |
# multiple lines. |
| 276 |
#: utils.py:3 |
| 277 |
msgid "bar" |
| 278 |
msgstr "" |
| 279 |
|
| 280 |
#: main.py:1 |
| 281 |
msgid "foo" |
| 282 |
msgid_plural "foos" |
| 283 |
msgstr[0] "Voh" |
| 284 |
msgstr[1] "Voeh"''' in value |
| 285 |
assert value.find(b'msgid ""') < value.find(b'msgid "bar"') < value.find(b'msgid "foo"') |
| 286 |
|
| 287 |
|
| 288 |
def test_sorted_po_context(): |
| 289 |
catalog = Catalog() |
| 290 |
catalog.add(('foo', 'foos'), ('Voh', 'Voeh'), |
| 291 |
locations=[('main.py', 1)], |
| 292 |
context='there') |
| 293 |
catalog.add(('foo', 'foos'), ('Voh', 'Voeh'), |
| 294 |
locations=[('main.py', 1)]) |
| 295 |
catalog.add(('foo', 'foos'), ('Voh', 'Voeh'), |
| 296 |
locations=[('main.py', 1)], |
| 297 |
context='here') |
| 298 |
buf = BytesIO() |
| 299 |
pofile.write_po(buf, catalog, sort_output=True) |
| 300 |
value = buf.getvalue().strip() |
| 301 |
# We expect the foo without ctx, followed by "here" foo and "there" foo |
| 302 |
assert b'''\ |
| 303 |
#: main.py:1 |
| 304 |
msgid "foo" |
| 305 |
msgid_plural "foos" |
| 306 |
msgstr[0] "Voh" |
| 307 |
msgstr[1] "Voeh" |
| 308 |
|
| 309 |
#: main.py:1 |
| 310 |
msgctxt "here" |
| 311 |
msgid "foo" |
| 312 |
msgid_plural "foos" |
| 313 |
msgstr[0] "Voh" |
| 314 |
msgstr[1] "Voeh" |
| 315 |
|
| 316 |
#: main.py:1 |
| 317 |
msgctxt "there" |
| 318 |
msgid "foo" |
| 319 |
msgid_plural "foos" |
| 320 |
msgstr[0] "Voh" |
| 321 |
msgstr[1] "Voeh"''' in value |
| 322 |
|
| 323 |
|
| 324 |
def test_file_sorted_po(): |
| 325 |
catalog = Catalog() |
| 326 |
catalog.add('bar', locations=[('utils.py', 3)]) |
| 327 |
catalog.add(('foo', 'foos'), ('Voh', 'Voeh'), locations=[('main.py', 1)]) |
| 328 |
buf = BytesIO() |
| 329 |
pofile.write_po(buf, catalog, sort_by_file=True) |
| 330 |
value = buf.getvalue().strip() |
| 331 |
assert value.find(b'main.py') < value.find(b'utils.py') |
| 332 |
|
| 333 |
|
| 334 |
def test_file_with_no_lineno(): |
| 335 |
catalog = Catalog() |
| 336 |
catalog.add('bar', locations=[('utils.py', None)], |
| 337 |
user_comments=['Comment About `bar` with', |
| 338 |
'multiple lines.']) |
| 339 |
buf = BytesIO() |
| 340 |
pofile.write_po(buf, catalog, sort_output=True) |
| 341 |
value = buf.getvalue().strip() |
| 342 |
assert b'''\ |
| 343 |
# Comment About `bar` with |
| 344 |
# multiple lines. |
| 345 |
#: utils.py |
| 346 |
msgid "bar" |
| 347 |
msgstr ""''' in value |
| 348 |
|
| 349 |
|
| 350 |
def test_silent_location_fallback(): |
| 351 |
buf = BytesIO(b'''\ |
| 352 |
#: broken_file.py |
| 353 |
msgid "missing line number" |
| 354 |
msgstr "" |
| 355 |
|
| 356 |
#: broken_file.py:broken_line_number |
| 357 |
msgid "broken line number" |
| 358 |
msgstr ""''') |
| 359 |
catalog = pofile.read_po(buf) |
| 360 |
assert catalog['missing line number'].locations == [('broken_file.py', None)] |
| 361 |
assert catalog['broken line number'].locations == [] |
| 362 |
|
| 363 |
|
| 364 |
def test_include_lineno(): |
| 365 |
catalog = Catalog() |
| 366 |
catalog.add('foo', locations=[('main.py', 1)]) |
| 367 |
catalog.add('foo', locations=[('utils.py', 3)]) |
| 368 |
buf = BytesIO() |
| 369 |
pofile.write_po(buf, catalog, omit_header=True, include_lineno=True) |
| 370 |
assert buf.getvalue().strip() == b'''#: main.py:1 utils.py:3 |
| 371 |
msgid "foo" |
| 372 |
msgstr ""''' |
| 373 |
|
| 374 |
|
| 375 |
def test_no_include_lineno(): |
| 376 |
catalog = Catalog() |
| 377 |
catalog.add('foo', locations=[('main.py', 1)]) |
| 378 |
catalog.add('foo', locations=[('main.py', 2)]) |
| 379 |
catalog.add('foo', locations=[('utils.py', 3)]) |
| 380 |
buf = BytesIO() |
| 381 |
pofile.write_po(buf, catalog, omit_header=True, include_lineno=False) |
| 382 |
assert buf.getvalue().strip() == b'''#: main.py utils.py |
| 383 |
msgid "foo" |
| 384 |
msgstr ""''' |
| 385 |
|
| 386 |
|
| 387 |
def test_white_space_in_location(): |
| 388 |
catalog = Catalog() |
| 389 |
catalog.add('foo', locations=[('main.py', 1)]) |
| 390 |
catalog.add('foo', locations=[('utils b.py', 3)]) |
| 391 |
buf = BytesIO() |
| 392 |
pofile.write_po(buf, catalog, omit_header=True, include_lineno=True) |
| 393 |
assert buf.getvalue().strip() == b'''#: main.py:1 \xe2\x81\xa8utils b.py\xe2\x81\xa9:3 |
| 394 |
msgid "foo" |
| 395 |
msgstr ""''' |
| 396 |
|
| 397 |
|
| 398 |
def test_white_space_in_location_already_enclosed(): |
| 399 |
catalog = Catalog() |
| 400 |
catalog.add('foo', locations=[('main.py', 1)]) |
| 401 |
catalog.add('foo', locations=[('\u2068utils b.py\u2069', 3)]) |
| 402 |
buf = BytesIO() |
| 403 |
pofile.write_po(buf, catalog, omit_header=True, include_lineno=True) |
| 404 |
assert buf.getvalue().strip() == b'''#: main.py:1 \xe2\x81\xa8utils b.py\xe2\x81\xa9:3 |
| 405 |
msgid "foo" |
| 406 |
msgstr ""''' |
| 407 |
|
| 408 |
|
| 409 |
def test_tab_in_location(): |
| 410 |
catalog = Catalog() |
| 411 |
catalog.add('foo', locations=[('main.py', 1)]) |
| 412 |
catalog.add('foo', locations=[('utils\tb.py', 3)]) |
| 413 |
buf = BytesIO() |
| 414 |
pofile.write_po(buf, catalog, omit_header=True, include_lineno=True) |
| 415 |
assert buf.getvalue().strip() == b'''#: main.py:1 \xe2\x81\xa8utils b.py\xe2\x81\xa9:3 |
| 416 |
msgid "foo" |
| 417 |
msgstr ""''' |
| 418 |
|
| 419 |
|
| 420 |
def test_tab_in_location_already_enclosed(): |
| 421 |
catalog = Catalog() |
| 422 |
catalog.add('foo', locations=[('main.py', 1)]) |
| 423 |
catalog.add('foo', locations=[('\u2068utils\tb.py\u2069', 3)]) |
| 424 |
buf = BytesIO() |
| 425 |
pofile.write_po(buf, catalog, omit_header=True, include_lineno=True) |
| 426 |
assert buf.getvalue().strip() == b'''#: main.py:1 \xe2\x81\xa8utils b.py\xe2\x81\xa9:3 |
| 427 |
msgid "foo" |
| 428 |
msgstr ""''' |
| 429 |
|
| 430 |
|
| 431 |
def test_wrap_with_enclosed_file_locations(): |
| 432 |
# Ensure that file names containing white space are not wrapped regardless of the --width parameter |
| 433 |
catalog = Catalog() |
| 434 |
catalog.add('foo', locations=[('\u2068test utils.py\u2069', 1)]) |
| 435 |
catalog.add('foo', locations=[('\u2068test utils.py\u2069', 3)]) |
| 436 |
buf = BytesIO() |
| 437 |
pofile.write_po(buf, catalog, omit_header=True, include_lineno=True, width=1) |
| 438 |
assert buf.getvalue().strip() == b'''#: \xe2\x81\xa8test utils.py\xe2\x81\xa9:1 |
| 439 |
#: \xe2\x81\xa8test utils.py\xe2\x81\xa9:3 |
| 440 |
msgid "foo" |
| 441 |
msgstr ""''' |