Skip to content

Commit

Permalink
feat: pattern-parameter for Translation.get_public() (#1337)
Browse files Browse the repository at this point in the history
Allows to filter keys using an `fnmatch`-style pattern to keep request
payload low.
  • Loading branch information
phorward authored Nov 29, 2024
1 parent 4748671 commit d8062aa
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/viur/core/modules/translation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import enum
import fnmatch
import json
import logging
import os
Expand Down Expand Up @@ -186,14 +187,24 @@ def _reload_translations(self):
_last_reload = None # Cut my strings into pieces, this is my last reload...

@exposed
def get_public(self, *, languages: list[str] = None) -> dict[str, str] | dict[str, dict[str, str]]:
def get_public(
self,
*,
languages: list[str] = [],
pattern: str = "*",
) -> dict[str, str] | dict[str, dict[str, str]]:
"""
Dumps public translations as JSON.
:param languages: Allows to request a specific language.
:param pattern: Provide an fnmatch-style key filter pattern
Example calls:
- `/json/_translation/get_public` get public translations for current language
- `/json/_translation/get_public?languages=en` for english translations
- `/json/_translation/get_public?languages=en&pattern=bool.*` for english translations,
but only keys starting with "bool."
- `/json/_translation/get_public?languages=en&languages=de` for english and german translations
- `/json/_translation/get_public?languages=*` for all available languages
"""
Expand All @@ -217,15 +228,15 @@ def get_public(self, *, languages: list[str] = None) -> dict[str, str] | dict[st
lang: {
tr_key: str(translate(tr_key, force_lang=lang))
for tr_key, values in systemTranslations.items()
if values.get("_public_")
if values.get("_public_") and fnmatch.fnmatch(tr_key, pattern)
}
for lang in languages
})

return json.dumps({
tr_key: str(translate(tr_key))
for tr_key, values in systemTranslations.items()
if values.get("_public_")
if values.get("_public_") and fnmatch.fnmatch(tr_key, pattern)
})


Expand Down

0 comments on commit d8062aa

Please sign in to comment.