Skip to content

Commit

Permalink
Merge branch '3.6' into fix/caseInsensitive_and_unique
Browse files Browse the repository at this point in the history
  • Loading branch information
phorward authored Jan 23, 2025
2 parents 96bb74d + e81f030 commit dfb78ab
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
59 changes: 31 additions & 28 deletions src/viur/core/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@
on your own. Just use the TranslateSkel).
""" # FIXME: grammar, rst syntax
import datetime
import jinja2.ext as jinja2
import logging
import sys
import traceback
import typing as t
from pathlib import Path

import jinja2.ext as jinja2

from viur.core import current, db, languages, tasks
from viur.core.config import conf

Expand Down Expand Up @@ -157,6 +159,8 @@ class translate:
"translationCache",
"force_lang",
"public",
"filename",
"lineno",
)

def __init__(
Expand All @@ -166,6 +170,7 @@ def __init__(
hint: str = None,
force_lang: str = None,
public: bool = False,
caller_is_jinja: bool = False,
):
"""
:param key: The unique key defining this text fragment.
Expand All @@ -178,59 +183,57 @@ def __init__(
target language.
:param force_lang: Use this language instead the one of the request.
:param public: Flag for public translations, which can be obtained via /json/_translate/get_public.
:param caller_is_jinja: Is the call caused by our jinja method?
"""
super().__init__()
if not isinstance(key, str):
logging.warning(f"Got non-string (type {type(key)}) as {key=}!", exc_info=True)
if force_lang is not None and force_lang not in conf.i18n.available_dialects:
raise ValueError(f"The language {force_lang=} is not available")

key = str(key) # ensure key is a str
self.key = key.lower()
self.defaultText = defaultText or key
self.hint = hint

self.translationCache = None
if force_lang is not None and force_lang not in conf.i18n.available_dialects:
raise ValueError(f"The language {force_lang=} is not available")

self.force_lang = force_lang
self.public = public
self.filename, self.lineno = None, None

if self.key not in systemTranslations and conf.i18n.add_missing_translations:
# This translation seems to be new and should be added
for frame, line in traceback.walk_stack(sys._getframe(0).f_back):
if self.filename is None:
# Use the first frame as fallback.
# In case of calling this class directly,
# this is anyway the caller we're looking for.
self.filename = frame.f_code.co_filename
self.lineno = frame.f_lineno
if not caller_is_jinja:
break
if caller_is_jinja and not frame.f_code.co_filename.endswith(".py"):
# Look for the latest html, macro (not py) where the
# translate method has been used, that's our caller
self.filename = frame.f_code.co_filename
self.lineno = line
break

def __repr__(self) -> str:
return f"<translate object for {self.key} with force_lang={self.force_lang}>"


def __str__(self) -> str:
if self.translationCache is None:
global systemTranslations

from viur.core.render.html.env.viur import translate as jinja_translate

if self.key not in systemTranslations and conf.i18n.add_missing_translations:
# This translation seems to be new and should be added
filename = lineno = None
is_jinja = False
for frame, line in traceback.walk_stack(None):
if filename is None:
# Use the first frame as fallback.
# In case of calling this class directly,
# this is anyway the caller we're looking for.
filename = frame.f_code.co_filename
lineno = frame.f_lineno
if frame.f_code == jinja_translate.__code__:
# The call was caused by our jinja method
is_jinja = True
if is_jinja and not frame.f_code.co_filename.endswith(".py"):
# Look for the latest html, macro (not py) where the
# translate method has been used, that's our caller
filename = frame.f_code.co_filename
lineno = line
break

add_missing_translation(
key=self.key,
hint=self.hint,
default_text=self.defaultText,
filename=filename,
lineno=lineno,
filename=self.filename,
lineno=self.lineno,
public=self.public,
)

Expand Down
3 changes: 2 additions & 1 deletion src/viur/core/module.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
import enum
import functools
import inspect
import types
import typing as t
Expand Down Expand Up @@ -539,7 +540,7 @@ def _update_methods(self):
for key in dir(self):
if key[0] == "_":
continue
if isinstance(getattr(self.__class__, key, None), property):
if isinstance(getattr(self.__class__, key, None), (property, functools.cached_property)):
continue

prop = getattr(self, key)
Expand Down
2 changes: 1 addition & 1 deletion src/viur/core/render/html/env/viur.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def translate(
See also :class:`core.i18n.TranslationExtension`.
"""
return translate_class(key, default_text, hint, force_lang)(**kwargs)
return translate_class(key, default_text, hint, force_lang, caller_is_jinja=True)(**kwargs)


@jinjaGlobalFunction
Expand Down

0 comments on commit dfb78ab

Please sign in to comment.