Skip to content

Commit

Permalink
improving the fallback language order feature, related to #688
Browse files Browse the repository at this point in the history
  • Loading branch information
Henri Ylikotila committed Jan 22, 2018
1 parent 31b7549 commit 4264c48
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
37 changes: 13 additions & 24 deletions model/Concept.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,37 +108,26 @@ public function getDeprecated()
*/
public function getLabel()
{
$lang = $this->clang;
// 1. label in current language
if ($this->resource->label($lang) !== null) {
return $this->resource->label($lang);
}

// 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
if ($label !== null && strpos($label->getLang(), $lang . '-') === 0) {
return EasyRdf\Literal::create($label, $lang);
}
}

// 3. label in the vocabulary default language
foreach ($this->vocab->getConfig()->getFallbackLanguages() as $fallback) {
foreach ($this->vocab->getConfig()->getLanguageOrder($this->clang) as $fallback) {
if ($this->resource->label($fallback) !== null) {
return $this->resource->label($fallback);
}
// 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
if ($label !== null && strpos($label->getLang(), $fallback . '-') === 0) {
return EasyRdf\Literal::create($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
// Last resort: 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();
if (!$label->getLang()) {
$label->getValue();
}
return $label->getValue() . " (" . $label->getLang() . ")";
}

// empty
Expand Down
18 changes: 12 additions & 6 deletions model/VocabularyConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,16 +452,22 @@ public function getTypes($lang = null)
/**
* Returns an array of fallback languages that is ordered by priority and
* defined in the vocabulary configuration as a collection.
* Additionally, the chosen content language is inserted with the highest priority
* and the vocab default language is inserted with the lowest priority.
* @param string $clang
* @return array of language code strings
*/
public function getFallbackLanguages()
public function getLanguageOrder($clang)
{
$ret = array();
foreach ($this->resource->get('skosmos:fallbackLanguages') as $lang) {
$ret[] .= $lang;
$ret = array($clang);
$fallbacks = !empty($this->resource->get('skosmos:fallbackLanguages')) ? $this->resource->get('skosmos:fallbackLanguages') : array();
foreach ($fallbacks as $lang) {
if (!in_array($lang, $ret)) {
$ret[] = (string)$lang; // Literal to string conversion
}
}
if (empty($ret)) { // using the vocabulary default language as a fallback.
$ret[] = $this->getDefaultLanguage();
if (!in_array($this->getDefaultLanguage(), $ret)) {
$ret[] = (string)$this->getDefaultLanguage();
}
return $ret;
}
Expand Down

0 comments on commit 4264c48

Please sign in to comment.