Skip to content

Commit

Permalink
Simplify static translations
Browse files Browse the repository at this point in the history
  • Loading branch information
bartfeenstra committed Sep 28, 2024
1 parent d850708 commit c5065dc
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 68 deletions.
15 changes: 8 additions & 7 deletions betty/ancestry/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,30 @@

from betty.ancestry.date import HasDate
from betty.locale.localizable import (
StaticTranslationsLocalizable,
ShorthandStaticTranslations,
RequiredStaticTranslationsLocalizableAttr,
)

if TYPE_CHECKING:
from betty.date import Datey


@final
class Name(StaticTranslationsLocalizable, HasDate):
class Name(HasDate):
"""
A name.
A name can be translated, and have a date expressing the period the name was in use.
"""

#: The name.
name = RequiredStaticTranslationsLocalizableAttr("name")

def __init__(
self,
translations: ShorthandStaticTranslations,
name: ShorthandStaticTranslations,
*,
date: Datey | None = None,
):
super().__init__(
translations,
date=date,
)
super().__init__(date=date)
self.name = name
2 changes: 1 addition & 1 deletion betty/ancestry/place.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def coordinates(self, coordinates: Point):
@property
def label(self) -> Localizable:
with suppress(IndexError):
return self.names[0]
return self.names[0].name
return super().label

