| 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 os |
| 14 |
from io import BytesIO |
| 15 |
|
| 16 |
from babel.messages import Catalog, mofile |
| 17 |
from babel.support import Translations |
| 18 |
|
| 19 |
data_dir = os.path.join(os.path.dirname(__file__), 'data') |
| 20 |
|
| 21 |
|
| 22 |
def test_basics(): |
| 23 |
mo_path = os.path.join(data_dir, 'project', 'i18n', 'de', |
| 24 |
'LC_MESSAGES', 'messages.mo') |
| 25 |
with open(mo_path, 'rb') as mo_file: |
| 26 |
catalog = mofile.read_mo(mo_file) |
| 27 |
assert len(catalog) == 2 |
| 28 |
assert catalog.project == 'TestProject' |
| 29 |
assert catalog.version == '0.1' |
| 30 |
assert catalog['bar'].string == 'Stange' |
| 31 |
assert catalog['foobar'].string == ['Fuhstange', 'Fuhstangen'] |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
def test_sorting(): |
| 36 |
# Ensure the header is sorted to the first entry so that its charset |
| 37 |
# can be applied to all subsequent messages by GNUTranslations |
| 38 |
# (ensuring all messages are safely converted to unicode) |
| 39 |
catalog = Catalog(locale='en_US') |
| 40 |
catalog.add('', '''\ |
| 41 |
"Content-Type: text/plain; charset=utf-8\n" |
| 42 |
"Content-Transfer-Encoding: 8bit\n''') |
| 43 |
catalog.add('foo', 'Voh') |
| 44 |
catalog.add(('There is', 'There are'), ('Es gibt', 'Es gibt')) |
| 45 |
catalog.add('Fizz', '') |
| 46 |
catalog.add(('Fuzz', 'Fuzzes'), ('', '')) |
| 47 |
buf = BytesIO() |
| 48 |
mofile.write_mo(buf, catalog) |
| 49 |
buf.seek(0) |
| 50 |
translations = Translations(fp=buf) |
| 51 |
assert translations.ugettext('foo') == 'Voh' |
| 52 |
assert translations.ungettext('There is', 'There are', 1) == 'Es gibt' |
| 53 |
assert translations.ugettext('Fizz') == 'Fizz' |
| 54 |
assert translations.ugettext('Fuzz') == 'Fuzz' |
| 55 |
assert translations.ugettext('Fuzzes') == 'Fuzzes' |
| 56 |
|
| 57 |
|
| 58 |
def test_more_plural_forms(): |
| 59 |
catalog2 = Catalog(locale='ru_RU') |
| 60 |
catalog2.add(('Fuzz', 'Fuzzes'), ('', '', '')) |
| 61 |
buf = BytesIO() |
| 62 |
mofile.write_mo(buf, catalog2) |
| 63 |
|
| 64 |
|
| 65 |
def test_empty_translation_with_fallback(): |
| 66 |
catalog1 = Catalog(locale='fr_FR') |
| 67 |
catalog1.add('', '''\ |
| 68 |
"Content-Type: text/plain; charset=utf-8\n" |
| 69 |
"Content-Transfer-Encoding: 8bit\n''') |
| 70 |
catalog1.add('Fuzz', '') |
| 71 |
buf1 = BytesIO() |
| 72 |
mofile.write_mo(buf1, catalog1) |
| 73 |
buf1.seek(0) |
| 74 |
catalog2 = Catalog(locale='fr') |
| 75 |
catalog2.add('', '''\ |
| 76 |
"Content-Type: text/plain; charset=utf-8\n" |
| 77 |
"Content-Transfer-Encoding: 8bit\n''') |
| 78 |
catalog2.add('Fuzz', 'Flou') |
| 79 |
buf2 = BytesIO() |
| 80 |
mofile.write_mo(buf2, catalog2) |
| 81 |
buf2.seek(0) |
| 82 |
|
| 83 |
translations = Translations(fp=buf1) |
| 84 |
translations.add_fallback(Translations(fp=buf2)) |
| 85 |
|
| 86 |
assert translations.ugettext('Fuzz') == 'Flou' |
| 87 |
|
| 88 |
|
| 89 |
def test_read_mo_decodes_message_context() -> None: |
| 90 |
catalog = Catalog(locale='fi_FI') |
| 91 |
catalog.add('', '''\ |
| 92 |
"Content-Type: text/plain; charset=utf-8\n" |
| 93 |
"Content-Transfer-Encoding: 8bit\n''') |
| 94 |
catalog.add('x', 'åäöÅÄÖ', context='ほげ') |
| 95 |
buf = BytesIO() |
| 96 |
mofile.write_mo(buf, catalog) |
| 97 |
buf.seek(0) |
| 98 |
del catalog |
| 99 |
|
| 100 |
catalog = mofile.read_mo(buf) |
| 101 |
message = catalog.get('x', context='ほげ') |
| 102 |
assert message is not None |
| 103 |
assert message.id == 'x' |
| 104 |
assert message.context == 'ほげ' |
| 105 |
assert message.string == 'åäöÅÄÖ' |
| 106 |
assert not catalog.get('x') # Not without the context |