Skip to content

Commit ba2aa67

Browse files
feat: translate indicator and result description
call translate on input parameters before indicator metadata and indicator result template models are initialized
1 parent 6d7d9e1 commit ba2aa67

10 files changed

+360
-7
lines changed

ohsome_quality_api/indicators/models.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from datetime import datetime, timezone
22
from typing import Literal
33

4-
from pydantic import BaseModel, ConfigDict, Field, computed_field
4+
from fastapi_i18n import _
5+
from pydantic import BaseModel, ConfigDict, Field, computed_field, field_validator
56

67
from ohsome_quality_api.projects.definitions import ProjectEnum
78
from ohsome_quality_api.quality_dimensions.definitions import QualityDimensionEnum
@@ -23,6 +24,11 @@ class IndicatorMetadata(BaseModel):
2324
populate_by_name=True,
2425
)
2526

27+
@field_validator("name", "description", mode="before")
28+
@classmethod
29+
def translate(cls, value: str) -> str:
30+
return _(value)
31+
2632

2733
class LabelDescription(BaseModel):
2834
green: str

ohsome_quality_api/locale/de/LC_MESSAGES/messages.po

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ msgstr ""
287287
msgid ""
288288
"Calculate if mapping has saturated. High saturation has been reached if "
289289
"the growth of the fitted curve is minimal."
290-
msgstr ""
290+
msgstr "Berechne ob das Kartieren gesättigt ist. Bei hohe Sättigung ist die Steigung der Kurve minimal."
291291

292292
#: ohsome_quality_api/indicators/indicators.yaml
293293
msgid "Density"
@@ -464,23 +464,23 @@ msgstr ""
464464

465465
#: ohsome_quality_api/indicators/mapping_saturation/templates.yaml
466466
msgid "No saturation identified (Saturation ≤ 30%)."
467-
msgstr ""
467+
msgstr "Kein Sättigung erkannt (Sättigung ≤ 30%)."
468468

469469
#: ohsome_quality_api/indicators/mapping_saturation/templates.yaml
470470
msgid "Saturation is in progress (30% < Saturation ≤ 97%)."
471-
msgstr ""
471+
msgstr "Sättigung fortschreitend (30% < Sättigung ≤ 97%)."
472472

473473
#: ohsome_quality_api/indicators/mapping_saturation/templates.yaml
474474
msgid "High saturation has been reached (97% < Saturation ≤ 100%)."
475-
msgstr ""
475+
msgstr "Hohe Sättigung ereicht (97% < Sättigung ≤ 100%)."
476476

477477
#: ohsome_quality_api/indicators/mapping_saturation/templates.yaml
478478
msgid "Saturation could not be calculated."
479-
msgstr ""
479+
msgstr "Sättigung konnte nicht berechnet werden."
480480

481481
#: ohsome_quality_api/indicators/mapping_saturation/templates.yaml
482482
msgid "The saturation of the last 3 years is $saturation%."
483-
msgstr ""
483+
msgstr "Die Sättigung der letzten 3 Jahre ist $saturation%."
484484

485485
#: ohsome_quality_api/indicators/minimal/templates.yaml
486486
msgid "Bad data quality."

tests/conftest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import geojson
66
import pytest
7+
from fastapi_i18n.main import Translator, translator
78
from geojson import Feature, FeatureCollection, Polygon
89

910
from ohsome_quality_api.attributes.models import Attribute
@@ -265,3 +266,14 @@ def metadata_indicator_minimal() -> dict[str, IndicatorMetadata]:
265266
@pytest.fixture
266267
def indicators_metadata() -> dict[str, IndicatorMetadata]:
267268
return get_indicator_metadata()
269+
270+
271+
@pytest.fixture
272+
def locale_de(monkeypatch):
273+
monkeypatch.setenv(
274+
"FASTAPI_I18N_LOCALE_DIR",
275+
"/home/matthias/work/projects/oqapi/ohsome_quality_api/locale",
276+
)
277+
token = translator.set(Translator(locale="de"))
278+
yield
279+
translator.reset(token)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import pytest
2+
from approvaltests import verify
3+
4+
from tests.integrationtests.utils import PytestNamer, oqapi_vcr
5+
6+
ENDPOINT = "/indicators/"
7+
8+
# TODO: Remove and recreate VCR cassette for this test
9+
10+
11+
@pytest.fixture
12+
@oqapi_vcr.use_cassette
13+
def indicator(client, bpolys, monkeypatch):
14+
monkeypatch.setenv(
15+
"FASTAPI_I18N_LOCALE_DIR",
16+
"/home/matthias/work/projects/oqapi/ohsome_quality_api/locale",
17+
)
18+
endpoint = "/indicators/mapping-saturation"
19+
parameters = {
20+
"bpolys": bpolys,
21+
"topic": "building-count",
22+
}
23+
response = client.post(
24+
endpoint,
25+
json=parameters,
26+
headers={
27+
"Accept-Language": "de",
28+
"accept": "application/json",
29+
},
30+
)
31+
return response.json()
32+
33+
34+
def test_translation_indicator_metadata_description(indicator):
35+
verify(indicator["result"][0]["metadata"]["description"], namer=PytestNamer())
36+
37+
38+
def test_translation_indicator_result_description(indicator):
39+
"""Test successful translation of result description.
40+
41+
Result description are string templates. This tests checks if translations
42+
of string templates is working as expected.
43+
"""
44+
verify(indicator["result"][0]["result"]["description"], namer=PytestNamer())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Berechne ob das Kartieren gesättigt ist. Bei hohe Sättigung ist die Steigung der Kurve minimal.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Die Sättigung der letzten 3 Jahre ist 99.5%.Hohe Sättigung ereicht (97% < Sättigung ≤ 100%).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Die Sättigung der letzten 3 Jahre ist 99.5%.Hohe Sättigung ereicht (97% < Sättigung ≤ 100%).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "Mapping Saturation",
3+
"description": "Berechne ob das Kartieren gesättigt ist. Bei hohe Sättigung ist die Steigung der Kurve minimal.",
4+
"projects": [
5+
"core",
6+
"corine-land-cover",
7+
"expanse",
8+
"experimental",
9+
"idealvgi",
10+
"mapaction",
11+
"sketchmap",
12+
"bkg",
13+
"unicef"
14+
],
15+
"quality_dimension": "completeness"
16+
}

tests/integrationtests/fixtures/vcr_cassettes/api/test_i18n.yaml

Lines changed: 264 additions & 0 deletions
Large diffs are not rendered by default.

tests/unittests/test_indicators_definitions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
import geojson
55
import pytest
6+
from approvaltests import verify
67
from geojson import Feature, Polygon
78

89
from ohsome_quality_api.indicators import definitions, models
10+
from tests.integrationtests.utils import PytestNamer
911

1012

1113
@pytest.fixture(scope="class")
@@ -61,6 +63,12 @@ def test_get_indicator(metadata_indicator_minimal):
6163
assert indicator == metadata_indicator_minimal["minimal"]
6264

6365

66+
@pytest.mark.usefixtures("locale_de")
67+
def test_get_indicator_de():
68+
indicator = definitions.get_indicator("mapping-saturation")
69+
verify(indicator.model_dump_json(indent=2), namer=PytestNamer())
70+
71+
6472
def test_get_coverage(mock_get_reference_coverage):
6573
coverage = asyncio.run(
6674
definitions.get_coverage("building-comparison", inverse=False)

0 commit comments

Comments
 (0)