Skip to content

Commit

Permalink
added a function to unlocalise the search results
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Oct 3, 2022
1 parent dd96872 commit 7302dec
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions data/processors/export.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import json
from pathlib import Path
from typing import Union, Any

OUTPUT_DIR = Path(__file__).parent.parent / "output"


def unlocalise(value: Union[str, list[Any], dict[str, Any]]) -> Any:
"""Recursively unlocalise a dictionary"""
if isinstance(value, (bool, float, int, str)) or value is None:
return value
if isinstance(value, list):
return [unlocalise(v) for v in value]
if isinstance(value, dict):
# We consider each dict that has only the keys "de" and/or "en" as translated string
if set(value.keys()) | {"de", "en"} == {"de", "en"}:
# Since we only unlocalise dicts with either en and/or de or {}, the default to {} is fine
return value.get("de", value.get("en", {}))

return {k: unlocalise(v) for k, v in value.items()}
raise ValueError(f"Unhandled type {type(value)}")


def export_for_search(data, path):
"""export a subset of the data for the /search api"""
export = []
Expand Down Expand Up @@ -64,6 +81,9 @@ def export_for_search(data, path):
},
)

# the data contains translations, currently we dont allow these in the search api
unlocalise(export)

with open(path, "w", encoding="utf-8") as file:
json.dump(export, file)

Expand Down

0 comments on commit 7302dec

Please sign in to comment.