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

Simplify static translations #2043

Merged
merged 1 commit into from
Sep 29, 2024
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
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(HasDate, StaticTranslationsLocalizable):
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 @@ -167,7 +167,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
17 changes: 7 additions & 10 deletions betty/locale/localizable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
LinkedDataDumpableJsonLdObject,
JsonLdObject,
)
from betty.json.schema import Object, OneOf, Null, Schema
from betty.json.schema import OneOf, Null, Schema
from betty.locale import UNDETERMINED_LOCALE
from betty.locale import negotiate_locale, to_locale
from betty.locale.localized import LocalizedStr
Expand Down Expand Up @@ -288,16 +288,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 @@ -381,9 +380,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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{%- if names | length == 0 -%}
{%- set names = place.names -%}
{%- endif -%}
{%- set name = names | first | localize -%}
{%- set name = (names | first).name | localize -%}

{%- if not embedded -%}
<a href="{{ place | localized_url }}">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends 'entity/page.html.j2' %}
{% set place = place | default(entity) %}
{% set page_title = place.names | first | localize %}
{% set page_title = (place.names | first).name | localize %}
{% block page_content %}
{% include 'entity/meta--place.html.j2' %}

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
4 changes: 2 additions & 2 deletions betty/tests/ancestry/test_citation.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ async def test_dump_linked_data_should_dump_minimal(self) -> None:
"@type": "https://schema.org/Thing",
"id": "the_citation",
"private": False,
"location": {"translations": {}},
"location": {},
"facts": [],
"links": [],
"fileReferences": [],
Expand Down Expand Up @@ -109,7 +109,7 @@ async def test_dump_linked_data_should_dump_full(self) -> None:
"@type": "https://schema.org/Thing",
"id": "the_citation",
"private": False,
"location": {"translations": {}},
"location": {},
"source": "/source/the_source/index.json",
"facts": ["/event/the_event/index.json"],
"links": [],
Expand Down
4 changes: 2 additions & 2 deletions betty/tests/ancestry/test_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ async def test_description(self) -> None:
(
{
"@context": {"description": "https://schema.org/description"},
"description": {"translations": cast(Mapping[str, str], {})},
"description": cast(Mapping[str, str], {}),
},
DummyHasDescription(),
),
(
{
"@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_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ async def test_dump_linked_data_should_dump_minimal(self) -> None:
"citations": [],
"notes": [],
"links": [],
"description": {"translations": {}},
"description": {},
"fileReferences": [],
"place": None,
}
Expand Down Expand Up @@ -196,7 +196,7 @@ async def test_dump_linked_data_should_dump_full(self) -> None:
},
"place": "/place/the_place/index.json",
"links": [],
"description": {"translations": {}},
"description": {},
"fileReferences": [],
}
actual = await assert_dumps_linked_data(event)
Expand Down
4 changes: 2 additions & 2 deletions betty/tests/ancestry/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ async def test_dump_linked_data_should_dump_minimal(self) -> None:
"notes": [],
"links": [],
"referees": [],
"description": {"translations": {}},
"description": {},
}
actual = await assert_dumps_linked_data(file)
assert actual == expected
Expand Down Expand Up @@ -208,7 +208,7 @@ async def test_dump_linked_data_should_dump_full(self) -> None:
"file": "/file/the_file/index.json",
},
],
"description": {"translations": {}},
"description": {},
}
actual = await assert_dumps_linked_data(file)
assert actual == expected
Expand Down
14 changes: 7 additions & 7 deletions betty/tests/ancestry/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
{
"url": "https://example.com",
"label": {"translations": {UNDETERMINED_LOCALE: "Hello, world!"}},
"label": {UNDETERMINED_LOCALE: "Hello, world!"},
},
{
"url": "https://example.com",
Expand Down Expand Up @@ -73,8 +73,8 @@ async def test_dump_linked_data_should_dump_minimal(self) -> None:
"@context": {"description": "https://schema.org/description"},
"url": "https://example.com",
"locale": "und",
"label": {"translations": {}},
"description": {"translations": {}},
"label": {},
"description": {},
}
actual = await assert_dumps_linked_data(link)
assert actual == expected
Expand All @@ -91,10 +91,10 @@ async def test_dump_linked_data_should_dump_full(self) -> None:
"@context": {"description": "https://schema.org/description"},
"url": "https://example.com",
"relationship": "external",
"label": {"translations": {UNDETERMINED_LOCALE: "The Link"}},
"label": {UNDETERMINED_LOCALE: "The Link"},
"locale": "nl-NL",
"mediaType": "text/html",
"description": {"translations": {}},
"description": {},
}
actual = await assert_dumps_linked_data(link)
assert actual == expected
Expand Down Expand Up @@ -166,8 +166,8 @@ async def test_links(self) -> None:
},
"url": "https://example.com",
"locale": "und",
"label": {"translations": {}},
"description": {"translations": {}},
"label": {},
"description": {},
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion betty/tests/ancestry/test_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ async def test_date(self) -> None:
async def test_name(self) -> None:
name = "Ikke"
sut = Name(name)
assert sut.localize(DEFAULT_LOCALIZER) == name
assert sut.name.localize(DEFAULT_LOCALIZER) == name
2 changes: 1 addition & 1 deletion betty/tests/ancestry/test_note.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,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"},
"entity": None,
"links": [],
}
Expand Down
6 changes: 2 additions & 4 deletions betty/tests/ancestry/test_person.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,9 @@ async def test_dump_linked_data_should_dump_full(self) -> None:
{
"@context": {"description": "https://schema.org/description"},
"url": "https://example.com/the-person",
"label": {
"translations": {UNDETERMINED_LOCALE: "The Person Online"}
},
"label": {UNDETERMINED_LOCALE: "The Person Online"},
"locale": "und",
"description": {"translations": {}},
"description": {},
},
],
"fileReferences": [],
Expand Down
10 changes: 3 additions & 7 deletions betty/tests/ancestry/test_place.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,7 @@ async def test_dump_linked_data_should_dump_full(self) -> None:
"@id": "https://example.com/place/the_place/index.json",
"@type": "https://schema.org/Place",
"id": place_id,
"names": [
{"translations": {UNDETERMINED_LOCALE: name}},
],
"names": [{"name": {UNDETERMINED_LOCALE: name}}],
"events": [
"/event/E1/index.json",
],
Expand All @@ -228,11 +226,9 @@ async def test_dump_linked_data_should_dump_full(self) -> None:
{
"@context": {"description": "https://schema.org/description"},
"url": "https://example.com/the-place",
"label": {
"translations": {UNDETERMINED_LOCALE: "The Place Online"}
},
"label": {UNDETERMINED_LOCALE: "The Place Online"},
"locale": "und",
"description": {"translations": {}},
"description": {},
},
],
"coordinates": {
Expand Down
24 changes: 11 additions & 13 deletions betty/tests/ancestry/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ 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"}},
"author": {"translations": {}},
"publisher": {"translations": {}},
"name": {"und": "The Source"},
"author": {},
"publisher": {},
"fileReferences": [],
"contains": [],
"containedBy": None,
Expand Down Expand Up @@ -159,9 +159,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"},
"fileReferences": [],
"contains": [
"/source/the_contained_source/index.json",
Expand All @@ -182,11 +182,9 @@ async def test_dump_linked_data_should_dump_full(self) -> None:
{
"@context": {"description": "https://schema.org/description"},
"url": "https://example.com/the-source",
"label": {
"translations": {UNDETERMINED_LOCALE: "The Source Online"}
},
"label": {UNDETERMINED_LOCALE: "The Source Online"},
"locale": "und",
"description": {"translations": {}},
"description": {},
},
],
}
Expand Down Expand Up @@ -273,9 +271,9 @@ async def test_dump_linked_data_should_dump_with_private_associations(self) -> N
"@type": "https://schema.org/Thing",
"id": "the_source",
"private": False,
"name": {"translations": {}},
"author": {"translations": {}},
"publisher": {"translations": {}},
"name": {},
"author": {},
"publisher": {},
"fileReferences": [],
"contains": [
"/source/the_contained_source/index.json",
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
Loading
Loading