Skip to content

Commit

Permalink
adding a configurable property that enables defining a priority for l…
Browse files Browse the repository at this point in the history
…abel fallback languages, related to #688
  • Loading branch information
Henri Ylikotila committed Jan 19, 2018
1 parent 4ef44ec commit 31b7549
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
23 changes: 15 additions & 8 deletions model/Concept.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,8 @@ public function getLabel()
if ($this->resource->label($lang) !== null) {
return $this->resource->label($lang);
}

// 2. label in the vocabulary default language
if ($this->resource->label($this->vocab->getConfig()->getDefaultLanguage()) !== null) {
return $this->resource->label($this->vocab->getConfig()->getDefaultLanguage());
}

// 3. label in a subtag of the current language

// 2. label in a subtag of the current language
// We need to check all the labels in case one of them matches a subtag of the current language
foreach($this->resource->allLiterals('skos:prefLabel') as $label) {
// the label lang code is a subtag of the UI lang eg. en-GB - create a new literal with the main language
Expand All @@ -128,7 +123,19 @@ public function getLabel()
}
}

// 4. label in any language, including literal with empty language tag
// 3. label in the vocabulary default language
foreach ($this->vocab->getConfig()->getFallbackLanguages() as $fallback) {
if ($this->resource->label($fallback) !== null) {
return $this->resource->label($fallback);
}
}

// 4. label in the vocabulary default language
if ($this->resource->label($this->vocab->getConfig()->getDefaultLanguage()) !== null) {
return $this->resource->label($this->vocab->getConfig()->getDefaultLanguage());
}

// 5. label in any language, including literal with empty language tag
$label = $this->resource->label();
if ($label !== null) {
return $label->getLang() ? $label->getValue() . " (" . $label->getLang() . ")" : $label->getValue();
Expand Down
17 changes: 17 additions & 0 deletions model/VocabularyConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,4 +448,21 @@ public function getTypes($lang = null)
}
return $ret;
}

/**
* Returns an array of fallback languages that is ordered by priority and
* defined in the vocabulary configuration as a collection.
* @return array of language code strings
*/
public function getFallbackLanguages()
{
$ret = array();
foreach ($this->resource->get('skosmos:fallbackLanguages') as $lang) {
$ret[] .= $lang;
}
if (empty($ret)) { // using the vocabulary default language as a fallback.
$ret[] = $this->getDefaultLanguage();
}
return $ret;
}
}

0 comments on commit 31b7549

Please sign in to comment.