Skip to content

Commit

Permalink
Rename wrong private variables
Browse files Browse the repository at this point in the history
  • Loading branch information
uittenbroekrobbert committed Jun 25, 2024
1 parent 028301d commit 9561786
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
28 changes: 14 additions & 14 deletions tad/core/internationalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,46 @@

logger = logging.getLogger(__name__)

__default_language_fallback = "en"
__dynamic_field_translations: dict[str, dict[str, str]] = {}
__translations: dict[str, NullTranslations] = {}
_default_language_fallback = "en"
_dynamic_field_translations: dict[str, dict[str, str]] = {}
_translations: dict[str, NullTranslations] = {}
supported_translations = ["en", "nl", "fy"]
# babel does not support Frysian, to be able to load the right MO file, we need to 'map' it ourselves
___translations_to_locale = {"en": "en", "nl": "nl", "fy": "nl_FY"}
__translations_to_locale = {"en": "en", "nl": "nl", "fy": "nl_FY"}


def get_dynamic_field_translations(lang: str) -> dict[str, str]:
lang = get_supported_translation(lang)
if lang not in __dynamic_field_translations:
if lang not in _dynamic_field_translations:
with open(f"{get_settings().LANGUAGE_FILES_LOCATION}/{lang}.yaml") as stream:
translation = yaml.safe_load(stream)
if get_settings().CACHE_LANGUAGE_FILES:
logger.info("Loading translation to cache: %s", lang)
__dynamic_field_translations[lang] = translation
_dynamic_field_translations[lang] = translation
else:
logger.info("Returning translation without cache due to CACHE_LANGUAGE_FILES settings : %s", lang)
return translation
return __dynamic_field_translations[lang]
return _dynamic_field_translations[lang]


def get_supported_translation(lang: str):
if lang not in supported_translations:
logger.warning("Requested translation does not exist: %s, using fallback %s", lang, __default_language_fallback)
lang = __default_language_fallback
logger.warning("Requested translation does not exist: %s, using fallback %s", lang, _default_language_fallback)
lang = _default_language_fallback
return lang


def get_translation(lang: str) -> NullTranslations:
lang = get_supported_translation(lang)
if lang not in __translations:
translation = Translations.load("tad/locale/", locales=___translations_to_locale[lang])
if lang not in _translations:
translation = Translations.load("tad/locale/", locales=__translations_to_locale[lang])
if get_settings().CACHE_LANGUAGE_FILES:
logger.info("Loading translation to cache: %s", lang)
__translations[lang] = translation
_translations[lang] = translation
else:
logger.info("Returning translation without cache due to CACHE_LANGUAGE_FILES settings : %s", lang)
return translation
return __translations[lang]
return _translations[lang]


def format_datetime(value: datetime, locale: str, format: str = "medium") -> str:
Expand All @@ -71,4 +71,4 @@ def format_datetime(value: datetime, locale: str, format: str = "medium") -> str

def get_requested_language(request: Request) -> str:
# todo (robbert): nice to have, default support based on accept lang of browser
return request.cookies.get("lang", __default_language_fallback)
return request.cookies.get("lang", _default_language_fallback)
14 changes: 7 additions & 7 deletions tests/core/test_internationalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from fastapi import Request
from tad.core.config import get_settings
from tad.core.internationalization import (
__dynamic_field_translations, # type: ignore
__translations, # type: ignore
_dynamic_field_translations, # type: ignore
_translations, # type: ignore
format_datetime,
get_dynamic_field_translations,
get_requested_language,
Expand Down Expand Up @@ -53,16 +53,16 @@ def test_warning_get_dynamic_field_translations(caplog: pytest.LogCaptureFixture


def test_no_cache_get_dynamic_field_translations():
__dynamic_field_translations.clear()
_dynamic_field_translations.clear()
get_settings().CACHE_LANGUAGE_FILES = False
yaml_en = get_dynamic_field_translations("en")
get_settings().CACHE_LANGUAGE_FILES = True
assert len(yaml_en) == 4
assert len(__dynamic_field_translations) == 0
assert len(_dynamic_field_translations) == 0


def test_get_translation(caplog: pytest.LogCaptureFixture):
__translations.clear()
_translations.clear()
translation = get_translation("en")
assert caplog.records[0].message == "Loading translation to cache: en"
assert type(translation) == Translations
Expand All @@ -82,13 +82,13 @@ def test_warning_get_translation(caplog: pytest.LogCaptureFixture):


def test_no_cache_get_translations(caplog: pytest.LogCaptureFixture):
__translations.clear()
_translations.clear()
get_settings().CACHE_LANGUAGE_FILES = False
translation = get_translation("en")
assert caplog.records[0].message == "Returning translation without cache due to CACHE_LANGUAGE_FILES settings : en"
get_settings().CACHE_LANGUAGE_FILES = True
assert type(translation) == Translations
assert len(__translations) == 0
assert len(_translations) == 0


def test_format_datetime():
Expand Down

0 comments on commit 9561786

Please sign in to comment.