| 1 |
# CherryPy |
| 2 |
# Copyright (C) 2026 IKUS Software |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
import gettext |
| 18 |
import importlib.resources |
| 19 |
import unittest |
| 20 |
from datetime import datetime, timezone |
| 21 |
|
| 22 |
import cherrypy |
| 23 |
from cherrypy import _cpconfig |
| 24 |
from cherrypy.test import helper |
| 25 |
|
| 26 |
from .. import i18n |
| 27 |
|
| 28 |
TEXT_EN = 'Some text to translate' |
| 29 |
TEXT_FR = 'Du texte à traduire' |
| 30 |
|
| 31 |
|
| 32 |
class TestI18n(unittest.TestCase): |
| 33 |
def setUp(self): |
| 34 |
self.mo_dir = importlib.resources.files(__package__) / 'locales' |
| 35 |
self.assertTrue(self.mo_dir.is_dir()) |
| 36 |
cherrypy.request.config = _cpconfig.Config() |
| 37 |
|
| 38 |
def test_search_translation_en(self): |
| 39 |
# Load default translation return translation |
| 40 |
t = i18n._search_translation(self.mo_dir, 'messages', 'en') |
| 41 |
self.assertEqual("en", t.locale.language) |
| 42 |
# Test translation object |
| 43 |
self.assertEqual(TEXT_EN, t.gettext(TEXT_EN)) |
| 44 |
|
| 45 |
def test_search_translation_fr(self): |
| 46 |
# Custom lang return translation. |
| 47 |
t = i18n._search_translation(self.mo_dir, 'messages', 'fr') |
| 48 |
self.assertIsInstance(t, gettext.GNUTranslations) |
| 49 |
self.assertEqual("fr", t.locale.language) |
| 50 |
# Test translation object |
| 51 |
self.assertEqual(TEXT_FR, t.gettext(TEXT_EN)) |
| 52 |
|
| 53 |
def test_search_translation_invalid(self): |
| 54 |
# Load invalid translation return None |
| 55 |
t = i18n._search_translation(self.mo_dir, 'messages', 'tr') |
| 56 |
# Return Null translations. |
| 57 |
self.assertIn('NullTranslations', str(t.__class__)) |
| 58 |
|
| 59 |
|
| 60 |
class Root: |
| 61 |
|
| 62 |
@cherrypy.expose |
| 63 |
@cherrypy.tools.i18n(on=True) |
| 64 |
def index(self): |
| 65 |
return i18n.gettext(TEXT_EN) |
| 66 |
|
| 67 |
|
| 68 |
class AbstractI18nTest(helper.CPWebCase): |
| 69 |
default_lang = None |
| 70 |
interactive = False |
| 71 |
|
| 72 |
@classmethod |
| 73 |
def setup_server(cls): |
| 74 |
cherrypy.config.update( |
| 75 |
{ |
| 76 |
'tools.i18n.default': cls.default_lang, |
| 77 |
'tools.i18n.mo_dir': importlib.resources.files(__package__) / 'locales', |
| 78 |
'tools.i18n.domain': 'messages', |
| 79 |
'tools.i18n.cookie_name': 'locale', |
| 80 |
} |
| 81 |
) |
| 82 |
cherrypy.tree.mount(Root(), '/') |
| 83 |
|
| 84 |
|
| 85 |
class TestI18nWebCase(AbstractI18nTest): |
| 86 |
|
| 87 |
def test_language_with_invalid(self): |
| 88 |
# Query the page without login-in |
| 89 |
self.getPage("/", headers=[("Accept-Language", "invalid")]) |
| 90 |
self.assertStatus('200 OK') |
| 91 |
self.assertHeaderItemValue("Content-Language", "en") |
| 92 |
self.assertInBody(TEXT_EN) |
| 93 |
|
| 94 |
def test_language_with_unknown(self): |
| 95 |
# Query the page without login-in |
| 96 |
self.getPage("/", headers=[("Accept-Language", "it")]) |
| 97 |
self.assertStatus('200 OK') |
| 98 |
self.assertHeaderItemValue("Content-Language", "en") |
| 99 |
self.assertInBody(TEXT_EN) |
| 100 |
|
| 101 |
def test_language_en(self): |
| 102 |
self.getPage("/", headers=[("Accept-Language", "en-US,en;q=0.8")]) |
| 103 |
self.assertStatus('200 OK') |
| 104 |
self.assertHeaderItemValue("Content-Language", "en-US") |
| 105 |
self.assertInBody(TEXT_EN) |
| 106 |
|
| 107 |
def test_language_en_fr(self): |
| 108 |
self.getPage("/", headers=[("Accept-Language", "en-US,en;q=0.8,fr-CA;q=0.8")]) |
| 109 |
self.assertStatus('200 OK') |
| 110 |
self.assertHeaderItemValue("Content-Language", "en-US") |
| 111 |
self.assertInBody(TEXT_EN) |
| 112 |
|
| 113 |
def test_language_fr(self): |
| 114 |
self.getPage("/") |
| 115 |
self.assertInBody(TEXT_EN) |
| 116 |
self.getPage("/", headers=[("Accept-Language", "fr-CA;q=0.8,fr;q=0.6")]) |
| 117 |
self.assertStatus('200 OK') |
| 118 |
self.assertHeaderItemValue("Content-Language", "fr-CA") |
| 119 |
self.assertInBody(TEXT_FR) |
| 120 |
|
| 121 |
def test_language_en_US_POSIX(self): |
| 122 |
# When calling with locale variant |
| 123 |
self.getPage("/", headers=[("Accept-Language", "en-US-POSIX")]) |
| 124 |
self.assertStatus('200 OK') |
| 125 |
# Tehn page return en-US |
| 126 |
self.assertHeaderItemValue("Content-Language", "en-US") |
| 127 |
|
| 128 |
def test_cookie_fr(self): |
| 129 |
# When calling with locale variant |
| 130 |
self.getPage("/", headers=[("Accept-Language", "en-US"), ("Cookie", "locale=fr")]) |
| 131 |
self.assertStatus('200 OK') |
| 132 |
# Tehn page return en-US |
| 133 |
self.assertHeaderItemValue("Content-Language", "fr") |
| 134 |
|
| 135 |
def test_with_preferred_lang(self): |
| 136 |
# Given a default lang 'en' |
| 137 |
date = datetime.fromtimestamp(1680111611, timezone.utc) |
| 138 |
self.assertEqual(TEXT_EN, i18n.ugettext(TEXT_EN)) |
| 139 |
self.assertIn('March', i18n.format_datetime(date, format='long')) |
| 140 |
# When using preferred_lang with french |
| 141 |
with i18n.preferred_lang('fr'): |
| 142 |
# Then french translation is used |
| 143 |
self.assertEqual(TEXT_FR, i18n.ugettext(TEXT_EN)) |
| 144 |
# Then date time formating used french locale |
| 145 |
self.assertIn('mars', i18n.format_datetime(date, format='long')) |
| 146 |
# Then ouside the block, settings goest back to english |
| 147 |
self.assertEqual(TEXT_EN, i18n.ugettext(TEXT_EN)) |
| 148 |
self.assertIn('March', i18n.format_datetime(date, format='long')) |
| 149 |
|
| 150 |
def test_format_datetime_locales(self): |
| 151 |
date = datetime.fromtimestamp(1680111611, timezone.utc) |
| 152 |
with i18n.preferred_timezone('utc'): |
| 153 |
with i18n.preferred_lang('fr'): |
| 154 |
self.assertEqual('29 mars 2023, 17:40:11 TU', i18n.format_datetime(date, format='long')) |
| 155 |
with i18n.preferred_lang('en'): |
| 156 |
self.assertEqual('March 29, 2023, 5:40:11\u202fPM UTC', i18n.format_datetime(date, format='long')) |
| 157 |
with i18n.preferred_lang('en_US'): |
| 158 |
self.assertEqual('March 29, 2023, 5:40:11\u202fPM UTC', i18n.format_datetime(date, format='long')) |
| 159 |
with i18n.preferred_lang('en_GB'): |
| 160 |
self.assertEqual('29 March 2023, 17:40:11 UTC', i18n.format_datetime(date, format='long')) |
| 161 |
with i18n.preferred_lang('en_CH'): |
| 162 |
self.assertEqual('29 March 2023, 17:40:11 UTC', i18n.format_datetime(date, format='long')) |
| 163 |
with i18n.preferred_lang('de'): |
| 164 |
self.assertEqual('29. März 2023, 17:40:11 UTC', i18n.format_datetime(date, format='long')) |
| 165 |
with i18n.preferred_lang('de_CH'): |
| 166 |
self.assertEqual('29. März 2023, 17:40:11 UTC', i18n.format_datetime(date, format='long')) |
| 167 |
|
| 168 |
with i18n.preferred_timezone('Europe/Paris'): |
| 169 |
with i18n.preferred_lang('fr'): |
| 170 |
self.assertEqual('29 mars 2023, 19:40:11 +0200', i18n.format_datetime(date, format='long')) |
| 171 |
with i18n.preferred_lang('fr_CH'): |
| 172 |
self.assertEqual('29 mars 2023, 19:40:11 +0200', i18n.format_datetime(date, format='long')) |
| 173 |
with i18n.preferred_lang('en'): |
| 174 |
self.assertEqual('March 29, 2023, 7:40:11\u202fPM +0200', i18n.format_datetime(date, format='long')) |
| 175 |
with i18n.preferred_lang('en_US'): |
| 176 |
self.assertEqual('March 29, 2023, 7:40:11\u202fPM +0200', i18n.format_datetime(date, format='long')) |
| 177 |
with i18n.preferred_lang('en_GB'): |
| 178 |
self.assertEqual('29 March 2023, 19:40:11 CEST', i18n.format_datetime(date, format='long')) |
| 179 |
with i18n.preferred_lang('en_CH'): |
| 180 |
self.assertEqual('29 March 2023, 19:40:11 CEST', i18n.format_datetime(date, format='long')) |
| 181 |
with i18n.preferred_lang('de'): |
| 182 |
self.assertEqual('29. März 2023, 19:40:11 MESZ', i18n.format_datetime(date, format='long')) |
| 183 |
with i18n.preferred_lang('de_CH'): |
| 184 |
self.assertEqual('29. März 2023, 19:40:11 MESZ', i18n.format_datetime(date, format='long')) |
| 185 |
|
| 186 |
def test_list_available_locales(self): |
| 187 |
self.assertEqual(['de', 'en', 'fr'], sorted([str(l) for l in i18n.list_available_locales()])) |
| 188 |
|
| 189 |
def test_list_available_timezones(self): |
| 190 |
timezones = i18n.list_available_timezones() |
| 191 |
self.assertIn('America/Toronto', timezones) |
| 192 |
|
| 193 |
def test_get_timezone_name(self): |
| 194 |
with i18n.preferred_lang('en'): |
| 195 |
self.assertEqual('Eastern Time', i18n.get_timezone_name('America/Toronto')) |
| 196 |
self.assertEqual('ET', i18n.get_timezone_name('America/Toronto', width='short')) |
| 197 |
self.assertEqual('Eastern Time', i18n.get_timezone_name('America/Toronto', width='long')) |
| 198 |
with i18n.preferred_lang('fr'): |
| 199 |
self.assertEqual('heure de l’Est nord-américain', i18n.get_timezone_name('America/Toronto')) |
| 200 |
self.assertEqual('HE', i18n.get_timezone_name('America/Toronto', width='short')) |
| 201 |
self.assertEqual('heure de l’Est nord-américain', i18n.get_timezone_name('America/Toronto', width='long')) |
| 202 |
|
| 203 |
|
| 204 |
class TestI18nDefaultLangWebCase(AbstractI18nTest): |
| 205 |
default_lang = 'FR' |
| 206 |
|
| 207 |
@classmethod |
| 208 |
def teardown_class(cls): |
| 209 |
# Reset default-lang to avoid issue with other test |
| 210 |
cherrypy.config['tools.i18n.default'] = 'en' |
| 211 |
super().teardown_class() |
| 212 |
|
| 213 |
def test_default_lang_without_accept_language(self): |
| 214 |
# Given a default language |
| 215 |
# When user connect to the application without Accept-Language |
| 216 |
self.getPage("/") |
| 217 |
self.assertStatus(200) |
| 218 |
# Then page is displayed with default lang |
| 219 |
self.assertInBody(TEXT_FR) |
| 220 |
|
| 221 |
def test_default_lang_with_accept_language(self): |
| 222 |
# Given a default language |
| 223 |
# When user connect to the application with Accept-Language English |
| 224 |
self.getPage("/", headers=[("Accept-Language", "en-US,en;q=0.8")]) |
| 225 |
self.assertStatus(200) |
| 226 |
# Then page is displayed as english |
| 227 |
self.assertInBody(TEXT_EN) |
| 228 |
|
| 229 |
def test_default_lang_with_unknown_accept_language(self): |
| 230 |
# Given a default language |
| 231 |
# When user connect to the application with Accept-Language English |
| 232 |
self.getPage("/", headers=[("Accept-Language", "it")]) |
| 233 |
self.assertStatus(200) |
| 234 |
# Then page is displayed as english |
| 235 |
self.assertInBody(TEXT_FR) |
| 236 |
|
| 237 |
|
| 238 |
class TestI18nInvalidDefaultLangWebCase(AbstractI18nTest): |
| 239 |
default_lang = 'invalid' |
| 240 |
|
| 241 |
def test_default_lang_invalid(self): |
| 242 |
# Given an invalid default language |
| 243 |
# When user connect to the application without Accept-Language |
| 244 |
self.getPage("/") |
| 245 |
self.assertStatus(200) |
| 246 |
# Then page is displayed with fallback to "en" |
| 247 |
self.assertInBody(TEXT_EN) |