Skip to content
Snippets Groups Projects

Localization support

Merged mikrats requested to merge localization-fi into main
+ 103
0
"""
Translation tests
=================
"""
import os
from typing import List
import pytest
from flask import Flask
from flask_babel import (
Babel,
Locale,
force_locale,
gettext,
get_translations,
)
from babel.messages.extract import extract_from_dir
from tjts5901.i18n import SupportedLocales
from tjts5901 import __file__ as pkg_file
from conftests import client, app
@pytest.fixture
def babel(app: Flask):
"""
Babel translation fixture.
Returns babel tranlaslation fixture registered in flask app
"""
with app.app_context():
yield app.extensions['babel'].instance
def test_babel_translations(app: Flask, babel: Babel):
"""
Test that translations exists for test string "Hello, world!". This test
will fail if the translation is missing for any language.
This test is not intended to test the translation itself, but rather to
ensure that the translation exists.
And if the actual translation for "Hello, world!" is "Hello, world!", then
the test needs to be updated to use a different test string.
"""
# For flask_babel to work, we need to run in app context
with app.app_context():
# Iterate through all of the languages available.
languages: List[Locale] = babel.list_translations()
for locale in languages:
if locale.language == "en":
# By default everything should be in english
continue
with force_locale(locale):
assert gettext("Hello, World!") != "Hello, World!", f"Message is not translated for language {locale.language}"
def test_app_language_detection(client, babel):
"""
Similar to :func:`test_babel_translations`, but uses e2e test client
to test translations.
Uses the Accept-Language header to set the language for the request.
TODO: Write variation that includes the territory code in the Accept-Language header.
"""
# Iterate through all of the languages available.
with client.application.app_context():
languages: List[Locale] = babel.list_translations()
for locale in languages:
if locale.language == "en":
# By default everything should be in english
continue
response = client.get('/hello', headers={'Accept-Language': locale.language})
resp_as_string = response.data.decode('utf-8')
assert gettext("Hello, World!") == resp_as_string, f"Message is not translated for language {locale.language}"
@pytest.fixture(scope="session")
def app_strings():
"""
Fixture for extracting strings from the application source code.
"""
# TODO: Read method_map from config file
method_map = [
('**.py', 'python'),
('**/templates/**.html', 'jinja2'),
]
# Collect all of the messages from the source code
dir_path = os.path.dirname(pkg_file)
messages = set()
for msg in extract_from_dir(dir_path, method_map):
messages.add(msg[2])
return messages
Loading