diff --git a/libs/search-widget/src/core/api.ts b/libs/search-widget/src/core/api.ts index c4010353f..b1a7f01b7 100644 --- a/libs/search-widget/src/core/api.ts +++ b/libs/search-widget/src/core/api.ts @@ -9,11 +9,12 @@ import type { TokenAnnotation, } from '@nuclia/core'; import { Nuclia, Resource, ResourceProperties, Search, WritableKnowledgeBox } from '@nuclia/core'; -import { filter, forkJoin, map, merge, Observable, of } from 'rxjs'; +import { filter, forkJoin, map, merge, Observable, of, take } from 'rxjs'; import { nucliaStore } from './stores/main.store'; import { loadModel } from './tensor'; import type { EntityGroup, WidgetOptions } from './models'; import { generatedEntitiesColor } from './utils'; +import { _ } from './i18n'; import type { Annotation } from './stores/annotation.store'; let nucliaApi: Nuclia | null; @@ -92,17 +93,19 @@ export const loadEntities = (): Observable => { if (!nucliaApi) { throw new Error('Nuclia API not initialized'); } - return nucliaApi.knowledgeBox.getEntities().pipe( - map((entityMap: Entities) => + return forkJoin([nucliaApi.knowledgeBox.getEntities(), _.pipe(take(1))]).pipe( + map(([entityMap, translate]) => Object.entries(entityMap) .map(([groupId, group]) => ({ id: groupId, title: group.title || `entities.${groupId.toLowerCase()}`, color: group.color || generatedEntitiesColor[groupId], - entities: Object.entries(group.entities).map(([entityId, entity]) => entity.value), + entities: Object.entries(group.entities) + .map(([entityId, entity]) => entity.value) + .sort((a, b) => a.localeCompare(b)), custom: group.custom, })) - .sort((a, b) => a.id.localeCompare(b.id)), + .sort((a, b) => translate(a.title).localeCompare(translate(b.title))), ), ); }; diff --git a/libs/search-widget/src/core/stores/resource.store.ts b/libs/search-widget/src/core/stores/resource.store.ts index 427257103..04cab4403 100644 --- a/libs/search-widget/src/core/stores/resource.store.ts +++ b/libs/search-widget/src/core/stores/resource.store.ts @@ -1,7 +1,8 @@ -import { BehaviorSubject } from 'rxjs'; +import { BehaviorSubject, take } from 'rxjs'; import type { Resource } from '@nuclia/core'; import type { EntityGroup } from '../models'; import { generatedEntitiesColor } from '../utils'; +import { _ } from '../../core/i18n'; export type ResourceStore = { resource: BehaviorSubject; @@ -26,17 +27,16 @@ function initResourceStore() { function setResource(resource: Resource) { resourceStore.resource.next(resource); - const entityGroups: EntityGroup[] = Object.entries(resource.getNamedEntities()) - .sort((a, b) => a[0].localeCompare(b[0])) - .reduce((groups, [groupId, entities]) => { - groups.push({ + _.pipe(take(1)).subscribe((translate) => { + const entityGroups: EntityGroup[] = Object.entries(resource.getNamedEntities()) + .map(([groupId, entities]) => ({ id: groupId, title: `entities.${groupId.toLowerCase()}`, color: generatedEntitiesColor[groupId], entities: entities.filter((value) => !!value).sort((a, b) => a.localeCompare(b)), - }); - return groups; - }, [] as EntityGroup[]); - resourceStore.resourceEntities.next(entityGroups); - resourceStore.hasEntities.next(entityGroups.length > 0); + })) + .sort((a, b) => translate(a.title).localeCompare(translate(b.title))); + resourceStore.resourceEntities.next(entityGroups); + resourceStore.hasEntities.next(entityGroups.length > 0); + }); }