From 3fc8b934e634c3299460f48b1387cc08c83f3cdf Mon Sep 17 00:00:00 2001 From: Guenter Hipler <guenter.hipler@unibas.ch> Date: Wed, 26 Aug 2015 09:58:30 -0400 Subject: [PATCH] Added support for facet translation within text domains. --- config/vufind/EDS.ini | 6 ++- config/vufind/Primo.ini | 6 ++- config/vufind/Summon.ini | 8 ++-- config/vufind/facets.ini | 6 ++- .../VuFind/src/VuFind/Search/Base/Options.php | 44 +++++++++++++++++++ .../VuFind/src/VuFind/Search/Base/Params.php | 17 +++---- .../VuFind/src/VuFind/Search/EDS/Options.php | 6 +-- .../VuFind/src/VuFind/Search/EDS/Results.php | 14 +++--- .../src/VuFind/Search/Primo/Options.php | 7 ++- .../src/VuFind/Search/Primo/Results.php | 16 ++++--- .../VuFind/src/VuFind/Search/Solr/Options.php | 6 +-- .../VuFind/src/VuFind/Search/Solr/Results.php | 10 +++-- .../src/VuFind/Search/Summon/Options.php | 7 ++- .../src/VuFind/Search/Summon/Results.php | 6 ++- 14 files changed, 112 insertions(+), 47 deletions(-) diff --git a/config/vufind/EDS.ini b/config/vufind/EDS.ini index 88596dfbe36..035e31bd42a 100644 --- a/config/vufind/EDS.ini +++ b/config/vufind/EDS.ini @@ -132,9 +132,11 @@ facet_limit = 100 ; how many values should we show for each facet? orFacets = * ; Any facets named in the list below will have their values run through the -; translation code; unlisted facets will displayed as-is without translation. For +; translation code; unlisted facets will displayed as-is without translation. For ; translated facets, be sure that all of the necessary strings are included in the -; language files found in the web/lang directory. By default, no facets are +; language files found in the languages directory. You may add a colon and the +; name of a text domain after the field name to specify translations in a specific +; text domain (subdirectory of the languages folder). By default, no facets are ; translated -- uncomment or add lines below to turn on this feature. ;translated_facets[] = SourceType diff --git a/config/vufind/Primo.ini b/config/vufind/Primo.ini index c59d90b8fbf..b99a3566ed1 100644 --- a/config/vufind/Primo.ini +++ b/config/vufind/Primo.ini @@ -115,9 +115,11 @@ facet_limit = 30 ; how many values should we show for each facet? special_facets = daterange ; Any facets named in the list below will have their values run through the -; translation code; unlisted facets will displayed as-is without translation. For +; translation code; unlisted facets will displayed as-is without translation. For ; translated facets, be sure that all of the necessary strings are included in the -; language files found in the web/lang directory. By default, no facets are +; language files found in the languages directory. You may add a colon and the +; name of a text domain after the field name to specify translations in a specific +; text domain (subdirectory of the languages folder). By default, no facets are ; translated -- uncomment or add lines below to turn on this feature. translated_facets[] = tlevel diff --git a/config/vufind/Summon.ini b/config/vufind/Summon.ini index 9dc2c7bbc36..43dae0de7bc 100644 --- a/config/vufind/Summon.ini +++ b/config/vufind/Summon.ini @@ -196,10 +196,12 @@ special_facets = "daterange,checkboxes:Summon" ; to false or omit to disable ORed facets. orFacets = * -; Any facets named in the list below will have their values run through the -; translation code; unlisted facets will displayed as-is without translation. For +; Any facets named in the list below will have their values run through the +; translation code; unlisted facets will displayed as-is without translation. For ; translated facets, be sure that all of the necessary strings are included in the -; language files found in the web/lang directory. By default, no facets are +; language files found in the languages directory. You may add a colon and the +; name of a text domain after the field name to specify translations in a specific +; text domain (subdirectory of the languages folder). By default, no facets are ; translated -- uncomment or add lines below to turn on this feature. ;translated_facets[] = ContentType diff --git a/config/vufind/facets.ini b/config/vufind/facets.ini index a25bf67a45c..cb7db4278fa 100644 --- a/config/vufind/facets.ini +++ b/config/vufind/facets.ini @@ -141,9 +141,11 @@ orFacets = * special_facets = "illustrated,daterange" ; Any facets named in the list below will have their values run through the -; translation code; unlisted facets will displayed as-is without translation. For +; translation code; unlisted facets will displayed as-is without translation. For ; translated facets, be sure that all of the necessary strings are included in the -; language files found in the web/lang directory. By default, no facets are +; language files found in the languages directory. You may add a colon and the +; name of a text domain after the field name to specify translations in a specific +; text domain (subdirectory of the languages folder). By default, no facets are ; translated -- uncomment or add lines below to turn on this feature. ;translated_facets[] = institution ;translated_facets[] = building diff --git a/module/VuFind/src/VuFind/Search/Base/Options.php b/module/VuFind/src/VuFind/Search/Base/Options.php index baf551397d1..e2f57f162e8 100644 --- a/module/VuFind/src/VuFind/Search/Base/Options.php +++ b/module/VuFind/src/VuFind/Search/Base/Options.php @@ -149,6 +149,13 @@ abstract class Options implements TranslatorAwareInterface */ protected $translatedFacets = []; + /** + * Text domains for translated facets + * + * @var array + */ + protected $translatedFacetsTextDomains = []; + /** * Spelling setting * @@ -466,6 +473,43 @@ abstract class Options implements TranslatorAwareInterface return $this->translatedFacets; } + /** + * Configure facet translation using an array of field names with optional + * colon-separated text domains. + * + * @param array $facets Incoming configuration. + * + * @return void + */ + public function setTranslatedFacets($facets) + { + // Reset properties: + $this->translatedFacets = $this->translatedFacetsTextDomains = []; + + // Fill in new data: + foreach ($facets as $current) { + $parts = explode(':', $current); + $this->translatedFacets[] = $parts[0]; + if (isset($parts[1])) { + $this->translatedFacetsTextDomains[$parts[0]] = $parts[1]; + } + } + } + + /** + * Look up the text domain for use when translating a particular facet + * field. + * + * @param string $field Field name being translated + * + * @return string + */ + public function getTextDomainForTranslatedFacet($field) + { + return isset($this->translatedFacetsTextDomains[$field]) + ? $this->translatedFacetsTextDomains[$field] : 'default'; + } + /** * Get current spellcheck setting and (optionally) change it. * diff --git a/module/VuFind/src/VuFind/Search/Base/Params.php b/module/VuFind/src/VuFind/Search/Base/Params.php index 6873b2f1ce9..c6a4a96dda2 100644 --- a/module/VuFind/src/VuFind/Search/Base/Params.php +++ b/module/VuFind/src/VuFind/Search/Base/Params.php @@ -914,11 +914,11 @@ class Params implements ServiceLocatorAwareInterface ? $this->getCheckboxFacetValues() : []; $list = []; + $translatedFacets = $this->getOptions()->getTranslatedFacets(); // Loop through all the current filter fields foreach ($this->filterList as $field => $values) { list($operator, $field) = $this->parseOperatorAndFieldName($field); - $translate - = in_array($field, $this->getOptions()->getTranslatedFacets()); + $translate = in_array($field, $translatedFacets); // and each value currently used for that field foreach ($values as $value) { // Add to the list unless it's in the list of fields to skip: @@ -947,12 +947,13 @@ class Params implements ServiceLocatorAwareInterface */ protected function formatFilterListEntry($field, $value, $operator, $translate) { - return [ - 'value' => $value, - 'displayText' => $translate ? $this->translate($value) : $value, - 'field' => $field, - 'operator' => $operator, - ]; + if ($translate) { + $domain = $this->getOptions()->getTextDomainForTranslatedFacet($field); + $displayText = $this->translate("$domain::$value"); + } else { + $displayText = $value; + } + return compact('value', 'displayText', 'field', 'operator'); } /** diff --git a/module/VuFind/src/VuFind/Search/EDS/Options.php b/module/VuFind/src/VuFind/Search/EDS/Options.php index 41327ea4fdc..e73859e7559 100644 --- a/module/VuFind/src/VuFind/Search/EDS/Options.php +++ b/module/VuFind/src/VuFind/Search/EDS/Options.php @@ -132,9 +132,9 @@ class Options extends \VuFind\Search\Base\Options if (isset($facetConf->Advanced_Facet_Settings->translated_facets) && count($facetConf->Advanced_Facet_Settings->translated_facets) > 0 ) { - foreach ($facetConf->Advanced_Facet_Settings->translated_facets as $c) { - $this->translatedFacets[] = $c; - } + $this->setTranslatedFacets( + $facetConf->Advanced_Facet_Settings->translated_facets->toArray() + ); } } diff --git a/module/VuFind/src/VuFind/Search/EDS/Results.php b/module/VuFind/src/VuFind/Search/EDS/Results.php index 500e1d38426..91828971b8c 100644 --- a/module/VuFind/src/VuFind/Search/EDS/Results.php +++ b/module/VuFind/src/VuFind/Search/EDS/Results.php @@ -104,7 +104,7 @@ class Results extends \VuFind\Search\Base\Results if (is_array($this->responseFacets)) { // Get the filter list -- we'll need to check it below: $filterList = $this->getParams()->getFilters(); - + $translatedFacets = $this->getOptions()->getTranslatedFacets(); foreach ($this->responseFacets as $current) { // The "displayName" value is actually the name of the field on // EBSCO's side -- we'll probably need to translate this to a @@ -112,9 +112,10 @@ class Results extends \VuFind\Search\Base\Results $field = $current['displayName']; // Should we translate values for the current facet? - $translate = in_array( - $field, $this->getOptions()->getTranslatedFacets() - ); + if ($translate = in_array($field, $translatedFacets)) { + $transTextDomain = $this->getOptions() + ->getTextDomainForTranslatedFacet($field); + } // Loop through all the facet values to see if any are applied. foreach ($current['counts'] as $facetIndex => $facetDetails) { @@ -139,8 +140,9 @@ class Results extends \VuFind\Search\Base\Results // Create display value: $current['counts'][$facetIndex]['displayText'] = $translate - ? $this->translate($facetDetails['displayText']) - : $facetDetails['displayText']; + ? $this->translate( + "$transTextDomain::{$facetDetails['displayText']}" + ) : $facetDetails['displayText']; // Create display value: $current['counts'][$facetIndex]['value'] diff --git a/module/VuFind/src/VuFind/Search/Primo/Options.php b/module/VuFind/src/VuFind/Search/Primo/Options.php index 7236deb59cb..304507fdc16 100644 --- a/module/VuFind/src/VuFind/Search/Primo/Options.php +++ b/module/VuFind/src/VuFind/Search/Primo/Options.php @@ -59,10 +59,9 @@ class Options extends \VuFind\Search\Base\Options if (isset($facetSettings->Advanced_Facet_Settings->translated_facets) && count($facetSettings->Advanced_Facet_Settings->translated_facets) > 0 ) { - $list = $facetSettings->Advanced_Facet_Settings->translated_facets; - foreach ($list as $c) { - $this->translatedFacets[] = $c; - } + $this->setTranslatedFacets( + $facetSettings->Advanced_Facet_Settings->translated_facets->toArray() + ); } if (isset($facetSettings->Advanced_Facet_Settings->special_facets)) { $this->specialAdvancedFacets diff --git a/module/VuFind/src/VuFind/Search/Primo/Results.php b/module/VuFind/src/VuFind/Search/Primo/Results.php index cf0fbdfd7e2..a37c6dfd2f8 100644 --- a/module/VuFind/src/VuFind/Search/Primo/Results.php +++ b/module/VuFind/src/VuFind/Search/Primo/Results.php @@ -81,19 +81,23 @@ class Results extends \VuFind\Search\Base\Results $order = array_flip(array_keys($filter)); // Loop through the facets returned by Primo. $facetResult = []; + $translatedFacets = $this->getOptions()->getTranslatedFacets(); if (is_array($this->responseFacets)) { foreach ($this->responseFacets as $field => $current) { - $translate - = in_array($field, $this->getOptions()->getTranslatedFacets()); + if ($translate = in_array($field, $translatedFacets)) { + $transTextDomain = $this->getOptions() + ->getTextDomainForTranslatedFacet($field); + } if (isset($filter[$field])) { $new = []; foreach ($current as $value => $count) { + $rawFixed = $this->getParams()->fixPrimoFacetValue($value); + $displayText = $translate ? $this->translate( + "$transTextDomain::$value", [], $rawFixed + ) : $rawFixed; $new[] = [ 'value' => $value, - 'displayText' => - $translate - ? $this->translate($value) - : $this->getParams()->fixPrimoFacetValue($value), + 'displayText' => $displayText, 'isApplied' => $this->getParams()->hasFilter("$field:" . $value), 'operator' => 'AND', 'count' => $count diff --git a/module/VuFind/src/VuFind/Search/Solr/Options.php b/module/VuFind/src/VuFind/Search/Solr/Options.php index fde902c08f4..f617d467adf 100644 --- a/module/VuFind/src/VuFind/Search/Solr/Options.php +++ b/module/VuFind/src/VuFind/Search/Solr/Options.php @@ -155,9 +155,9 @@ class Options extends \VuFind\Search\Base\Options if (isset($facetSettings->Advanced_Settings->translated_facets) && count($facetSettings->Advanced_Settings->translated_facets) > 0 ) { - foreach ($facetSettings->Advanced_Settings->translated_facets as $c) { - $this->translatedFacets[] = $c; - } + $this->setTranslatedFacets( + $facetSettings->Advanced_Settings->translated_facets->toArray() + ); } if (isset($facetSettings->Advanced_Settings->special_facets)) { $this->specialAdvancedFacets diff --git a/module/VuFind/src/VuFind/Search/Solr/Results.php b/module/VuFind/src/VuFind/Search/Solr/Results.php index c61a0638452..d6b5f94aaa4 100644 --- a/module/VuFind/src/VuFind/Search/Solr/Results.php +++ b/module/VuFind/src/VuFind/Search/Solr/Results.php @@ -240,6 +240,7 @@ class Results extends \VuFind\Search\Base\Results // Loop through every field returned by the result set $fieldFacets = $this->responseFacets->getFieldFacets(); + $translatedFacets = $this->getOptions()->getTranslatedFacets(); foreach (array_keys($filter) as $field) { $data = isset($fieldFacets[$field]) ? $fieldFacets[$field] : []; // Skip empty arrays: @@ -253,15 +254,18 @@ class Results extends \VuFind\Search\Base\Results // Build our array of values for this field $list[$field]['list'] = []; // Should we translate values for the current facet? - $translate - = in_array($field, $this->getOptions()->getTranslatedFacets()); + if ($translate = in_array($field, $translatedFacets)) { + $translateTextDomain = $this->getOptions() + ->getTextDomainForTranslatedFacet($field); + } // Loop through values: foreach ($data as $value => $count) { // Initialize the array of data about the current facet: $currentSettings = []; $currentSettings['value'] = $value; $currentSettings['displayText'] - = $translate ? $this->translate($value) : $value; + = $translate + ? $this->translate("$translateTextDomain::$value") : $value; $currentSettings['count'] = $count; $currentSettings['operator'] = $this->getParams()->getFacetOperator($field); diff --git a/module/VuFind/src/VuFind/Search/Summon/Options.php b/module/VuFind/src/VuFind/Search/Summon/Options.php index ee938d1227c..626a2b65a4b 100644 --- a/module/VuFind/src/VuFind/Search/Summon/Options.php +++ b/module/VuFind/src/VuFind/Search/Summon/Options.php @@ -66,10 +66,9 @@ class Options extends \VuFind\Search\Base\Options if (isset($facetSettings->Advanced_Facet_Settings->translated_facets) && count($facetSettings->Advanced_Facet_Settings->translated_facets) > 0 ) { - $list = $facetSettings->Advanced_Facet_Settings->translated_facets; - foreach ($list as $c) { - $this->translatedFacets[] = $c; - } + $this->setTranslatedFacets( + $facetSettings->Advanced_Facet_Settings->translated_facets->toArray() + ); } if (isset($facetSettings->Advanced_Facet_Settings->special_facets)) { $this->specialAdvancedFacets diff --git a/module/VuFind/src/VuFind/Search/Summon/Results.php b/module/VuFind/src/VuFind/Search/Summon/Results.php index 844d97b320c..108418e0c20 100644 --- a/module/VuFind/src/VuFind/Search/Summon/Results.php +++ b/module/VuFind/src/VuFind/Search/Summon/Results.php @@ -204,6 +204,10 @@ class Results extends \VuFind\Search\Base\Results $translate = in_array( $field, $this->getOptions()->getTranslatedFacets() ); + if ($translate) { + $transTextDomain = $this->getOptions() + ->getTextDomainForTranslatedFacet($field); + } // Loop through all the facet values to see if any are applied. foreach ($current['counts'] as $facetIndex => $facetDetails) { @@ -236,7 +240,7 @@ class Results extends \VuFind\Search\Base\Results // Create display value: $current['counts'][$facetIndex]['displayText'] = $translate - ? $this->translate($facetDetails['value']) + ? $this->translate("$transTextDomain::{$facetDetails['value']}") : $facetDetails['value']; } -- GitLab