| 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 |
import pytest |
| 17 |
|
| 18 |
from babel import __version__ as VERSION |
| 19 |
from babel.core import Locale, UnknownLocaleError |
| 20 |
from babel.dates import format_datetime |
| 21 |
from babel.messages import Message, checkers |
| 22 |
from babel.messages.catalog import TranslationError |
| 23 |
from babel.messages.checkers import _validate_format, python_format |
| 24 |
from babel.messages.plurals import PLURALS |
| 25 |
from babel.messages.pofile import read_po |
| 26 |
from babel.util import LOCALTZ |
| 27 |
|
| 28 |
# the last msgstr[idx] is always missing except for singular plural forms |
| 29 |
|
| 30 |
|
| 31 |
def test_1_num_plurals_checkers(): |
| 32 |
for _locale in [p for p in PLURALS if PLURALS[p][0] == 1]: |
| 33 |
try: |
| 34 |
locale = Locale.parse(_locale) |
| 35 |
except UnknownLocaleError: |
| 36 |
# Just an alias? Not what we're testing here, let's continue |
| 37 |
continue |
| 38 |
date = format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale=_locale) |
| 39 |
plural = PLURALS[_locale][0] |
| 40 |
po_file = (f"""\ |
| 41 |
# {locale.english_name} translations for TestProject. |
| 42 |
# Copyright (C) 2007 FooBar, Inc. |
| 43 |
# This file is distributed under the same license as the TestProject |
| 44 |
# project. |
| 45 |
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007. |
| 46 |
# |
| 47 |
msgid "" |
| 48 |
msgstr "" |
| 49 |
"Project-Id-Version: TestProject 0.1\\n" |
| 50 |
"Report-Msgid-Bugs-To: [email protected]\\n" |
| 51 |
"POT-Creation-Date: 2007-04-01 15:30+0200\\n" |
| 52 |
"PO-Revision-Date: {date}\\n" |
| 53 |
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n" |
| 54 |
"Language-Team: {_locale} <[email protected]>\n" |
| 55 |
"Plural-Forms: nplurals={plural}; plural={plural};\\n" |
| 56 |
"MIME-Version: 1.0\\n" |
| 57 |
"Content-Type: text/plain; charset=utf-8\\n" |
| 58 |
"Content-Transfer-Encoding: 8bit\\n" |
| 59 |
"Generated-By: Babel {VERSION}\\n" |
| 60 |
|
| 61 |
#. This will be a translator comment, |
| 62 |
#. that will include several lines |
| 63 |
#: project/file1.py:8 |
| 64 |
msgid "bar" |
| 65 |
msgstr "" |
| 66 |
|
| 67 |
#: project/file2.py:9 |
| 68 |
msgid "foobar" |
| 69 |
msgid_plural "foobars" |
| 70 |
msgstr[0] "" |
| 71 |
|
| 72 |
""").encode('utf-8') |
| 73 |
|
| 74 |
# This test will fail for revisions <= 406 because so far |
| 75 |
# catalog.num_plurals was neglected |
| 76 |
catalog = read_po(BytesIO(po_file), _locale) |
| 77 |
message = catalog['foobar'] |
| 78 |
checkers.num_plurals(catalog, message) |
| 79 |
|
| 80 |
|
| 81 |
def test_2_num_plurals_checkers(): |
| 82 |
# in this testcase we add an extra msgstr[idx], we should be |
| 83 |
# disregarding it |
| 84 |
for _locale in [p for p in PLURALS if PLURALS[p][0] == 2]: |
| 85 |
if _locale in ['nn', 'no']: |
| 86 |
_locale = 'nn_NO' |
| 87 |
num_plurals = PLURALS[_locale.split('_')[0]][0] |
| 88 |
plural_expr = PLURALS[_locale.split('_')[0]][1] |
| 89 |
else: |
| 90 |
num_plurals = PLURALS[_locale][0] |
| 91 |
plural_expr = PLURALS[_locale][1] |
| 92 |
try: |
| 93 |
locale = Locale(_locale) |
| 94 |
date = format_datetime(datetime.now(LOCALTZ), |
| 95 |
'yyyy-MM-dd HH:mmZ', |
| 96 |
tzinfo=LOCALTZ, locale=_locale) |
| 97 |
except UnknownLocaleError: |
| 98 |
# Just an alias? Not what we're testing here, let's continue |
| 99 |
continue |
| 100 |
po_file = f"""\ |
| 101 |
# {locale.english_name} translations for TestProject. |
| 102 |
# Copyright (C) 2007 FooBar, Inc. |
| 103 |
# This file is distributed under the same license as the TestProject |
| 104 |
# project. |
| 105 |
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007. |
| 106 |
# |
| 107 |
msgid "" |
| 108 |
msgstr "" |
| 109 |
"Project-Id-Version: TestProject 0.1\\n" |
| 110 |
"Report-Msgid-Bugs-To: [email protected]\\n" |
| 111 |
"POT-Creation-Date: 2007-04-01 15:30+0200\\n" |
| 112 |
"PO-Revision-Date: {date}\\n" |
| 113 |
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n" |
| 114 |
"Language-Team: {_locale} <[email protected]>\\n" |
| 115 |
"Plural-Forms: nplurals={num_plurals}; plural={plural_expr};\\n" |
| 116 |
"MIME-Version: 1.0\\n" |
| 117 |
"Content-Type: text/plain; charset=utf-8\\n" |
| 118 |
"Content-Transfer-Encoding: 8bit\\n" |
| 119 |
"Generated-By: Babel {VERSION}\\n" |
| 120 |
|
| 121 |
#. This will be a translator comment, |
| 122 |
#. that will include several lines |
| 123 |
#: project/file1.py:8 |
| 124 |
msgid "bar" |
| 125 |
msgstr "" |
| 126 |
|
| 127 |
#: project/file2.py:9 |
| 128 |
msgid "foobar" |
| 129 |
msgid_plural "foobars" |
| 130 |
msgstr[0] "" |
| 131 |
msgstr[1] "" |
| 132 |
msgstr[2] "" |
| 133 |
|
| 134 |
""".encode('utf-8') |
| 135 |
# we should be adding the missing msgstr[0] |
| 136 |
|
| 137 |
# This test will fail for revisions <= 406 because so far |
| 138 |
# catalog.num_plurals was neglected |
| 139 |
catalog = read_po(BytesIO(po_file), _locale) |
| 140 |
message = catalog['foobar'] |
| 141 |
checkers.num_plurals(catalog, message) |
| 142 |
|
| 143 |
|
| 144 |
def test_3_num_plurals_checkers(): |
| 145 |
for _locale in [p for p in PLURALS if PLURALS[p][0] == 3]: |
| 146 |
plural = format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale=_locale) |
| 147 |
english_name = Locale.parse(_locale).english_name |
| 148 |
po_file = fr"""\ |
| 149 |
# {english_name} translations for TestProject. |
| 150 |
# Copyright (C) 2007 FooBar, Inc. |
| 151 |
# This file is distributed under the same license as the TestProject |
| 152 |
# project. |
| 153 |
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007. |
| 154 |
# |
| 155 |
msgid "" |
| 156 |
msgstr "" |
| 157 |
"Project-Id-Version: TestProject 0.1\n" |
| 158 |
"Report-Msgid-Bugs-To: [email protected]\n" |
| 159 |
"POT-Creation-Date: 2007-04-01 15:30+0200\n" |
| 160 |
"PO-Revision-Date: {plural}\n" |
| 161 |
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" |
| 162 |
"Language-Team: {_locale} <[email protected]>\n" |
| 163 |
"Plural-Forms: nplurals={PLURALS[_locale][0]}; plural={PLURALS[_locale][0]};\n" |
| 164 |
"MIME-Version: 1.0\n" |
| 165 |
"Content-Type: text/plain; charset=utf-8\n" |
| 166 |
"Content-Transfer-Encoding: 8bit\n" |
| 167 |
"Generated-By: Babel {VERSION}\n" |
| 168 |
|
| 169 |
#. This will be a translator comment, |
| 170 |
#. that will include several lines |
| 171 |
#: project/file1.py:8 |
| 172 |
msgid "bar" |
| 173 |
msgstr "" |
| 174 |
|
| 175 |
#: project/file2.py:9 |
| 176 |
msgid "foobar" |
| 177 |
msgid_plural "foobars" |
| 178 |
msgstr[0] "" |
| 179 |
msgstr[1] "" |
| 180 |
|
| 181 |
""".encode('utf-8') |
| 182 |
|
| 183 |
# This test will fail for revisions <= 406 because so far |
| 184 |
# catalog.num_plurals was neglected |
| 185 |
catalog = read_po(BytesIO(po_file), _locale) |
| 186 |
message = catalog['foobar'] |
| 187 |
checkers.num_plurals(catalog, message) |
| 188 |
|
| 189 |
|
| 190 |
def test_4_num_plurals_checkers(): |
| 191 |
for _locale in [p for p in PLURALS if PLURALS[p][0] == 4]: |
| 192 |
date = format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale=_locale) |
| 193 |
english_name = Locale.parse(_locale).english_name |
| 194 |
plural = PLURALS[_locale][0] |
| 195 |
po_file = fr"""\ |
| 196 |
# {english_name} translations for TestProject. |
| 197 |
# Copyright (C) 2007 FooBar, Inc. |
| 198 |
# This file is distributed under the same license as the TestProject |
| 199 |
# project. |
| 200 |
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007. |
| 201 |
# |
| 202 |
msgid "" |
| 203 |
msgstr "" |
| 204 |
"Project-Id-Version: TestProject 0.1\n" |
| 205 |
"Report-Msgid-Bugs-To: [email protected]\n" |
| 206 |
"POT-Creation-Date: 2007-04-01 15:30+0200\n" |
| 207 |
"PO-Revision-Date: {date}\n" |
| 208 |
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" |
| 209 |
"Language-Team: {_locale} <[email protected]>\n" |
| 210 |
"Plural-Forms: nplurals={plural}; plural={plural};\n" |
| 211 |
"MIME-Version: 1.0\n" |
| 212 |
"Content-Type: text/plain; charset=utf-8\n" |
| 213 |
"Content-Transfer-Encoding: 8bit\n" |
| 214 |
"Generated-By: Babel {VERSION}\n" |
| 215 |
|
| 216 |
#. This will be a translator comment, |
| 217 |
#. that will include several lines |
| 218 |
#: project/file1.py:8 |
| 219 |
msgid "bar" |
| 220 |
msgstr "" |
| 221 |
|
| 222 |
#: project/file2.py:9 |
| 223 |
msgid "foobar" |
| 224 |
msgid_plural "foobars" |
| 225 |
msgstr[0] "" |
| 226 |
msgstr[1] "" |
| 227 |
msgstr[2] "" |
| 228 |
|
| 229 |
""".encode('utf-8') |
| 230 |
|
| 231 |
# This test will fail for revisions <= 406 because so far |
| 232 |
# catalog.num_plurals was neglected |
| 233 |
catalog = read_po(BytesIO(po_file), _locale) |
| 234 |
message = catalog['foobar'] |
| 235 |
checkers.num_plurals(catalog, message) |
| 236 |
|
| 237 |
|
| 238 |
def test_5_num_plurals_checkers(): |
| 239 |
for _locale in [p for p in PLURALS if PLURALS[p][0] == 5]: |
| 240 |
date = format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale=_locale) |
| 241 |
english_name = Locale.parse(_locale).english_name |
| 242 |
plural = PLURALS[_locale][0] |
| 243 |
po_file = fr"""\ |
| 244 |
# {english_name} translations for TestProject. |
| 245 |
# Copyright (C) 2007 FooBar, Inc. |
| 246 |
# This file is distributed under the same license as the TestProject |
| 247 |
# project. |
| 248 |
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007. |
| 249 |
# |
| 250 |
msgid "" |
| 251 |
msgstr "" |
| 252 |
"Project-Id-Version: TestProject 0.1\n" |
| 253 |
"Report-Msgid-Bugs-To: [email protected]\n" |
| 254 |
"POT-Creation-Date: 2007-04-01 15:30+0200\n" |
| 255 |
"PO-Revision-Date: {date}\n" |
| 256 |
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" |
| 257 |
"Language-Team: {_locale} <[email protected]>\n" |
| 258 |
"Plural-Forms: nplurals={plural}; plural={plural};\n" |
| 259 |
"MIME-Version: 1.0\n" |
| 260 |
"Content-Type: text/plain; charset=utf-8\n" |
| 261 |
"Content-Transfer-Encoding: 8bit\n" |
| 262 |
"Generated-By: Babel {VERSION}\n" |
| 263 |
|
| 264 |
#. This will be a translator comment, |
| 265 |
#. that will include several lines |
| 266 |
#: project/file1.py:8 |
| 267 |
msgid "bar" |
| 268 |
msgstr "" |
| 269 |
|
| 270 |
#: project/file2.py:9 |
| 271 |
msgid "foobar" |
| 272 |
msgid_plural "foobars" |
| 273 |
msgstr[0] "" |
| 274 |
msgstr[1] "" |
| 275 |
msgstr[2] "" |
| 276 |
msgstr[3] "" |
| 277 |
|
| 278 |
""".encode('utf-8') |
| 279 |
|
| 280 |
# This test will fail for revisions <= 406 because so far |
| 281 |
# catalog.num_plurals was neglected |
| 282 |
catalog = read_po(BytesIO(po_file), _locale) |
| 283 |
message = catalog['foobar'] |
| 284 |
checkers.num_plurals(catalog, message) |
| 285 |
|
| 286 |
|
| 287 |
def test_6_num_plurals_checkers(): |
| 288 |
for _locale in [p for p in PLURALS if PLURALS[p][0] == 6]: |
| 289 |
english_name = Locale.parse(_locale).english_name |
| 290 |
date = format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale=_locale) |
| 291 |
plural = PLURALS[_locale][0] |
| 292 |
po_file = fr"""\ |
| 293 |
# {english_name} translations for TestProject. |
| 294 |
# Copyright (C) 2007 FooBar, Inc. |
| 295 |
# This file is distributed under the same license as the TestProject |
| 296 |
# project. |
| 297 |
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007. |
| 298 |
# |
| 299 |
msgid "" |
| 300 |
msgstr "" |
| 301 |
"Project-Id-Version: TestProject 0.1\n" |
| 302 |
"Report-Msgid-Bugs-To: [email protected]\n" |
| 303 |
"POT-Creation-Date: 2007-04-01 15:30+0200\n" |
| 304 |
"PO-Revision-Date: {date}\n" |
| 305 |
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" |
| 306 |
"Language-Team: {_locale} <[email protected]>\n" |
| 307 |
"Plural-Forms: nplurals={plural}; plural={plural};\n" |
| 308 |
"MIME-Version: 1.0\n" |
| 309 |
"Content-Type: text/plain; charset=utf-8\n" |
| 310 |
"Content-Transfer-Encoding: 8bit\n" |
| 311 |
"Generated-By: Babel {VERSION}\n" |
| 312 |
|
| 313 |
#. This will be a translator comment, |
| 314 |
#. that will include several lines |
| 315 |
#: project/file1.py:8 |
| 316 |
msgid "bar" |
| 317 |
msgstr "" |
| 318 |
|
| 319 |
#: project/file2.py:9 |
| 320 |
msgid "foobar" |
| 321 |
msgid_plural "foobars" |
| 322 |
msgstr[0] "" |
| 323 |
msgstr[1] "" |
| 324 |
msgstr[2] "" |
| 325 |
msgstr[3] "" |
| 326 |
msgstr[4] "" |
| 327 |
|
| 328 |
""".encode('utf-8') |
| 329 |
|
| 330 |
# This test will fail for revisions <= 406 because so far |
| 331 |
# catalog.num_plurals was neglected |
| 332 |
catalog = read_po(BytesIO(po_file), _locale) |
| 333 |
message = catalog['foobar'] |
| 334 |
checkers.num_plurals(catalog, message) |
| 335 |
|
| 336 |
|
| 337 |
@pytest.mark.parametrize(('msgid', 'msgstr'), [ |
| 338 |
('foo %s', 'foo'), |
| 339 |
(('foo %s', 'bar'), ('foo', 'bar')), |
| 340 |
(('foo', 'bar %s'), ('foo', 'bar')), |
| 341 |
(('foo %s', 'bar'), ('foo')), |
| 342 |
(('foo %s', 'bar %d'), ('foo %s', 'bar %d', 'baz')), |
| 343 |
(('foo %s', 'bar %d'), ('foo %s', 'bar %d', 'baz %d', 'qux')), |
| 344 |
]) |
| 345 |
def test_python_format_invalid(msgid, msgstr): |
| 346 |
msg = Message(msgid, msgstr) |
| 347 |
with pytest.raises(TranslationError): |
| 348 |
python_format(None, msg) |
| 349 |
|
| 350 |
|
| 351 |
@pytest.mark.parametrize(('msgid', 'msgstr'), [ |
| 352 |
('foo', 'foo'), |
| 353 |
('foo', 'foo %s'), |
| 354 |
('foo %s', ''), |
| 355 |
(('foo %s', 'bar %d'), ('foo %s', 'bar %d')), |
| 356 |
(('foo %s', 'bar %d'), ('foo %s', 'bar %d', 'baz %d')), |
| 357 |
(('foo', 'bar %s'), ('foo')), |
| 358 |
(('foo', 'bar %s'), ('', '')), |
| 359 |
(('foo', 'bar %s'), ('foo', '')), |
| 360 |
(('foo %s', 'bar %d'), ('foo %s', '')), |
| 361 |
]) |
| 362 |
def test_python_format_valid(msgid, msgstr): |
| 363 |
msg = Message(msgid, msgstr) |
| 364 |
python_format(None, msg) |
| 365 |
|
| 366 |
|
| 367 |
@pytest.mark.parametrize(('msgid', 'msgstr', 'error'), [ |
| 368 |
('%s %(foo)s', '%s %(foo)s', 'format string mixes positional and named placeholders'), |
| 369 |
('foo %s', 'foo', 'placeholders are incompatible'), |
| 370 |
('%s', '%(foo)s', 'the format strings are of different kinds'), |
| 371 |
('%s', '%s %d', 'positional format placeholders are unbalanced'), |
| 372 |
('%s', '%d', "incompatible format for placeholder 1: 's' and 'd' are not compatible"), |
| 373 |
('%s %s %d', '%s %s %s', "incompatible format for placeholder 3: 'd' and 's' are not compatible"), |
| 374 |
('%(foo)s', '%(bar)s', "unknown named placeholder 'bar'"), |
| 375 |
('%(foo)s', '%(bar)d', "unknown named placeholder 'bar'"), |
| 376 |
('%(foo)s', '%(foo)d', "incompatible format for placeholder 'foo': 'd' and 's' are not compatible"), |
| 377 |
]) |
| 378 |
def test__validate_format_invalid(msgid, msgstr, error): |
| 379 |
with pytest.raises(TranslationError, match=error): |
| 380 |
_validate_format(msgid, msgstr) |
| 381 |
|
| 382 |
|
| 383 |
@pytest.mark.parametrize(('msgid', 'msgstr'), [ |
| 384 |
('foo', 'foo'), |
| 385 |
('foo', 'foo %s'), |
| 386 |
('%s foo', 'foo %s'), |
| 387 |
('%i', '%d'), |
| 388 |
('%d', '%u'), |
| 389 |
('%x', '%X'), |
| 390 |
('%f', '%F'), |
| 391 |
('%F', '%g'), |
| 392 |
('%g', '%G'), |
| 393 |
('%(foo)s', 'foo'), |
| 394 |
('%(foo)s', '%(foo)s %(foo)s'), |
| 395 |
('%(bar)s foo %(n)d', '%(n)d foo %(bar)s'), |
| 396 |
]) |
| 397 |
def test__validate_format_valid(msgid, msgstr): |
| 398 |
_validate_format(msgid, msgstr) |