Skip to content

Commit 19d5b5c

Browse files
Facet / Add meta property to customize label. (#8536)
* Facet / Add meta property to customize label. Add also documentation on how to configure labels when the key used for the facet is not known. * Facet / Add meta property to customize label. * Facet / Add meta property to customize label. Apply translation key filter in getFacetLabel --------- Co-authored-by: Jose García <josegar74@gmail.com>
1 parent b252795 commit 19d5b5c

File tree

9 files changed

+95
-10
lines changed

9 files changed

+95
-10
lines changed

docs/manual/docs/customizing-application/configuring-faceted-search.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,23 @@ When using a generic field like `tag.default` and including only a subset of key
338338
},
339339
```
340340
341+
To translate the label `IDP_TOPICS`, 2 options:
342+
343+
* Use the translation API to add your custom translation in the database for the facet key `facet-IDP_TOPICS` (see the Admin console --> Settings --> Languages).
344+
* Or declare a meta property `labels` in the facet configuration:
345+
346+
``` js
347+
"IDP_TOPICS": {
348+
"terms": {
349+
...
350+
"meta": {
351+
"labels": {
352+
"eng": "IDP topics",
353+
"fre": "Thèmes IDP"
354+
},
355+
```
356+
357+
341358
## Decorate aggregations {#configuring-facet-decorator}
342359
343360
All aggregations can be decorated by an icon or an image in the home page or in other pages. The decorator is configured in the `meta` properties of the facet:

web-ui/src/main/resources/catalog/components/elasticsearch/directives/FacetDirective.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,55 @@
187187
}
188188
]);
189189

190+
module.service("gnFacetMetaLabel", [
191+
"$translate",
192+
"$filter",
193+
function ($translate, $filter) {
194+
var translateKey = function (key) {
195+
try {
196+
var facetKeyTranslatorFilter = $filter("facetKeyTranslator");
197+
var t = facetKeyTranslatorFilter(key);
198+
return t;
199+
} catch (e) {
200+
return key;
201+
}
202+
};
203+
204+
/**
205+
* Retrieves the facet label.
206+
*
207+
* If the facet has a meta-property label defined with the current UI language, retrieves the translation
208+
* from the facet configuration; otherwise it uses the provided translation key to retrieve the translation
209+
* from the application translation files.
210+
*
211+
* {
212+
* "facetConfig": {
213+
* "resourceType": {
214+
* "terms": {
215+
* "field": "resourceType"
216+
* },
217+
* "meta": {
218+
* "labels": {
219+
* "eng": "Hierarchy level",
220+
* "spa": "Nivel jerárquico",
221+
* ...
222+
* },
223+
* ...
224+
*
225+
* @param facet facet configuration.
226+
* @param key translation key.
227+
* @returns {*|null}
228+
*/
229+
this.getFacetLabel = function (facet, key) {
230+
if (!facet || !facet.meta || !facet.meta.labels) {
231+
return translateKey(key);
232+
}
233+
var currentLang = $translate.use();
234+
return facet.meta.labels[currentLang] || translateKey(key);
235+
};
236+
}
237+
]);
238+
190239
module.filter("facetTooltip", [
191240
"$translate",
192241
"$filter",
@@ -266,7 +315,8 @@
266315
module.directive("esFacets", [
267316
"gnFacetSorter",
268317
"gnSearchSettings",
269-
function (gnFacetSorter, gnSearchSettings) {
318+
"gnFacetMetaLabel",
319+
function (gnFacetSorter, gnSearchSettings, gnFacetMetaLabel) {
270320
return {
271321
restrict: "A",
272322
controllerAs: "ctrl",
@@ -291,6 +341,7 @@
291341
// Directive tab field property
292342
scope.isTabMode = scope.ctrl.tabField !== undefined;
293343
scope.facetSorter = gnFacetSorter.sortByTranslation;
344+
scope.getFacetLabel = gnFacetMetaLabel.getFacetLabel;
294345
}
295346
};
296347
}

web-ui/src/main/resources/catalog/components/elasticsearch/directives/partials/facets.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
gn-collapsible="::ctrl.fLvlCollapse[facet.key]"
5555
>
5656
<span class="flex-shrink text-ellipsis"
57-
>{{('facet-' + facet.key) | facetKeyTranslator}}</span
57+
>{{getFacetLabel(facet, 'facet-' + facet.key)}}</span
5858
>
5959
<span
6060
class="fa fa-fw"

web-ui/src/main/resources/catalog/components/metadataactions/RelatedDirective.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,8 @@
889889
module.directive("gnRecordsFilters", [
890890
"$rootScope",
891891
"gnGlobalSettings",
892-
function ($rootScope, gnGlobalSettings) {
892+
"gnFacetMetaLabel",
893+
function ($rootScope, gnGlobalSettings, gnFacetMetaLabel) {
893894
return {
894895
restrict: "A",
895896
templateUrl: function (elem, attrs) {
@@ -911,6 +912,7 @@
911912
scope.criteria = { p: {} };
912913
scope.relatedFacetConfig =
913914
gnGlobalSettings.gnCfg.mods.recordview.relatedFacetConfig;
915+
scope.getFacetLabel = gnFacetMetaLabel.getFacetLabel;
914916

915917
function removeEmptyFilters(filters, agg) {
916918
var cleanFilterPos = [];

web-ui/src/main/resources/catalog/components/metadataactions/partials/recordsFilters.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<h2>{{title}}</h2>
33
<p class="text-muted" translate="">filterHelp</p>
44
<div data-ng-repeat="filter in filtersToProcess">
5-
<label>{{('facet-' + filter) | facetKeyTranslator}}</label>
5+
<label>{{getFacetLabel(agg[filter], 'facet-' + filter)}}</label>
66
<div>
77
<button
88
data-ng-repeat="(key, b) in agg[filter].buckets"

web-ui/src/main/resources/catalog/components/search/searchfiltertag/SearchFilterTagsDirective.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545

4646
module.directive("searchFilterTags", [
4747
"$location",
48-
function ($location) {
48+
"gnFacetMetaLabel",
49+
function ($location, gnFacetMetaLabel) {
4950
return {
5051
restrict: "EA",
5152
require: "^ngSearchForm",
@@ -61,6 +62,12 @@
6162

6263
// key is the raw facet path, value is a valid filter object
6364
scope.facetFilterCache = {};
65+
scope.getFacetLabel = gnFacetMetaLabel.getFacetLabel;
66+
scope.dimensionList = {};
67+
for (var i = 0; i < scope.dimensions.length; i++) {
68+
var dimension = scope.dimensions[i];
69+
scope.dimensionList[dimension.key] = dimension;
70+
}
6471

6572
function getSearchParams() {
6673
if (scope.useLocationParameters) {

web-ui/src/main/resources/catalog/components/search/searchfiltertag/partials/searchFilterTagsTemplate.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
ng-click="removeFilter(filter)"
2222
>
2323
<strong class="text-no-wrap">
24-
<span ng-if="::!specific">{{('facet-' + filter.key) | facetKeyTranslator}}</span>
24+
<span ng-if="::!specific"
25+
>{{getFacetLabel(dimensionList[filter.key], 'facet-' + filter.key)}}</span
26+
>
2527
<span ng-if="::specific">{{filter.key}}</span>
2628
</strong>
2729
<div class="flex-spacer"></div>

web-ui/src/main/resources/catalog/js/CatController.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,7 @@
16081608
"gnExternalViewer",
16091609
"gnAlertService",
16101610
"gnESFacet",
1611+
"gnFacetMetaLabel",
16111612
function (
16121613
$scope,
16131614
$http,
@@ -1628,7 +1629,8 @@
16281629
$cookies,
16291630
gnExternalViewer,
16301631
gnAlertService,
1631-
gnESFacet
1632+
gnESFacet,
1633+
gnFacetMetaLabel
16321634
) {
16331635
$scope.version = "0.0.1";
16341636
var defaultNode = "srv";
@@ -1753,6 +1755,8 @@
17531755
$scope.isExternalViewerEnabled = gnExternalViewer.isEnabled();
17541756
$scope.externalViewerUrl = gnExternalViewer.getBaseUrl();
17551757
$scope.publicationOptions = [];
1758+
$scope.getFacetLabel = gnFacetMetaLabel.getFacetLabel;
1759+
17561760

17571761
$http.get("../api/records/sharing/options").then(function (response) {
17581762
$scope.publicationOptions = response.data;

web-ui/src/main/resources/catalog/views/default/templates/home.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ <h1 data-translate="">topMaps</h1>
123123
<h1 class="col-md-12">
124124
<span data-translate="" data-ng-if="homeFacet.list.length > 2">browseBy</span>
125125
<span data-ng-if="homeFacet.list.length < 3" data-translate="">
126-
{{::('facet-' + homeFacet.list[0]) | facetKeyTranslator}}
126+
{{::getFacetLabel(searchInfo.aggregations[homeFacet.list[0]], 'facet-' +
127+
homeFacet.list[0])}}
127128
</span>
128129
</h1>
129130
<div class="gn-topic-select col-md-12" data-ng-if="homeFacet.list.length > 2">
@@ -139,7 +140,7 @@ <h1 class="col-md-12">
139140
data-ng-model="homeFacet.key"
140141
data-ng-value="facetKey"
141142
/>
142-
<span data-translate="">{{::('facet-' + facetKey) | facetKeyTranslator}}</span
143+
<span data-translate="">{{::getFacetLabel(agg, 'facet-' + facetKey)}}</span
143144
>&nbsp;
144145
</label>
145146
</div>
@@ -157,7 +158,8 @@ <h1 class="col-md-12">
157158
<div data-ng-show="searchInfo.aggregations[homeFacet.lastKey].buckets.length > 0">
158159
<div class="row">
159160
<h1 class="col-md-12" data-translate="">
160-
{{('facet-' + homeFacet.lastKey) | facetKeyTranslator}}
161+
{{getFacetLabel(searchInfo.aggregations[homeFacet.lastKey], 'facet-' +
162+
homeFacet.lastKey)}}
161163
</h1>
162164
</div>
163165
<div class="row">

0 commit comments

Comments
 (0)