| 1 |
# vim: ts=4:sw=4:expandtab |
| 2 |
|
| 3 |
# BleachBit |
| 4 |
# Copyright (C) 2008-2025 Andrew Ziem |
| 5 |
# https://www.bleachbit.org |
| 6 |
# |
| 7 |
# This program is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
|
| 21 |
import locale |
| 22 |
import unittest |
| 23 |
from unittest import mock |
| 24 |
|
| 25 |
from bleachbit.Language import get_active_language_code, \ |
| 26 |
get_supported_language_codes, \ |
| 27 |
get_text, \ |
| 28 |
setup_translation, \ |
| 29 |
get_supported_language_code_name_dict |
| 30 |
from bleachbit.Options import options |
| 31 |
from tests import common |
| 32 |
|
| 33 |
|
| 34 |
def skipIfMissingPo(f): |
| 35 |
"""Skip unit test if missing translations""" |
| 36 |
lang_count = len(get_supported_language_codes()) |
| 37 |
return unittest.skipIf(lang_count < 3, 'missing translations: run `make -C po local`')(f) |
| 38 |
|
| 39 |
|
| 40 |
class LanguageTestCase(common.BleachbitTestCase): |
| 41 |
|
| 42 |
"""Test case for module Language""" |
| 43 |
|
| 44 |
def setUp(self): |
| 45 |
super().setUp() |
| 46 |
options.set('auto_detect_lang', False) |
| 47 |
options.set('forced_language', '') |
| 48 |
|
| 49 |
def tearDown(self): |
| 50 |
super().tearDown() |
| 51 |
# reset |
| 52 |
options.set('forced_language', 'en') |
| 53 |
setup_translation() |
| 54 |
|
| 55 |
def test_get_active_language_code(self): |
| 56 |
"""Test get_active_language_code()""" |
| 57 |
lang_id = get_active_language_code() |
| 58 |
self.assertIsLanguageCode(lang_id) |
| 59 |
|
| 60 |
def test_get_supported_language_codes(self): |
| 61 |
"""Test get_supported_language_codes()""" |
| 62 |
slangs = get_supported_language_codes() |
| 63 |
self.assertTrue(isinstance(slangs, list)) |
| 64 |
self.assertGreater(len(slangs), 1) |
| 65 |
for slang in slangs: |
| 66 |
if slang in common.SKIP_ALIAS_CODES: |
| 67 |
continue |
| 68 |
self.assertIsLanguageCode(slang) |
| 69 |
self.assertIn('en_US', slangs) |
| 70 |
if len(get_supported_language_codes()) < 3: |
| 71 |
self.skipTest('missing translations') |
| 72 |
self.assertIn('es', slangs) |
| 73 |
|
| 74 |
def test_get_supported_language_code_name_dict_unknown_code(self): |
| 75 |
with mock.patch('bleachbit.Language.get_supported_language_codes', return_value=['en', 'es', 'foo@bar']): |
| 76 |
supported_langs = get_supported_language_code_name_dict() |
| 77 |
self.assertIn('en', supported_langs) |
| 78 |
self.assertIn('es', supported_langs) |
| 79 |
self.assertIn('foo@bar', supported_langs) |
| 80 |
|
| 81 |
@skipIfMissingPo |
| 82 |
def test_switch_language_twice(self): |
| 83 |
"""English should still work after switching twice""" |
| 84 |
|
| 85 |
options.set('auto_detect_lang', True) |
| 86 |
options.set('forced_language', '') |
| 87 |
setup_translation() |
| 88 |
self.assertEqual(get_text('Preview'), 'Preview') |
| 89 |
|
| 90 |
options.set('auto_detect_lang', False) |
| 91 |
options.set('forced_language', 'es') |
| 92 |
setup_translation() |
| 93 |
self.assertIn(get_text('Preview'), ('Vista previa', 'Previsualizar')) |
| 94 |
|
| 95 |
options.set('forced_language', 'en') |
| 96 |
setup_translation() |
| 97 |
self.assertEqual(get_text('Preview'), 'Preview') |
| 98 |
|
| 99 |
def test_get_text_all(self): |
| 100 |
"""Test get_text across all languages""" |
| 101 |
for lang_id in get_supported_language_codes(): |
| 102 |
if lang_id in common.SKIP_ALIAS_CODES: |
| 103 |
continue |
| 104 |
options.set('forced_language', lang_id) |
| 105 |
self.assertEqual(get_active_language_code(), lang_id) |
| 106 |
setup_translation() |
| 107 |
text = get_text('Preview') |
| 108 |
self.assertIsInstance(text, str) |
| 109 |
self.assertGreater(len(text), 0) |
| 110 |
|
| 111 |
@skipIfMissingPo |
| 112 |
def test_get_text_au(self): |
| 113 |
"""Test Australian English |
| 114 |
|
| 115 |
It should not get confused with American English. |
| 116 |
""" |
| 117 |
options.set('forced_language', 'en_AU') |
| 118 |
self.assertEqual(get_active_language_code(), 'en_AU') |
| 119 |
setup_translation() |
| 120 |
self.assertEqual(get_text('Localizations'), 'Localisations') |
| 121 |
|
| 122 |
@skipIfMissingPo |
| 123 |
def test_get_text_fallback(self): |
| 124 |
"""Language code x_Y should fall back to x""" |
| 125 |
for lang_id in ('es', 'es_XX', 'es_ES', 'es_ES.UTF-8', 'es_1235'): |
| 126 |
options.set('forced_language', lang_id) |
| 127 |
self.assertEqual(get_active_language_code(), lang_id) |
| 128 |
setup_translation() |
| 129 |
self.assertIn(get_text('Preview'), |
| 130 |
('Vista previa', 'Previsualizar')) |
| 131 |
|
| 132 |
def test_options_import_failure(self): |
| 133 |
"""Test handling of failed Options import in language detection""" |
| 134 |
with mock.patch.dict('sys.modules', {'bleachbit.Options': None}): |
| 135 |
with self.assertLogs(level='ERROR') as log_context: |
| 136 |
result = get_active_language_code() |
| 137 |
self.assertIn("Failed to get language options", |
| 138 |
log_context.output[0]) |
| 139 |
|
| 140 |
self.assertIn(result, [locale.getlocale()[0], 'C', 'en', 'en_US']) |