Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Make translations usable #1408

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/viur/core/bones/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(
max_length: int | None = 254,
min_length: int | None = None,
natural_sorting: bool | t.Callable = False,
escape_html: bool = True,
**kwargs
):
"""
Expand All @@ -42,6 +43,8 @@ def __init__(
`True` enables sorting according to DIN 5007 Variant 2.
With passing a `callable`, a custom transformer method can be set
that creates the value for the index property.
:param escape_html: Replace some characters in the string with HTML-safe sequences with
using :meth:`utils.string.escape` for safe use in HTML.
:param kwargs: Inherited arguments from the BaseBone.
"""
# fixme: Remove in viur-core >= 4
Expand All @@ -67,6 +70,7 @@ def __init__(
elif not natural_sorting:
self.natural_sorting = None
# else: keep self.natural_sorting as is
self.escape_html = escape_html

def type_coerce_single_value(self, value: t.Any) -> str:
"""Convert a value to a string (if not already)
Expand Down Expand Up @@ -167,9 +171,12 @@ def singleValueFromClient(self, value, skel, bone_name, client_data):
Returns None and the escaped value if the value would be valid for
this bone, otherwise the empty value and an error-message.
"""

if not (err := self.isInvalid(str(value))):
return utils.string.escape(value, self.max_length), None
if self.escape_html:
return utils.string.escape(value, self.max_length), None
elif self.max_length:
return value[:self.max_length], None
return value, None

return self.getEmptyValue(), [ReadFromClientError(ReadFromClientErrorSeverity.Invalid, err)]

Expand Down
21 changes: 20 additions & 1 deletion src/viur/core/modules/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from viur.core.bones import *
from viur.core.i18n import KINDNAME, initializeTranslations, systemTranslations, translate
from viur.core.prototypes.list import List
from viur.core.skeleton import Skeleton, SkeletonInstance
from viur.core.skeleton import Skeleton, SkeletonInstance, ViurTagsSearchAdapter


class Creator(enum.Enum):
Expand All @@ -20,12 +20,28 @@ class Creator(enum.Enum):
class TranslationSkel(Skeleton):
kindName = KINDNAME

database_adapters = [
ViurTagsSearchAdapter(max_length=256),
]

name = StringBone(
descr="Name",
visible=False,
compute=Compute(
fn=lambda skel: str(skel["tr_key"]),
interval=ComputeInterval(ComputeMethod.OnWrite),
),
)

tr_key = StringBone(
descr=translate(
"core.translationskel.tr_key.descr",
"Translation key",
),
searchable=True,
escape_html=False,
required=True,
min_length=1,
unique=UniqueValue(UniqueLockMethod.SameValue, False,
"This translation key exist already"),
)
Expand All @@ -37,6 +53,8 @@ class TranslationSkel(Skeleton):
),
searchable=True,
languages=conf.i18n.available_dialects,
escape_html=False,
max_length=1024,
params={
"tooltip": translate(
"core.translationskel.translations.tooltip",
Expand All @@ -63,6 +81,7 @@ class TranslationSkel(Skeleton):
)

default_text = StringBone(
escape_html=False,
descr=translate(
"core.translationskel.default_text.descr",
"Fallback value",
Expand Down