| 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 |
import copy |
| 14 |
import datetime |
| 15 |
import pickle |
| 16 |
from io import StringIO |
| 17 |
|
| 18 |
from babel.dates import UTC, format_datetime |
| 19 |
from babel.messages import catalog, pofile |
| 20 |
from babel.util import FixedOffsetTimezone |
| 21 |
|
| 22 |
|
| 23 |
def test_message_python_format(): |
| 24 |
assert catalog.PYTHON_FORMAT.search('foo %d bar') |
| 25 |
assert catalog.PYTHON_FORMAT.search('foo %s bar') |
| 26 |
assert catalog.PYTHON_FORMAT.search('foo %r bar') |
| 27 |
assert catalog.PYTHON_FORMAT.search('foo %(name).1f') |
| 28 |
assert catalog.PYTHON_FORMAT.search('foo %(name)3.3f') |
| 29 |
assert catalog.PYTHON_FORMAT.search('foo %(name)3f') |
| 30 |
assert catalog.PYTHON_FORMAT.search('foo %(name)06d') |
| 31 |
assert catalog.PYTHON_FORMAT.search('foo %(name)Li') |
| 32 |
assert catalog.PYTHON_FORMAT.search('foo %(name)#d') |
| 33 |
assert catalog.PYTHON_FORMAT.search('foo %(name)-4.4hs') |
| 34 |
assert catalog.PYTHON_FORMAT.search('foo %(name)*.3f') |
| 35 |
assert catalog.PYTHON_FORMAT.search('foo %(name).*f') |
| 36 |
assert catalog.PYTHON_FORMAT.search('foo %(name)3.*f') |
| 37 |
assert catalog.PYTHON_FORMAT.search('foo %(name)*.*f') |
| 38 |
assert catalog.PYTHON_FORMAT.search('foo %()s') |
| 39 |
|
| 40 |
|
| 41 |
def test_message_python_brace_format(): |
| 42 |
assert not catalog._has_python_brace_format('') |
| 43 |
assert not catalog._has_python_brace_format('foo') |
| 44 |
assert not catalog._has_python_brace_format('{') |
| 45 |
assert not catalog._has_python_brace_format('}') |
| 46 |
assert not catalog._has_python_brace_format('{} {') |
| 47 |
assert not catalog._has_python_brace_format('{{}}') |
| 48 |
assert catalog._has_python_brace_format('{}') |
| 49 |
assert catalog._has_python_brace_format('foo {name}') |
| 50 |
assert catalog._has_python_brace_format('foo {name!s}') |
| 51 |
assert catalog._has_python_brace_format('foo {name!r}') |
| 52 |
assert catalog._has_python_brace_format('foo {name!a}') |
| 53 |
assert catalog._has_python_brace_format('foo {name!r:10}') |
| 54 |
assert catalog._has_python_brace_format('foo {name!r:10.2}') |
| 55 |
assert catalog._has_python_brace_format('foo {name!r:10.2f}') |
| 56 |
assert catalog._has_python_brace_format('foo {name!r:10.2f} {name!r:10.2f}') |
| 57 |
assert catalog._has_python_brace_format('foo {name!r:10.2f=}') |
| 58 |
|
| 59 |
|
| 60 |
def test_message_translator_comments(): |
| 61 |
mess = catalog.Message('foo', user_comments=['Comment About `foo`']) |
| 62 |
assert mess.user_comments == ['Comment About `foo`'] |
| 63 |
mess = catalog.Message('foo', |
| 64 |
auto_comments=['Comment 1 About `foo`', |
| 65 |
'Comment 2 About `foo`']) |
| 66 |
assert mess.auto_comments == ['Comment 1 About `foo`', 'Comment 2 About `foo`'] |
| 67 |
|
| 68 |
|
| 69 |
def test_message_clone_message_object(): |
| 70 |
msg = catalog.Message('foo', locations=[('foo.py', 42)]) |
| 71 |
clone = msg.clone() |
| 72 |
clone.locations.append(('bar.py', 42)) |
| 73 |
assert msg.locations == [('foo.py', 42)] |
| 74 |
msg.flags.add('fuzzy') |
| 75 |
assert not clone.fuzzy and msg.fuzzy |
| 76 |
|
| 77 |
|
| 78 |
def test_catalog_add_returns_message_instance(): |
| 79 |
cat = catalog.Catalog() |
| 80 |
message = cat.add('foo') |
| 81 |
assert message.id == 'foo' |
| 82 |
|
| 83 |
|
| 84 |
def test_catalog_two_messages_with_same_singular(): |
| 85 |
cat = catalog.Catalog() |
| 86 |
cat.add('foo') |
| 87 |
cat.add(('foo', 'foos')) |
| 88 |
assert len(cat) == 1 |
| 89 |
|
| 90 |
|
| 91 |
def test_catalog_duplicate_auto_comment(): |
| 92 |
cat = catalog.Catalog() |
| 93 |
cat.add('foo', auto_comments=['A comment']) |
| 94 |
cat.add('foo', auto_comments=['A comment', 'Another comment']) |
| 95 |
assert cat['foo'].auto_comments == ['A comment', 'Another comment'] |
| 96 |
|
| 97 |
|
| 98 |
def test_catalog_duplicate_user_comment(): |
| 99 |
cat = catalog.Catalog() |
| 100 |
cat.add('foo', user_comments=['A comment']) |
| 101 |
cat.add('foo', user_comments=['A comment', 'Another comment']) |
| 102 |
assert cat['foo'].user_comments == ['A comment', 'Another comment'] |
| 103 |
|
| 104 |
|
| 105 |
def test_catalog_duplicate_location(): |
| 106 |
cat = catalog.Catalog() |
| 107 |
cat.add('foo', locations=[('foo.py', 1)]) |
| 108 |
cat.add('foo', locations=[('foo.py', 1)]) |
| 109 |
assert cat['foo'].locations == [('foo.py', 1)] |
| 110 |
|
| 111 |
|
| 112 |
def test_catalog_update_message_changed_to_plural(): |
| 113 |
cat = catalog.Catalog() |
| 114 |
cat.add('foo', 'Voh') |
| 115 |
tmpl = catalog.Catalog() |
| 116 |
tmpl.add(('foo', 'foos')) |
| 117 |
cat.update(tmpl) |
| 118 |
assert cat['foo'].string == ('Voh', '') |
| 119 |
assert cat['foo'].fuzzy |
| 120 |
|
| 121 |
|
| 122 |
def test_catalog_update_message_changed_to_simple(): |
| 123 |
cat = catalog.Catalog() |
| 124 |
cat.add('foo' 'foos', ('Voh', 'Vöhs')) |
| 125 |
tmpl = catalog.Catalog() |
| 126 |
tmpl.add('foo') |
| 127 |
cat.update(tmpl) |
| 128 |
assert cat['foo'].string == 'Voh' |
| 129 |
assert cat['foo'].fuzzy |
| 130 |
|
| 131 |
|
| 132 |
def test_catalog_update_message_updates_comments(): |
| 133 |
cat = catalog.Catalog() |
| 134 |
cat['foo'] = catalog.Message('foo', locations=[('main.py', 5)]) |
| 135 |
assert cat['foo'].auto_comments == [] |
| 136 |
assert cat['foo'].user_comments == [] |
| 137 |
# Update cat['foo'] with a new location and a comment |
| 138 |
cat['foo'] = catalog.Message('foo', locations=[('main.py', 7)], |
| 139 |
user_comments=['Foo Bar comment 1']) |
| 140 |
assert cat['foo'].user_comments == ['Foo Bar comment 1'] |
| 141 |
# now add yet another location with another comment |
| 142 |
cat['foo'] = catalog.Message('foo', locations=[('main.py', 9)], |
| 143 |
auto_comments=['Foo Bar comment 2']) |
| 144 |
assert cat['foo'].auto_comments == ['Foo Bar comment 2'] |
| 145 |
|
| 146 |
|
| 147 |
def test_catalog_update_fuzzy_matching_with_case_change(): |
| 148 |
cat = catalog.Catalog() |
| 149 |
cat.add('FOO', 'Voh') |
| 150 |
cat.add('bar', 'Bahr') |
| 151 |
tmpl = catalog.Catalog() |
| 152 |
tmpl.add('foo') |
| 153 |
cat.update(tmpl) |
| 154 |
assert len(cat.obsolete) == 1 |
| 155 |
assert 'FOO' not in cat |
| 156 |
|
| 157 |
assert cat['foo'].string == 'Voh' |
| 158 |
assert cat['foo'].fuzzy is True |
| 159 |
|
| 160 |
|
| 161 |
def test_catalog_update_fuzzy_matching_with_char_change(): |
| 162 |
cat = catalog.Catalog() |
| 163 |
cat.add('fo', 'Voh') |
| 164 |
cat.add('bar', 'Bahr') |
| 165 |
tmpl = catalog.Catalog() |
| 166 |
tmpl.add('foo') |
| 167 |
cat.update(tmpl) |
| 168 |
assert len(cat.obsolete) == 1 |
| 169 |
assert 'fo' not in cat |
| 170 |
|
| 171 |
assert cat['foo'].string == 'Voh' |
| 172 |
assert cat['foo'].fuzzy is True |
| 173 |
|
| 174 |
|
| 175 |
def test_catalog_update_fuzzy_matching_no_msgstr(): |
| 176 |
cat = catalog.Catalog() |
| 177 |
cat.add('fo', '') |
| 178 |
tmpl = catalog.Catalog() |
| 179 |
tmpl.add('fo') |
| 180 |
tmpl.add('foo') |
| 181 |
cat.update(tmpl) |
| 182 |
assert 'fo' in cat |
| 183 |
assert 'foo' in cat |
| 184 |
|
| 185 |
assert cat['fo'].string == '' |
| 186 |
assert cat['fo'].fuzzy is False |
| 187 |
assert cat['foo'].string is None |
| 188 |
assert cat['foo'].fuzzy is False |
| 189 |
|
| 190 |
|
| 191 |
def test_catalog_update_fuzzy_matching_with_new_context(): |
| 192 |
cat = catalog.Catalog() |
| 193 |
cat.add('foo', 'Voh') |
| 194 |
cat.add('bar', 'Bahr') |
| 195 |
tmpl = catalog.Catalog() |
| 196 |
tmpl.add('Foo', context='Menu') |
| 197 |
cat.update(tmpl) |
| 198 |
assert len(cat.obsolete) == 1 |
| 199 |
assert 'foo' not in cat |
| 200 |
|
| 201 |
message = cat.get('Foo', 'Menu') |
| 202 |
assert message.string == 'Voh' |
| 203 |
assert message.fuzzy is True |
| 204 |
assert message.context == 'Menu' |
| 205 |
|
| 206 |
|
| 207 |
def test_catalog_update_fuzzy_matching_with_changed_context(): |
| 208 |
cat = catalog.Catalog() |
| 209 |
cat.add('foo', 'Voh', context='Menu|File') |
| 210 |
cat.add('bar', 'Bahr', context='Menu|File') |
| 211 |
tmpl = catalog.Catalog() |
| 212 |
tmpl.add('Foo', context='Menu|Edit') |
| 213 |
cat.update(tmpl) |
| 214 |
assert len(cat.obsolete) == 1 |
| 215 |
assert cat.get('Foo', 'Menu|File') is None |
| 216 |
|
| 217 |
message = cat.get('Foo', 'Menu|Edit') |
| 218 |
assert message.string == 'Voh' |
| 219 |
assert message.fuzzy is True |
| 220 |
assert message.context == 'Menu|Edit' |
| 221 |
|
| 222 |
|
| 223 |
def test_catalog_update_fuzzy_matching_no_cascading(): |
| 224 |
cat = catalog.Catalog() |
| 225 |
cat.add('fo', 'Voh') |
| 226 |
cat.add('foo', 'Vohe') |
| 227 |
tmpl = catalog.Catalog() |
| 228 |
tmpl.add('fo') |
| 229 |
tmpl.add('foo') |
| 230 |
tmpl.add('fooo') |
| 231 |
cat.update(tmpl) |
| 232 |
assert 'fo' in cat |
| 233 |
assert 'foo' in cat |
| 234 |
|
| 235 |
assert cat['fo'].string == 'Voh' |
| 236 |
assert cat['fo'].fuzzy is False |
| 237 |
assert cat['foo'].string == 'Vohe' |
| 238 |
assert cat['foo'].fuzzy is False |
| 239 |
assert cat['fooo'].string == 'Vohe' |
| 240 |
assert cat['fooo'].fuzzy is True |
| 241 |
|
| 242 |
|
| 243 |
def test_catalog_update_fuzzy_matching_long_string(): |
| 244 |
lipsum = "\ |
| 245 |
Lorem Ipsum is simply dummy text of the printing and typesetting \ |
| 246 |
industry. Lorem Ipsum has been the industry's standard dummy text ever \ |
| 247 |
since the 1500s, when an unknown printer took a galley of type and \ |
| 248 |
scrambled it to make a type specimen book. It has survived not only \ |
| 249 |
five centuries, but also the leap into electronic typesetting, \ |
| 250 |
remaining essentially unchanged. It was popularised in the 1960s with \ |
| 251 |
the release of Letraset sheets containing Lorem Ipsum passages, and \ |
| 252 |
more recently with desktop publishing software like Aldus PageMaker \ |
| 253 |
including versions of Lorem Ipsum." |
| 254 |
cat = catalog.Catalog() |
| 255 |
cat.add("ZZZZZZ " + lipsum, "foo") |
| 256 |
tmpl = catalog.Catalog() |
| 257 |
tmpl.add(lipsum + " ZZZZZZ") |
| 258 |
cat.update(tmpl) |
| 259 |
assert cat[lipsum + " ZZZZZZ"].fuzzy is True |
| 260 |
assert len(cat.obsolete) == 0 |
| 261 |
|
| 262 |
|
| 263 |
def test_catalog_update_without_fuzzy_matching(): |
| 264 |
cat = catalog.Catalog() |
| 265 |
cat.add('fo', 'Voh') |
| 266 |
cat.add('bar', 'Bahr') |
| 267 |
tmpl = catalog.Catalog() |
| 268 |
tmpl.add('foo') |
| 269 |
cat.update(tmpl, no_fuzzy_matching=True) |
| 270 |
assert len(cat.obsolete) == 2 |
| 271 |
|
| 272 |
|
| 273 |
def test_catalog_fuzzy_matching_regarding_plurals(): |
| 274 |
cat = catalog.Catalog() |
| 275 |
cat.add(('foo', 'foh'), ('foo', 'foh')) |
| 276 |
ru = copy.copy(cat) |
| 277 |
ru.locale = 'ru_RU' |
| 278 |
ru.update(cat) |
| 279 |
assert ru['foo'].fuzzy is True |
| 280 |
ru = copy.copy(cat) |
| 281 |
ru.locale = 'ru_RU' |
| 282 |
ru['foo'].string = ('foh', 'fohh', 'fohhh') |
| 283 |
ru.update(cat) |
| 284 |
assert ru['foo'].fuzzy is False |
| 285 |
|
| 286 |
|
| 287 |
def test_catalog_update_no_template_mutation(): |
| 288 |
tmpl = catalog.Catalog() |
| 289 |
tmpl.add('foo') |
| 290 |
cat1 = catalog.Catalog() |
| 291 |
cat1.add('foo', 'Voh') |
| 292 |
cat1.update(tmpl) |
| 293 |
cat2 = catalog.Catalog() |
| 294 |
cat2.update(tmpl) |
| 295 |
|
| 296 |
assert cat2['foo'].string is None |
| 297 |
assert cat2['foo'].fuzzy is False |
| 298 |
|
| 299 |
|
| 300 |
def test_catalog_update_po_updates_pot_creation_date(): |
| 301 |
template = catalog.Catalog() |
| 302 |
localized_catalog = copy.deepcopy(template) |
| 303 |
localized_catalog.locale = 'de_DE' |
| 304 |
assert template.mime_headers != localized_catalog.mime_headers |
| 305 |
assert template.creation_date == localized_catalog.creation_date |
| 306 |
template.creation_date = datetime.datetime.now() - \ |
| 307 |
datetime.timedelta(minutes=5) |
| 308 |
localized_catalog.update(template) |
| 309 |
assert template.creation_date == localized_catalog.creation_date |
| 310 |
|
| 311 |
|
| 312 |
def test_catalog_update_po_ignores_pot_creation_date(): |
| 313 |
template = catalog.Catalog() |
| 314 |
localized_catalog = copy.deepcopy(template) |
| 315 |
localized_catalog.locale = 'de_DE' |
| 316 |
assert template.mime_headers != localized_catalog.mime_headers |
| 317 |
assert template.creation_date == localized_catalog.creation_date |
| 318 |
template.creation_date = datetime.datetime.now() - \ |
| 319 |
datetime.timedelta(minutes=5) |
| 320 |
localized_catalog.update(template, update_creation_date=False) |
| 321 |
assert template.creation_date != localized_catalog.creation_date |
| 322 |
|
| 323 |
|
| 324 |
def test_catalog_update_po_keeps_po_revision_date(): |
| 325 |
template = catalog.Catalog() |
| 326 |
localized_catalog = copy.deepcopy(template) |
| 327 |
localized_catalog.locale = 'de_DE' |
| 328 |
fake_rev_date = datetime.datetime.now() - datetime.timedelta(days=5) |
| 329 |
localized_catalog.revision_date = fake_rev_date |
| 330 |
assert template.mime_headers != localized_catalog.mime_headers |
| 331 |
assert template.creation_date == localized_catalog.creation_date |
| 332 |
template.creation_date = datetime.datetime.now() - \ |
| 333 |
datetime.timedelta(minutes=5) |
| 334 |
localized_catalog.update(template) |
| 335 |
assert localized_catalog.revision_date == fake_rev_date |
| 336 |
|
| 337 |
|
| 338 |
def test_catalog_stores_datetime_correctly(): |
| 339 |
localized = catalog.Catalog() |
| 340 |
localized.locale = 'de_DE' |
| 341 |
localized[''] = catalog.Message('', |
| 342 |
"POT-Creation-Date: 2009-03-09 15:47-0700\n" + |
| 343 |
"PO-Revision-Date: 2009-03-09 15:47-0700\n") |
| 344 |
for key, value in localized.mime_headers: |
| 345 |
if key in ('POT-Creation-Date', 'PO-Revision-Date'): |
| 346 |
assert value == '2009-03-09 15:47-0700' |
| 347 |
|
| 348 |
|
| 349 |
def test_catalog_mime_headers_contain_same_information_as_attributes(): |
| 350 |
cat = catalog.Catalog() |
| 351 |
cat[''] = catalog.Message('', |
| 352 |
"Last-Translator: Foo Bar <[email protected]>\n" + |
| 353 |
"Language-Team: de <[email protected]>\n" + |
| 354 |
"POT-Creation-Date: 2009-03-01 11:20+0200\n" + |
| 355 |
"PO-Revision-Date: 2009-03-09 15:47-0700\n") |
| 356 |
assert cat.locale is None |
| 357 |
mime_headers = dict(cat.mime_headers) |
| 358 |
|
| 359 |
assert cat.last_translator == 'Foo Bar <[email protected]>' |
| 360 |
assert mime_headers['Last-Translator'] == 'Foo Bar <[email protected]>' |
| 361 |
|
| 362 |
assert cat.language_team == 'de <[email protected]>' |
| 363 |
assert mime_headers['Language-Team'] == 'de <[email protected]>' |
| 364 |
|
| 365 |
dt = datetime.datetime(2009, 3, 9, 15, 47, tzinfo=FixedOffsetTimezone(-7 * 60)) |
| 366 |
assert cat.revision_date == dt |
| 367 |
formatted_dt = format_datetime(dt, 'yyyy-MM-dd HH:mmZ', locale='en') |
| 368 |
assert mime_headers['PO-Revision-Date'] == formatted_dt |
| 369 |
|
| 370 |
|
| 371 |
def test_message_fuzzy(): |
| 372 |
assert not catalog.Message('foo').fuzzy |
| 373 |
msg = catalog.Message('foo', 'foo', flags=['fuzzy']) |
| 374 |
assert msg.fuzzy |
| 375 |
assert msg.id == 'foo' |
| 376 |
|
| 377 |
|
| 378 |
def test_message_pluralizable(): |
| 379 |
assert not catalog.Message('foo').pluralizable |
| 380 |
assert catalog.Message(('foo', 'bar')).pluralizable |
| 381 |
|
| 382 |
|
| 383 |
def test_message_python_format_2(): |
| 384 |
assert not catalog.Message('foo').python_format |
| 385 |
assert not catalog.Message(('foo', 'foo')).python_format |
| 386 |
assert catalog.Message('foo %(name)s bar').python_format |
| 387 |
assert catalog.Message(('foo %(name)s', 'foo %(name)s')).python_format |
| 388 |
|
| 389 |
|
| 390 |
def test_message_python_brace_format_2(): |
| 391 |
assert not catalog.Message('foo').python_brace_format |
| 392 |
assert not catalog.Message(('foo', 'foo')).python_brace_format |
| 393 |
assert catalog.Message('foo {name} bar').python_brace_format |
| 394 |
assert catalog.Message(('foo {name}', 'foo {name}')).python_brace_format |
| 395 |
|
| 396 |
|
| 397 |
def test_catalog(): |
| 398 |
cat = catalog.Catalog(project='Foobar', version='1.0', |
| 399 |
copyright_holder='Foo Company') |
| 400 |
assert cat.header_comment == ( |
| 401 |
'# Translations template for Foobar.\n' |
| 402 |
'# Copyright (C) %(year)d Foo Company\n' |
| 403 |
'# This file is distributed under the same ' |
| 404 |
'license as the Foobar project.\n' |
| 405 |
'# FIRST AUTHOR <EMAIL@ADDRESS>, %(year)d.\n' |
| 406 |
'#') % {'year': datetime.date.today().year} |
| 407 |
|
| 408 |
cat = catalog.Catalog(project='Foobar', version='1.0', |
| 409 |
copyright_holder='Foo Company') |
| 410 |
cat.header_comment = ( |
| 411 |
'# The POT for my really cool PROJECT project.\n' |
| 412 |
'# Copyright (C) 1990-2003 ORGANIZATION\n' |
| 413 |
'# This file is distributed under the same license as the PROJECT\n' |
| 414 |
'# project.\n' |
| 415 |
'#\n') |
| 416 |
assert cat.header_comment == ( |
| 417 |
'# The POT for my really cool Foobar project.\n' |
| 418 |
'# Copyright (C) 1990-2003 Foo Company\n' |
| 419 |
'# This file is distributed under the same license as the Foobar\n' |
| 420 |
'# project.\n' |
| 421 |
'#\n') |
| 422 |
|
| 423 |
|
| 424 |
def test_catalog_mime_headers(): |
| 425 |
created = datetime.datetime(1990, 4, 1, 15, 30, tzinfo=UTC) |
| 426 |
cat = catalog.Catalog(project='Foobar', version='1.0', |
| 427 |
creation_date=created) |
| 428 |
assert cat.mime_headers == [ |
| 429 |
('Project-Id-Version', 'Foobar 1.0'), |
| 430 |
('Report-Msgid-Bugs-To', 'EMAIL@ADDRESS'), |
| 431 |
('POT-Creation-Date', '1990-04-01 15:30+0000'), |
| 432 |
('PO-Revision-Date', 'YEAR-MO-DA HO:MI+ZONE'), |
| 433 |
('Last-Translator', 'FULL NAME <EMAIL@ADDRESS>'), |
| 434 |
('Language-Team', 'LANGUAGE <[email protected]>'), |
| 435 |
('MIME-Version', '1.0'), |
| 436 |
('Content-Type', 'text/plain; charset=utf-8'), |
| 437 |
('Content-Transfer-Encoding', '8bit'), |
| 438 |
('Generated-By', f'Babel {catalog.VERSION}\n'), |
| 439 |
] |
| 440 |
|
| 441 |
|
| 442 |
def test_catalog_mime_headers_set_locale(): |
| 443 |
created = datetime.datetime(1990, 4, 1, 15, 30, tzinfo=UTC) |
| 444 |
revised = datetime.datetime(1990, 8, 3, 12, 0, tzinfo=UTC) |
| 445 |
cat = catalog.Catalog(locale='de_DE', project='Foobar', version='1.0', |
| 446 |
creation_date=created, revision_date=revised, |
| 447 |
last_translator='John Doe <[email protected]>', |
| 448 |
language_team='de_DE <[email protected]>') |
| 449 |
assert cat.mime_headers == [ |
| 450 |
('Project-Id-Version', 'Foobar 1.0'), |
| 451 |
('Report-Msgid-Bugs-To', 'EMAIL@ADDRESS'), |
| 452 |
('POT-Creation-Date', '1990-04-01 15:30+0000'), |
| 453 |
('PO-Revision-Date', '1990-08-03 12:00+0000'), |
| 454 |
('Last-Translator', 'John Doe <[email protected]>'), |
| 455 |
('Language', 'de_DE'), |
| 456 |
('Language-Team', 'de_DE <[email protected]>'), |
| 457 |
('Plural-Forms', 'nplurals=2; plural=(n != 1);'), |
| 458 |
('MIME-Version', '1.0'), |
| 459 |
('Content-Type', 'text/plain; charset=utf-8'), |
| 460 |
('Content-Transfer-Encoding', '8bit'), |
| 461 |
('Generated-By', f'Babel {catalog.VERSION}\n'), |
| 462 |
] |
| 463 |
|
| 464 |
|
| 465 |
def test_catalog_mime_headers_type_coercion(): |
| 466 |
""" |
| 467 |
Test that mime headers' keys and values are coerced to strings |
| 468 |
""" |
| 469 |
cat = catalog.Catalog(locale='de_DE', project='Foobar', version='1.0') |
| 470 |
# This is a strange interface in that it doesn't actually overwrite all |
| 471 |
# of the MIME headers, but just sets the ones that are passed in (and known). |
| 472 |
cat.mime_headers = {b'REPORT-MSGID-BUGS-TO': 8}.items() |
| 473 |
assert dict(cat.mime_headers)['Report-Msgid-Bugs-To'] == '8' |
| 474 |
|
| 475 |
|
| 476 |
def test_catalog_num_plurals(): |
| 477 |
assert catalog.Catalog(locale='en').num_plurals == 2 |
| 478 |
assert catalog.Catalog(locale='ga').num_plurals == 5 |
| 479 |
|
| 480 |
|
| 481 |
def test_catalog_plural_expr(): |
| 482 |
assert catalog.Catalog(locale='en').plural_expr == '(n != 1)' |
| 483 |
assert (catalog.Catalog(locale='ga').plural_expr |
| 484 |
== '(n == 1 ? 0 : n == 2 ? 1 : n >= 3 && n <= 6 ? 2 : n >= 7 && n <= 10 ? 3 : 4)') |
| 485 |
|
| 486 |
|
| 487 |
def test_catalog_plural_forms(): |
| 488 |
assert (catalog.Catalog(locale='en').plural_forms |
| 489 |
== 'nplurals=2; plural=(n != 1);') |
| 490 |
assert (catalog.Catalog(locale='pt_BR').plural_forms |
| 491 |
== 'nplurals=3; plural=(n == 0 || n == 1) ? 0 : n != 0 && n % 1000000 == 0 ? 1 : 2;') |
| 492 |
|
| 493 |
|
| 494 |
def test_catalog_setitem(): |
| 495 |
cat = catalog.Catalog() |
| 496 |
cat['foo'] = catalog.Message('foo') |
| 497 |
assert cat['foo'].id == 'foo' |
| 498 |
|
| 499 |
cat = catalog.Catalog() |
| 500 |
cat['foo'] = catalog.Message('foo', locations=[('main.py', 1)]) |
| 501 |
assert cat['foo'].locations == [('main.py', 1)] |
| 502 |
cat['foo'] = catalog.Message('foo', locations=[('utils.py', 5)]) |
| 503 |
assert cat['foo'].locations == [('main.py', 1), ('utils.py', 5)] |
| 504 |
|
| 505 |
|
| 506 |
def test_catalog_add(): |
| 507 |
cat = catalog.Catalog() |
| 508 |
foo = cat.add('foo') |
| 509 |
assert foo.id == 'foo' |
| 510 |
assert cat['foo'] is foo |
| 511 |
|
| 512 |
|
| 513 |
def test_catalog_update(): |
| 514 |
template = catalog.Catalog(header_comment="# A Custom Header") |
| 515 |
template.add('green', locations=[('main.py', 99)]) |
| 516 |
template.add('blue', locations=[('main.py', 100)]) |
| 517 |
template.add(('salad', 'salads'), locations=[('util.py', 42)]) |
| 518 |
cat = catalog.Catalog(locale='de_DE') |
| 519 |
cat.add('blue', 'blau', locations=[('main.py', 98)]) |
| 520 |
cat.add('head', 'Kopf', locations=[('util.py', 33)]) |
| 521 |
cat.add(('salad', 'salads'), ('Salat', 'Salate'), |
| 522 |
locations=[('util.py', 38)]) |
| 523 |
|
| 524 |
cat.update(template) |
| 525 |
assert len(cat) == 3 |
| 526 |
|
| 527 |
msg1 = cat['green'] |
| 528 |
assert not msg1.string |
| 529 |
assert msg1.locations == [('main.py', 99)] |
| 530 |
|
| 531 |
msg2 = cat['blue'] |
| 532 |
assert msg2.string == 'blau' |
| 533 |
assert msg2.locations == [('main.py', 100)] |
| 534 |
|
| 535 |
msg3 = cat['salad'] |
| 536 |
assert msg3.string == ('Salat', 'Salate') |
| 537 |
assert msg3.locations == [('util.py', 42)] |
| 538 |
|
| 539 |
assert 'head' not in cat |
| 540 |
assert list(cat.obsolete.values())[0].id == 'head' |
| 541 |
|
| 542 |
cat.update(template, update_header_comment=True) |
| 543 |
assert cat.header_comment == template.header_comment # Header comment also gets updated |
| 544 |
|
| 545 |
|
| 546 |
def test_datetime_parsing(): |
| 547 |
val1 = catalog._parse_datetime_header('2006-06-28 23:24+0200') |
| 548 |
assert val1.timetuple()[:5] == (2006, 6, 28, 23, 24) |
| 549 |
assert val1.utctimetuple()[:5] == (2006, 6, 28, 21, 24) |
| 550 |
assert val1.tzinfo.tzname(None) == 'Etc/GMT+120' |
| 551 |
assert val1 == datetime.datetime(2006, 6, 28, 21, 24, tzinfo=UTC) |
| 552 |
|
| 553 |
val2 = catalog._parse_datetime_header('2006-06-28 23:24') |
| 554 |
assert val2.year == 2006 |
| 555 |
assert val2.month == 6 |
| 556 |
assert val2.day == 28 |
| 557 |
assert val2.tzinfo is None |
| 558 |
|
| 559 |
|
| 560 |
def test_update_catalog_comments(): |
| 561 |
# Based on https://web.archive.org/web/20100710131029/http://babel.edgewall.org/attachment/ticket/163/cat-update-comments.py |
| 562 |
|
| 563 |
catalog = pofile.read_po(StringIO(''' |
| 564 |
# A user comment |
| 565 |
#. An auto comment |
| 566 |
#: main.py:1 |
| 567 |
#, fuzzy, python-format |
| 568 |
msgid "foo %(name)s" |
| 569 |
msgstr "foo %(name)s" |
| 570 |
''')) |
| 571 |
|
| 572 |
assert all(message.user_comments and message.auto_comments for message in catalog if message.id) |
| 573 |
|
| 574 |
# NOTE: in the POT file, there are no comments |
| 575 |
template = pofile.read_po(StringIO(''' |
| 576 |
#: main.py:1 |
| 577 |
#, fuzzy, python-format |
| 578 |
msgid "bar %(name)s" |
| 579 |
msgstr "" |
| 580 |
''')) |
| 581 |
|
| 582 |
catalog.update(template) |
| 583 |
|
| 584 |
# Auto comments will be obliterated here |
| 585 |
assert all(message.user_comments for message in catalog if message.id) |
| 586 |
|
| 587 |
|
| 588 |
def test_catalog_tz_pickleable(): |
| 589 |
""" |
| 590 |
Test that catalogs with timezoned times are pickleable. |
| 591 |
This would previously fail with `FixedOffsetTimezone.__init__() missing 1 required positional argument: 'offset'` |
| 592 |
when trying to load the pickled data. |
| 593 |
""" |
| 594 |
pickle.loads(pickle.dumps(pofile.read_po(StringIO(r""" |
| 595 |
msgid "" |
| 596 |
msgstr "" |
| 597 |
"POT-Creation-Date: 2007-04-01 15:30+0200\n" |
| 598 |
""")))) |