From 5939f40a5e9019269d8603cc47b29f126110feea Mon Sep 17 00:00:00 2001 From: Ahonen Mika J Date: Thu, 25 Feb 2021 13:00:26 +0200 Subject: [PATCH 1/2] Add support for multilingual properties in case no hit in content language; fixes #1115 --- model/Concept.php | 4 +- tests/ConceptTest.php | 72 +++++++++++++++++++++++++++++++++- tests/test-vocab-data/test.ttl | 16 +++++++- tests/testconfig.ttl | 1 + view/concept-shared.twig | 5 +-- 5 files changed, 90 insertions(+), 8 deletions(-) diff --git a/model/Concept.php b/model/Concept.php index 7fecf2551..eff6291fc 100644 --- a/model/Concept.php +++ b/model/Concept.php @@ -577,8 +577,8 @@ public function getProperties() // Iterating through every literal and adding these to the data object. foreach ($this->resource->allLiterals($sprop) as $val) { $literal = new ConceptPropertyValueLiteral($this->model, $this->vocab, $this->resource, $val, $prop); - // only add literals when they match the content/hit language or have no language defined - if (isset($ret[$prop]) && ($literal->getLang() === $this->clang || $literal->getLang() === null)) { + // only add literals when they match the content/hit language or have no language defined OR when they are literals of a multilingual property + if (isset($ret[$prop]) && ($literal->getLang() === $this->clang || $literal->getLang() === null) || $this->vocab->getConfig()->hasMultiLingualProperty($prop)) { $ret[$prop]->addValue($literal); } diff --git a/tests/ConceptTest.php b/tests/ConceptTest.php index 7e2bfb407..4a1472d81 100644 --- a/tests/ConceptTest.php +++ b/tests/ConceptTest.php @@ -175,7 +175,7 @@ public function testGetPropertiesCorrectNumberOfProperties() { $props = $this->concept->getProperties(); - $this->assertEquals(6, sizeof($props)); + $this->assertEquals(8, sizeof($props)); } /** @@ -188,7 +188,7 @@ public function testGetPropertiesCorrectNumberOfProperties() public function testGetPropertiesCorrectOrderOfProperties() { $props = $this->concept->getProperties(); - $expected = array (0 => 'rdf:type', 1=> 'skos:broader',2 => 'skos:narrower',3 => 'skos:altLabel',4 => 'skos:scopeNote',5 => 'http://www.skosmos.skos/testprop'); + $expected = array (0 => 'rdf:type', 1 => 'skos:broader', 2 => 'skos:narrower', 3 => 'skos:altLabel', 4 => 'skos:scopeNote', 5 => 'http://www.skosmos.skos/multiLingOff', 6 => 'http://www.skosmos.skos/multiLingOn', 7 => 'http://www.skosmos.skos/testprop'); $this->assertEquals($expected, array_keys($props)); } @@ -461,6 +461,74 @@ public function testGetPropertiesWithNarrowersPartOfACollection() } } + /** + * @covers Concept::getProperties + */ + public function testMultilingualPropertiesOnWithLangHit() { + $vocab = $this->model->getVocabulary('test'); + $concepts = $vocab->getConceptInfo('http://www.skosmos.skos/test/ta112', 'en'); + $concept = $concepts[0]; + $props = $concept->getProperties(); + $propvals = $props['http://www.skosmos.skos/multiLingOn']->getValues(); + $runner = array(); + foreach ($propvals as $propval) { + array_push($runner, $propval->getLabel()); + } + $compareableArray = ['English', 'Finnish', 'Without lang tag']; + $this->assertSame($runner, $compareableArray); + } + + /** + * @covers Concept::getProperties + */ + public function testMultilingualPropertiesOnWithoutLangHit() { + $vocab = $this->model->getVocabulary('test'); + $concepts = $vocab->getConceptInfo('http://www.skosmos.skos/test/ta112', 'ru'); + $concept = $concepts[0]; + $props = $concept->getProperties(); + $propvals = $props['http://www.skosmos.skos/multiLingOn']->getValues(); + $runner = array(); + foreach ($propvals as $propval) { + array_push($runner, $propval->getLabel()); + } + $compareableArray = ['English', 'Finnish', 'Without lang tag']; + $this->assertSame($runner, $compareableArray); + } + + /** + * @covers Concept::getProperties + */ + public function testMultilingualPropertiesOffWithLangHit() { + $vocab = $this->model->getVocabulary('test'); + $concepts = $vocab->getConceptInfo('http://www.skosmos.skos/test/ta112', 'en'); + $concept = $concepts[0]; + $props = $concept->getProperties(); + $propvals = $props['http://www.skosmos.skos/multiLingOff']->getValues(); + $runner = array(); + foreach ($propvals as $propval) { + array_push($runner, $propval->getLabel()); + } + $compareableArray = ['English', 'Without lang tag']; + $this->assertSame($runner, $compareableArray); + } + + /** + * @covers Concept::getProperties + */ + public function testMultilingualPropertiesOffWithoutLangHit() { + $vocab = $this->model->getVocabulary('test'); + $concepts = $vocab->getConceptInfo('http://www.skosmos.skos/test/ta112', 'ru'); + $concept = $concepts[0]; + $props = $concept->getProperties(); + $propvals = $props['http://www.skosmos.skos/multiLingOff']->getValues(); + $runner = array(); + foreach ($propvals as $propval) { + array_push($runner, $propval->getLabel()); + } + $compareableArray = ['Without lang tag']; + $this->assertSame($runner, $compareableArray); + } + /** * @covers Concept::getProperties */ diff --git a/tests/test-vocab-data/test.ttl b/tests/test-vocab-data/test.ttl index a588082c5..6dd3bac68 100644 --- a/tests/test-vocab-data/test.ttl +++ b/tests/test-vocab-data/test.ttl @@ -20,6 +20,14 @@ skosmos:testprop a rdf:Property ; rdfs:label "Skosmos test property"@en ; rdfs:comment "description for Skosmos test property"@en . +skosmos:multiLingOn a rdf:Property ; + rdfs:label "Skosmos test property"@en ; + rdfs:comment "description for Skosmos test property"@en . + +skosmos:multiLingOff a rdf:Property ; + rdfs:label "Skosmos test property"@en ; + rdfs:comment "description for Skosmos test property"@en . + skos:prefLabel a rdf:Property ; rdfs:label "preferred label"@en . @@ -52,7 +60,13 @@ test:ta112 a skos:Concept, meta:TestClass ; "Karppi"@fi ; skos:altLabel "Golden crucian"@en; skos:hiddenLabel "Karpit"@fi; - skos:scopeNote "Carp are oily freshwater fish"@en . + skos:scopeNote "Carp are oily freshwater fish"@en; + skosmos:multiLingOn "English"@en, + "Finnish"@fi, + "Without lang tag"; + skosmos:multiLingOff "English"@en, + "Finnish"@fi, + "Without lang tag" . test:ta114 a skos:Concept, meta:TestClass ; skos:broader test:ta1 ; diff --git a/tests/testconfig.ttl b/tests/testconfig.ttl index 7a09af1e7..a48e969e5 100644 --- a/tests/testconfig.ttl +++ b/tests/testconfig.ttl @@ -86,6 +86,7 @@ skosmos:showTopConcepts "true"; skosmos:shortName "Test short", "Testi lyhyt"@fi; + skosmos:hasMultiLingualProperty ; skosmos:sparqlGraph . :test-notation-sort a skosmos:Vocabulary, void:Dataset ; diff --git a/view/concept-shared.twig b/view/concept-shared.twig index c9af3fb21..268b1ef66 100644 --- a/view/concept-shared.twig +++ b/view/concept-shared.twig @@ -93,11 +93,10 @@
{% if request.vocab.config.hasMultiLingualProperty(property.type) %} - {% set prevlang = '' %}{# Only displaying the language when it appears for the very first time #} {% for language,labels in concept.allLabels(property.type) %} {% for value in labels %} -
{{ value.label }}{% if prevlang != language %} {{ language }}{% endif %}
- {% set prevlang = language %} +
+
{{ value.label }}
{% if loop.first %} {{ language }}{% endif %}
{% endfor %} {% endfor %} {% else %} From 1acb473e24f5aba4e01ba96548522415c5a55c44 Mon Sep 17 00:00:00 2001 From: Vainonen Date: Thu, 25 Feb 2021 13:40:11 +0200 Subject: [PATCH 2/2] add support for links in multilingual properties --- view/concept-shared.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/view/concept-shared.twig b/view/concept-shared.twig index 268b1ef66..32b8a9d9d 100644 --- a/view/concept-shared.twig +++ b/view/concept-shared.twig @@ -96,7 +96,7 @@ {% for language,labels in concept.allLabels(property.type) %} {% for value in labels %}
-
{{ value.label }}
{% if loop.first %} {{ language }}{% endif %}
+
{% if value.containsHtml %}{{ value.label|raw }}{% else %}{{ value.label }}{% endif %}
{% if loop.first %} {{ language }}{% endif %}
{% endfor %} {% endfor %} {% else %}