@override
Expand Down
15 changes: 6 additions & 9 deletions betty/locale/localizable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,15 @@ def __init__(
self, *, title: str = "Static translations", description: str | None = None
):
super().__init__(
def_name="staticTranslations", title=title, description=description
title=title,
description=(
(description or "") + "Keys are IETF BCP-47 language tags."
).strip(),
)
translations_schema = Object(
title="Translations", description="Keys are IETF BCP-47 language tags."
)
translations_schema._schema["additionalProperties"] = {
self._schema["additionalProperties"] = {
"type": "string",
"description": "A human-readable translation.",
}
self.add_property("translations", translations_schema)


class StaticTranslationsLocalizable(
Expand Down Expand Up @@ -376,9 +375,7 @@ def localize(self, localizer: Localizer) -> LocalizedStr:

@override
async def dump_linked_data(self, project: Project) -> DumpMapping[Dump]:
return {
"translations": {**self._translations},
}
return {**self._translations}

@override
@classmethod
Expand Down
2 changes: 1 addition & 1 deletion betty/project/extension/cotton_candy/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ async def _build_place(self, place: Place) -> Mapping[str, str] | None:
"text": " ".join(
translation.lower()
for name in place.names
for translation in name.translations.values()
for translation in name.name.translations.values()
),
"result": await self._render_entity(place),
}
Expand Down
2 changes: 1 addition & 1 deletion betty/tests/ancestry/test_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async def test_description(self) -> None:
(
{
"@context": {"description": "https://schema.org/description"},
"description": {"translations": {"und": "Hello, world!"}},
"description": {"und": "Hello, world!"},
},
DummyHasDescription(description="Hello, world!"),
),
Expand Down
4 changes: 2 additions & 2 deletions betty/tests/ancestry/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
{
"url": "https://example.com",
"label": {"translations": {UNDETERMINED_LOCALE: "Hello, world!"}},
"label": {UNDETERMINED_LOCALE: "Hello, world!"},
},
{
"url": "https://example.com",
Expand Down Expand Up @@ -88,7 +88,7 @@ async def test_dump_linked_data_should_dump_full(self) -> None:
expected: Mapping[str, Any] = {
"url": "https://example.com",
"relationship": "external",
"label": {"translations": {UNDETERMINED_LOCALE: "The Link"}},
"label": {UNDETERMINED_LOCALE: "The Link"},
"locale": "nl-NL",
"mediaType": "text/html",
}
Expand Down
2 changes: 1 addition & 1 deletion betty/tests/ancestry/test_note.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async def test_dump_linked_data_should_dump_full(self) -> None:
"@type": "https://schema.org/Thing",
"id": "the_note",
"private": False,
"text": {"translations": {"und": "The Note"}},
"text": {"und": "The Note"},
"links": [
{
"url": "/note/the_note/index.json",
Expand Down
4 changes: 1 addition & 3 deletions betty/tests/ancestry/test_person.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,7 @@ async def test_dump_linked_data_should_dump_full(self) -> None:
"links": [
{
"url": "https://example.com/the-person",
"label": {
"translations": {UNDETERMINED_LOCALE: "The Person Online"}
},
"label": {UNDETERMINED_LOCALE: "The Person Online"},
"locale": "und",
},
{
Expand Down
6 changes: 2 additions & 4 deletions betty/tests/ancestry/test_place.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ async def test_dump_linked_data_should_dump_full(self) -> None:
"@type": "https://schema.org/Place",
"id": place_id,
"names": [
{"translations": {"nl-NL": name}},
{"nl-NL": name},
],
"events": [
"/event/E1/index.json",
Expand All @@ -241,9 +241,7 @@ async def test_dump_linked_data_should_dump_full(self) -> None:
"links": [
{
"url": "https://example.com/the-place",
"label": {
"translations": {UNDETERMINED_LOCALE: "The Place Online"}
},
"label": {UNDETERMINED_LOCALE: "The Place Online"},
"locale": "und",
},
{
Expand Down
12 changes: 5 additions & 7 deletions betty/tests/ancestry/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ async def test_dump_linked_data_should_dump_minimal(self) -> None:
"@type": "https://schema.org/Thing",
"id": "the_source",
"private": False,
"name": {"translations": {"und": "The Source"}},
"name": {"und": "The Source"},
"contains": [],
"citations": [],
"notes": [],
Expand Down Expand Up @@ -168,9 +168,9 @@ async def test_dump_linked_data_should_dump_full(self) -> None:
"@type": "https://schema.org/Thing",
"id": "the_source",
"private": False,
"name": {"translations": {"und": "The Source"}},
"author": {"translations": {"und": "The Author"}},
"publisher": {"translations": {"und": "The Publisher"}},
"name": {"und": "The Source"},
"author": {"und": "The Author"},
"publisher": {"und": "The Publisher"},
"contains": [
"/source/the_contained_source/index.json",
],
Expand All @@ -189,9 +189,7 @@ async def test_dump_linked_data_should_dump_full(self) -> None:
"links": [
{
"url": "https://example.com/the-source",
"label": {
"translations": {UNDETERMINED_LOCALE: "The Source Online"}
},
"label": {UNDETERMINED_LOCALE: "The Source Online"},
"locale": "und",
},
{
Expand Down
2 changes: 1 addition & 1 deletion betty/tests/gramps/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ async def test_place_should_include_name(self) -> None:
names = place.names
assert len(names) == 1
name = names[0]
assert name.localize(DEFAULT_LOCALIZER) == "Amsterdam"
assert name.name.localize(DEFAULT_LOCALIZER) == "Amsterdam"

async def test_place_should_include_note(self) -> None:
ancestry = await self._load_partial(
Expand Down
25 changes: 6 additions & 19 deletions betty/tests/locale/localizable/test___init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,34 +169,21 @@ def test_replace(self) -> None:
("expected", "translations"),
[
(
{"translations": {}},
{},
{},
),
(
{
"translations": {
UNDETERMINED_LOCALE: "Hello, world!",
}
},
{UNDETERMINED_LOCALE: "Hello, world!"},
"Hello, world!",
),
(
{
"translations": {
"en-US": "Hello, world!",
}
},
{"en-US": "Hello, world!"},
{
"en-US": "Hello, world!",
},
),
(
{
"translations": {
"nl-NL": "Hallo, wereld!",
"en": "Hello, world!",
}
},
{"nl-NL": "Hallo, wereld!", "en": "Hello, world!"},
{
"nl-NL": "Hallo, wereld!",
"en": "Hello, world!",
Expand All @@ -220,8 +207,8 @@ async def get_sut_instances(
self,
) -> Sequence[tuple[Schema, Sequence[Dump], Sequence[Dump]]]:
valid_datas: Sequence[Dump] = [
{"translations": {DEFAULT_LOCALE: "Hello, world!"}},
{"translations": {"nl": "Hallo, wereld!", "uk": "Привіт Світ!"}},
{DEFAULT_LOCALE: "Hello, world!"},
{"nl": "Hallo, wereld!", "uk": "Привіт Світ!"},
]
invalid_datas: Sequence[Dump] = [
True,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from pathlib import Path

from betty.ancestry.presence import Presence
from betty.ancestry.citation import Citation
from betty.ancestry.event import Event
from betty.ancestry.event_type.event_types import Unknown as UnknownEventType
Expand All @@ -9,10 +8,11 @@
from betty.ancestry.name import Name
from betty.ancestry.person import Person
from betty.ancestry.place import Place
from betty.ancestry.presence import Presence
from betty.ancestry.presence_role.presence_roles import Subject
from betty.ancestry.source import Source
from betty.project.extension.cotton_candy import CottonCandy
from betty.locale.localizer import DEFAULT_LOCALIZER
from betty.project.extension.cotton_candy import CottonCandy
from betty.test_utils.assets.templates import TemplateTestBase


Expand Down Expand Up @@ -72,8 +72,8 @@ async def test_privacy(self, tmp_path: Path) -> None:
) as (actual, _):
assert public_file.description
assert public_file.description.localize(DEFAULT_LOCALIZER) in actual
assert place_name
assert place_name.localize(DEFAULT_LOCALIZER) in actual
assert place_name.name
assert place_name.name.localize(DEFAULT_LOCALIZER) in actual
assert (
public_person_for_presence.label.localize(DEFAULT_LOCALIZER) in actual
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from betty.ancestry.name import Name
from betty.ancestry.place import Place
from betty.date import Date
from betty.project.extension.cotton_candy import CottonCandy
from betty.locale.localizer import DEFAULT_LOCALIZER
from betty.project.extension.cotton_candy import CottonCandy
from betty.test_utils.assets.templates import TemplateTestBase


Expand Down Expand Up @@ -62,15 +62,15 @@ async def test_privacy(self) -> None:
"entity": place,
},
) as (actual, _):
assert place_name
assert place_name.localize(DEFAULT_LOCALIZER) in actual
assert place_name.name
assert place_name.name.localize(DEFAULT_LOCALIZER) in actual
assert public_place_event.description
assert public_place_event.description.localize(DEFAULT_LOCALIZER) in actual
assert enclosed_name
assert enclosed_name.localize(DEFAULT_LOCALIZER) in actual
assert enclosing_name
assert enclosing_name.localize(DEFAULT_LOCALIZER) in actual
assert enclosing_name.localize(DEFAULT_LOCALIZER) in actual
assert enclosed_name.name
assert enclosed_name.name.localize(DEFAULT_LOCALIZER) in actual
assert enclosing_name.name
assert enclosing_name.name.localize(DEFAULT_LOCALIZER) in actual
assert enclosing_name.name.localize(DEFAULT_LOCALIZER) in actual
assert public_enclosed_event.description
assert (
public_enclosed_event.description.localize(DEFAULT_LOCALIZER) in actual
Expand Down

0 comments on commit c5065dc

Please sign in to comment.