Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
block/description: support OSM's description when country lang is k…
Browse files Browse the repository at this point in the history
…nown
  • Loading branch information
remi-dupre committed Jul 22, 2021
1 parent a78ae14 commit 58f7563
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
47 changes: 46 additions & 1 deletion idunn/places/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,41 @@
"country": 7,
}

# List of official languages for given country codes.
COUNTRY_LANGUAGES = {
# European coutries (source: https://en.wikipedia.org/wiki/Member_state_of_the_European_Union)
"at": ["de"],
"be": ["nl", "fr", "de"],
"bg": ["bg"],
"hr": ["hr"],
"cy": ["el", "tr"],
"cz": ["cs"],
"dk": ["da"],
"ee": ["et"],
"fi": ["fi", "sv"],
"fr": ["fr"],
"de": ["de"],
"gr": ["el"],
"hu": ["hu"],
"ie": ["en", "ga"],
"it": ["it"],
"lv": ["lv"],
"lt": ["lt"],
"lu": ["fr", "de", "lu"],
"mt": ["mt", "en"],
"nl": ["nl"],
"pl": ["pl"],
"pt": ["pt"],
"ro": ["ro"],
"sk": ["sk"],
"si": ["sl"],
"es": ["es", "gl", "ca", "oc", "eu"],
"se": ["sv"],
# Other countries
"gb": ["en"],
"us": ["en"],
}


class BasePlace(dict):
PLACE_TYPE = ""
Expand Down Expand Up @@ -273,7 +308,17 @@ def get_quotation_request_url(self):
return None

def get_description(self, lang):
return self.properties.get(f"description:{lang}")
if f"description:{lang}" in self.properties:
return self.properties.get(f"description:{lang}")

country_code = self.get_country_code()

# Check that there is little to no ambiguity on local language and that
# it matches `lang`.
if not country_code or COUNTRY_LANGUAGES.get(country_code.lower()) != [lang.lower()]:
return None

return self.properties.get("description")

def get_description_url(self, _lang):
return None
Expand Down
33 changes: 33 additions & 0 deletions tests/test_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from idunn.blocks.description import DescriptionBlock
from idunn.places import POI
from .utils import read_fixture


def orsay(lang=None):
place = {"properties": {}}

full = read_fixture("fixtures/orsay_museum.json")
place["administrative_regions"] = full["administrative_regions"]

key = f"description:{lang}" if lang else "description"
place["properties"][key] = "Le musée d’Orsay est un musée national inauguré en 1986."

return POI(place)


def test_description_block_with_lang():
assert DescriptionBlock.from_es(orsay(lang="fr"), lang="en") is None
assert DescriptionBlock.from_es(orsay(lang="fr"), lang="fr") == DescriptionBlock(
type="description",
description="Le musée d’Orsay est un musée national inauguré en 1986.",
source="osm",
)


def test_description_block_without_lang():
assert DescriptionBlock.from_es(orsay(), lang="en") is None
assert DescriptionBlock.from_es(orsay(), lang="fr") == DescriptionBlock(
type="description",
description="Le musée d’Orsay est un musée national inauguré en 1986.",
source="osm",
)

0 comments on commit 58f7563

Please sign in to comment.