From 63710dd674f95721760230fb320c40b4f1f21555 Mon Sep 17 00:00:00 2001 From: BatLeDev Date: Thu, 12 Nov 2026 17:01:40 +0100 Subject: [PATCH 1/4] feat: internationalized events and session language in the UI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Events sent to the events service carry their title and body in fr and en, from a single table in shared/event-texts.ts (same pattern as portals). The resource title is now the plain catalog title; the associated catalog moves to the event body. Fixes the "à échoué" and "resource" typos. - The document lang follows the session language instead of the fixed "en". - Log dates use the localized dayjs format, resource sizes the session locale. - vue-i18n falls back to English on a missing key. Co-Authored-By: Claude Opus 5.5 --- api/src/catalogs/router.ts | 5 +- api/src/catalogs/service.ts | 13 ++--- api/src/imports/router.ts | 6 +-- api/src/imports/service.ts | 22 ++++---- api/src/publications/router.ts | 6 ++- shared/event-texts.ts | 51 +++++++++++++++++++ .../features/shared/event-texts.unit.spec.ts | 23 +++++++++ ui/index.html | 2 +- ui/src/App.vue | 3 ++ ui/src/components/logs.vue | 2 +- ui/src/components/resources-explorer.vue | 4 +- ui/src/main.ts | 2 +- worker/src/lib/import.ts | 6 ++- worker/src/lib/publication.ts | 6 ++- worker/src/worker.ts | 6 ++- 15 files changed, 122 insertions(+), 35 deletions(-) create mode 100644 shared/event-texts.ts create mode 100644 tests/features/shared/event-texts.unit.spec.ts diff --git a/api/src/catalogs/router.ts b/api/src/catalogs/router.ts index eb1a538..2f3be9d 100644 --- a/api/src/catalogs/router.ts +++ b/api/src/catalogs/router.ts @@ -86,7 +86,7 @@ router.post('/', requireOperationScope('postCatalog'), async (req, res) => { Object.assign(validCatalog, await prepareCatalog(validCatalog)) await mongo.catalogs.insertOne(validCatalog) - sendCatalogEvent(validCatalog, 'a été créé', 'create', sessionState) + sendCatalogEvent(validCatalog, 'create', sessionState) delete validCatalog.secrets // Do not return secrets in the response res.status(201).json(validCatalog) }) @@ -169,8 +169,7 @@ router.delete('/:id', requireOperationScope('deleteCatalog'), async (req, res) = // Delete all imports for this catalog but not the imported datasets await mongo.imports.deleteMany({ 'catalog.id': req.params.id }) - const msg = deletePublications ? 'a été supprimé avec ses publications' : 'a été supprimé' - sendCatalogEvent(catalog, msg, 'delete', sessionState) + sendCatalogEvent(catalog, 'delete', sessionState, deletePublications ? 'delete-with-publications' : 'delete') res.status(204).send() }) diff --git a/api/src/catalogs/service.ts b/api/src/catalogs/service.ts index 2e39917..5811eec 100644 --- a/api/src/catalogs/service.ts +++ b/api/src/catalogs/service.ts @@ -4,6 +4,7 @@ import type { Catalog } from '#types' import eventsQueue from '@data-fair/lib-node/events-queue.js' import { cipher, decipherSecrets } from '@data-fair/catalogs-shared/cipher.ts' +import { type EventAction, eventText } from '@data-fair/catalogs-shared/event-texts.ts' import { getPlugin } from '../plugins/service.ts' import config from '#config' import mongo from '#mongo' @@ -11,18 +12,18 @@ import mongo from '#mongo' /** * Helper function to send events related to catalogs * @param catalog The catalog object - * @param actionText The text describing the action (e.g. "a été créé") * @param topicAction The action part of the topic key (e.g. "create", "delete") * @param sessionState Optional session state for authentication + * @param textAction The action whose text is the title, when it is more precise than the topic one */ export const sendCatalogEvent = ( catalog: Catalog, - actionText: string, - topicAction: string, - sessionState?: SessionStateAuthenticated + topicAction: 'create' | 'patch' | 'delete', + sessionState?: SessionStateAuthenticated, + textAction: EventAction<'catalog'> = topicAction ) => { eventsQueue.pushEvent({ - title: `Le catalogue ${catalog.title} ${actionText}`, + title: eventText(`catalog-${textAction}`, { title: catalog.title }), topic: { key: `catalogs:catalog-${topicAction}:${catalog._id}` }, sender: catalog.owner, resource: { @@ -106,7 +107,7 @@ export const updateCatalog = async (catalog: Catalog, changes: Record { }) if (existingImport && getAccountRole(sessionState, existingImport.owner) === 'admin') { await mongo.imports.deleteOne({ _id: existingImport._id }) - sendImportEvent(existingImport, 'a été supprimé (remplacé par un nouvel import)', 'delete', sessionState) + sendImportEvent(existingImport, 'delete', sessionState, 'replace') } } @@ -94,7 +94,7 @@ router.post('/', requireOperationScope('postImport'), async (req, res) => { const validImport = await validateImport(imp) await mongo.imports.insertOne(validImport) - sendImportEvent(validImport, 'a été créé', 'create', sessionState) + sendImportEvent(validImport, 'create', sessionState) res.status(201).json(validImport) }) @@ -141,7 +141,7 @@ router.delete('/:id', requireOperationScope('deleteImport'), async (req, res) => assertAccountRole(sessionState, importDoc.owner, 'admin') await mongo.imports.deleteOne({ _id: req.params.id }) - sendImportEvent(importDoc, 'a été supprimé', 'delete', sessionState) + sendImportEvent(importDoc, 'delete', sessionState) res.status(204).send() }) diff --git a/api/src/imports/service.ts b/api/src/imports/service.ts index 4d9e5e2..75689be 100644 --- a/api/src/imports/service.ts +++ b/api/src/imports/service.ts @@ -4,29 +4,31 @@ import { emit as wsEmit } from '@data-fair/lib-node/ws-emitter.js' import eventsQueue from '@data-fair/lib-node/events-queue.js' import type { SessionStateAuthenticated } from '@data-fair/lib-express' import { getNextImportDate } from '@data-fair/catalogs-shared/cron.ts' +import { type EventAction, eventText } from '@data-fair/catalogs-shared/event-texts.ts' import mongo from '#mongo' /** - * Helper function to send events related to catalogs - * @param catalog The catalog object - * @param actionText The text describing the action (e.g. "a été créé") + * Helper function to send events related to imports + * @param imp The import object * @param topicAction The action part of the topic key (e.g. "create", "delete") - * @param sessionState Optional session state for authentication + * @param sessionState Session state for authentication + * @param textAction The action whose text is the title, when it is more precise than the topic one */ export const sendImportEvent = ( imp: Import, - actionText: string, - topicAction: string, - sessionState: SessionStateAuthenticated + topicAction: 'create' | 'update' | 'delete', + sessionState: SessionStateAuthenticated, + textAction: EventAction<'import'> = topicAction ) => { eventsQueue.pushEvent({ - title: `L'import de la resource ${imp.remoteResource.title} ${actionText}`, + title: eventText(`import-${textAction}`, { title: imp.remoteResource.title }), + body: eventText('catalog', { title: imp.catalog.title }), topic: { key: `catalogs:import-${topicAction}:${imp._id}` }, sender: imp.owner, resource: { type: 'catalog', id: imp.catalog.id, - title: 'Catalogue associé : ' + imp.catalog.title, + title: imp.catalog.title, } }, sessionState) } @@ -70,7 +72,7 @@ export const updateImport = async (importDoc: Import, changes: Record { // Emit an event for the publication eventsQueue.pushEvent({ - title: `La publication pour le jeu de données ${validPublication.dataFairDataset.title || validPublication.dataFairDataset.id} a été créée`, + title: eventText('publication-create', { title: validPublication.dataFairDataset.title || validPublication.dataFairDataset.id }), + body: eventText('catalog', { title: validPublication.catalog.title }), topic: { key: `catalogs:publication-create:${validPublication._id}` }, sender: validPublication.owner, resource: { type: 'catalog', id: validPublication.catalog.id, - title: 'Catalogue associé : ' + validPublication.catalog.title, + title: validPublication.catalog.title, } }, sessionState) res.status(201).json(validPublication) diff --git a/shared/event-texts.ts b/shared/event-texts.ts new file mode 100644 index 0000000..5ce97f8 --- /dev/null +++ b/shared/event-texts.ts @@ -0,0 +1,51 @@ +// Texts of the events sent to the events service, in every language of the platform: +// events picks the one of each subscription (see InternationalizedTitle in lib-common-types/event) + +export type EventText = { fr: string, en: string } +type Lang = keyof EventText +type Params = Record + +const fr = { + 'catalog-create': 'Le catalogue {title} a été créé', + 'catalog-patch': 'Le catalogue {title} a été modifié', + 'catalog-delete': 'Le catalogue {title} a été supprimé', + 'catalog-delete-with-publications': 'Le catalogue {title} a été supprimé avec ses publications', + 'import-create': "L'import de la ressource {title} a été créé", + 'import-update': "L'import de la ressource {title} a été modifié", + 'import-delete': "L'import de la ressource {title} a été supprimé", + 'import-replace': "L'import de la ressource {title} a été supprimé (remplacé par un nouvel import)", + 'import-error': "L'import de la ressource {title} a échoué", + 'publication-create': 'La publication du jeu de données {title} a été créée', + 'publication-error': 'La publication du jeu de données {title} a échoué', + catalog: 'Catalogue : {title}' +} + +export type EventTextKey = keyof typeof fr + +const en: Record = { + 'catalog-create': 'The catalog {title} was created', + 'catalog-patch': 'The catalog {title} was updated', + 'catalog-delete': 'The catalog {title} was deleted', + 'catalog-delete-with-publications': 'The catalog {title} was deleted along with its publications', + 'import-create': 'The import of the resource {title} was created', + 'import-update': 'The import of the resource {title} was updated', + 'import-delete': 'The import of the resource {title} was deleted', + 'import-replace': 'The import of the resource {title} was deleted (replaced by a new import)', + 'import-error': 'The import of the resource {title} failed', + 'publication-create': 'The publication of the dataset {title} was created', + 'publication-error': 'The publication of the dataset {title} failed', + catalog: 'Catalog: {title}' +} + +export const messages: Record> = { fr, en } + +/** Actions of a resource that have an event title, e.g. EventAction<'import'> = 'create' | 'update' | … */ +export type EventAction = Key extends `${Resource}-${infer Action}` ? Action : never + +const langs = Object.keys(messages) as Lang[] + +const interpolate = (message: string, params: Params) => + message.replace(/\{(\w+)\}/g, (match, name) => params[name] === undefined ? match : String(params[name])) + +export const eventText = (key: EventTextKey, params: Params = {}): EventText => + Object.fromEntries(langs.map(lang => [lang, interpolate(messages[lang][key], params)])) as EventText diff --git a/tests/features/shared/event-texts.unit.spec.ts b/tests/features/shared/event-texts.unit.spec.ts new file mode 100644 index 0000000..d69ea9c --- /dev/null +++ b/tests/features/shared/event-texts.unit.spec.ts @@ -0,0 +1,23 @@ +import { test, expect } from '@playwright/test' +import { eventText, messages } from '../../../shared/event-texts.ts' + +test.describe('event texts', () => { + test('every language has the same keys and placeholders', () => { + const placeholders = (message: string) => [...message.matchAll(/\{(\w+)\}/g)].map(m => m[1]).sort() + for (const [key, message] of Object.entries(messages.fr)) { + expect(placeholders(messages.en[key as keyof typeof messages.en]), key).toEqual(placeholders(message)) + } + expect(Object.keys(messages.en).sort()).toEqual(Object.keys(messages.fr).sort()) + }) + + test('builds an internationalized title', () => { + expect(eventText('import-error', { title: 'Stations' })).toEqual({ + fr: "L'import de la ressource Stations a échoué", + en: 'The import of the resource Stations failed' + }) + }) + + test('keeps a placeholder without value', () => { + expect(eventText('catalog', {}).en).toBe('Catalog: {title}') + }) +}) diff --git a/ui/index.html b/ui/index.html index b2ae812..f62daf4 100644 --- a/ui/index.html +++ b/ui/index.html @@ -1,5 +1,5 @@ - + diff --git a/ui/src/App.vue b/ui/src/App.vue index 8bc01d7..e84fc66 100644 --- a/ui/src/App.vue +++ b/ui/src/App.vue @@ -12,4 +12,7 @@ import uiNotif from '@data-fair/lib-vuetify/ui-notif.vue' import { useFrameServer } from '@data-fair/lib-vue-agents' useFrameServer('catalogs') + +const session = useSession() +watchEffect(() => { document.documentElement.lang = session.lang.value ?? 'fr' }) diff --git a/ui/src/components/logs.vue b/ui/src/components/logs.vue index 9957dfc..f5294b2 100644 --- a/ui/src/components/logs.vue +++ b/ui/src/components/logs.vue @@ -119,7 +119,7 @@ const getIcon = (logs: Log[]) => { return mdiCheckCircle } -const formatDate = (date: string) => dayjs(date).format('DD/MM/YYYY HH:mm') +const formatDate = (date: string) => dayjs(date).format('L LT') const iconsByStatus = { waiting: mdiClock, diff --git a/ui/src/components/resources-explorer.vue b/ui/src/components/resources-explorer.vue index a3da8d7..b8e9971 100644 --- a/ui/src/components/resources-explorer.vue +++ b/ui/src/components/resources-explorer.vue @@ -107,7 +107,7 @@