Skip to content

Commit 6666ba6

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 cab8b52 commit 6666ba6

File tree

6 files changed

+2881
-8
lines changed

6 files changed

+2881
-8
lines changed

ohsome_quality_api/indicators/models.py

Lines changed: 8 additions & 2 deletions
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
@@ -18,11 +19,16 @@ class IndicatorMetadata(BaseModel):
1819
model_config = ConfigDict(
1920
alias_generator=snake_to_lower_camel,
2021
title="Metadata",
21-
frozen=True,
22+
# frozen=True, # TODO: re-enable
2223
extra="forbid",
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."
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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
9+
# Test fallback to en if no translation string found -> Use non existent language.
10+
# Test if no accept-language header is setenv.
11+
# Test result description
12+
# Test result figure
13+
# Remove and recreate VCR cassette for this test
14+
15+
16+
@pytest.fixture
17+
def indicator(client, bpolys, monkeypatch):
18+
monkeypatch.setenv(
19+
"FASTAPI_I18N_LOCALE_DIR",
20+
"/home/matthias/work/projects/oqapi/ohsome_quality_api/locale",
21+
)
22+
endpoint = "/indicators/mapping-saturation"
23+
parameters = {
24+
"bpolys": bpolys,
25+
"topic": "building-count",
26+
}
27+
response = client.post(
28+
endpoint,
29+
json=parameters,
30+
headers={
31+
"Accept-Language": "de",
32+
"accept": "application/json",
33+
},
34+
)
35+
return response.json()
36+
37+
38+
@oqapi_vcr.use_cassette
39+
def test_translation_indicator_metadata_description(indicator):
40+
verify(indicator["result"][0]["metadata"]["description"], namer=PytestNamer())
41+
42+
43+
@oqapi_vcr.use_cassette
44+
def test_translation_indicator_result_description(indicator):
45+
"""Test successful translation of result description.
46+
47+
Result description are string templates. This tests checks if translations
48+
of string templates is working as expected.
49+
"""
50+
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%).

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

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

0 commit comments

Comments
 (0)