diff --git a/server/src/nest/assistant/assistant-attach-answer.ts b/server/src/nest/assistant/assistant-attach-answer.ts index e3ad9a447..71319beff 100644 --- a/server/src/nest/assistant/assistant-attach-answer.ts +++ b/server/src/nest/assistant/assistant-attach-answer.ts @@ -27,21 +27,21 @@ export interface AttachFacts { canBook: boolean; } -/** Each chip's words, as literal keys so the i18n check sees them. */ +/** Each chip's words, under *Key properties so the i18n check (lint:i18n-keys) sees every key. */ const CHIPS = { - keep: 'assistant.attach.chip.keep', - book: 'assistant.attach.chip.book', - attach: 'assistant.attach.chip.attach', - read: 'assistant.attach.chip.read', - keepWithBooking: 'assistant.attach.chip.keepWithBooking', + keepKey: 'assistant.attach.chip.keep', + bookKey: 'assistant.attach.chip.book', + attachKey: 'assistant.attach.chip.attach', + readKey: 'assistant.attach.chip.read', + keepWithBookingKey: 'assistant.attach.chip.keepWithBooking', } as const; /** Why a file held no booking, in the user's words. */ -const WHY: Readonly> = { - ai_not_configured: 'assistant.attach.why.aiNotConfigured', - ai_failed: 'assistant.attach.why.aiFailed', - no_text: 'assistant.attach.why.noText', - nothing_found: 'assistant.attach.why.nothingFound', +const WHY: Readonly> = { + ai_not_configured: { textKey: 'assistant.attach.why.aiNotConfigured' }, + ai_failed: { textKey: 'assistant.attach.why.aiFailed' }, + no_text: { textKey: 'assistant.attach.why.noText' }, + nothing_found: { textKey: 'assistant.attach.why.nothingFound' }, }; /** @@ -53,12 +53,12 @@ const WHY: Readonly> = { export function attachChipKeys(facts: AttachFacts): string[] { const wanted = facts.found > 0 - ? [facts.canKeep && facts.canLink && CHIPS.keepWithBooking, facts.readable && CHIPS.read] + ? [facts.canKeep && facts.canLink && CHIPS.keepWithBookingKey, facts.readable && CHIPS.readKey] : [ - facts.canKeep && CHIPS.keep, - facts.readable && facts.canBook && CHIPS.book, - facts.canKeep && facts.canLink && facts.hasTargets && CHIPS.attach, - facts.readable && CHIPS.read, + facts.canKeep && CHIPS.keepKey, + facts.readable && facts.canBook && CHIPS.bookKey, + facts.canKeep && facts.canLink && facts.hasTargets && CHIPS.attachKey, + facts.readable && CHIPS.readKey, ]; return wanted.filter((key): key is (typeof CHIPS)[keyof typeof CHIPS] => !!key).slice(0, MAX_FOLLOW_UPS); } @@ -70,11 +70,11 @@ export function attachLine( found: number, reason: BookingReadReason | null, ): string { - if (found > 0) return words.count('assistant.attach.found', found, { name }); + if (found > 0) return words.t('assistant.attach.found', { name, count: found }); return [ - words.text('assistant.attach.none', { name }), - ...(reason ? [words.text(WHY[reason])] : []), - words.text('assistant.attach.ask'), + words.t('assistant.attach.none', { name }), + ...(reason ? [words.t(WHY[reason].textKey)] : []), + words.t('assistant.attach.ask'), ].join(' '); } diff --git a/server/src/nest/assistant/assistant-file-answer.service.ts b/server/src/nest/assistant/assistant-file-answer.service.ts index 99e69d224..8518a1069 100644 --- a/server/src/nest/assistant/assistant-file-answer.service.ts +++ b/server/src/nest/assistant/assistant-file-answer.service.ts @@ -175,7 +175,7 @@ export class AssistantFileAnswers { private async acknowledgement(turn: FileTurn, card: TripCard, found: number, reason: BookingReadReason | null) { const words = await AssistantWords.of(turn.locale); const facts = await withRequestContext(this.orm, () => this.factsOf(turn, card, found)); - const questions = attachChipKeys(facts).map((key) => words.text(key)); + const questions = attachChipKeys(facts).map((key) => words.t(key)); return { line: attachLine(words, turn.name, found, reason), chips: questions.length > 0 ? { questions } : null, diff --git a/server/src/nest/assistant/assistant-words.ts b/server/src/nest/assistant/assistant-words.ts index a7ff06c00..cb6b44af4 100644 --- a/server/src/nest/assistant/assistant-words.ts +++ b/server/src/nest/assistant/assistant-words.ts @@ -47,18 +47,21 @@ export class AssistantWords { return new AssistantWords(code, own, english ?? own); } - /** The string under `key` with its params filled in; the key itself where no table has it. */ - text(key: string, params: Readonly> = {}): string { - const value = this.own[key] ?? this.english[key]; - return filled(typeof value === 'string' ? value : key, params); - } - - /** The form of a count-bearing string the language picks for `count`, with `count` among its params. */ - count(key: string, count: number, params: Readonly> = {}): string { - const all = { ...params, count }; + /** + * The string under `key` with its params filled in, as the client's t() + * reads it: a numeric `count` picks the form of a plural group the + * language takes for it. The key itself where no table has it. Named t() + * so lint:i18n-keys sees the keys the server writes with. + */ + t(key: string, params: Readonly> = {}): string { + const { count } = params; + if (typeof count !== 'number') { + const value = this.own[key] ?? this.english[key]; + return filled(typeof value === 'string' ? value : key, params); + } const own = resolvePluralKey(this.own, key, count, getIntlLanguage(this.code)); - if (own) return filled(String(this.own[own]), all); + if (own) return filled(String(this.own[own]), params); const english = resolvePluralKey(this.english, key, count, 'en'); - return english ? filled(String(this.english[english]), all) : key; + return english ? filled(String(this.english[english]), params) : key; } } diff --git a/server/tests/unit/nest/assistant/assistant-attach-answer.test.ts b/server/tests/unit/nest/assistant/assistant-attach-answer.test.ts index 26a4b6865..583bba655 100644 --- a/server/tests/unit/nest/assistant/assistant-attach-answer.test.ts +++ b/server/tests/unit/nest/assistant/assistant-attach-answer.test.ts @@ -65,13 +65,13 @@ describe('attachLine', () => { describe('AssistantWords', () => { it('ASTACH-005: picks the form the language takes for the count, and English where a table has no key', async () => { const ru = await AssistantWords.of('ru-RU'); - expect(ru.count('assistant.attach.found', 21, { name: 'a.pdf' })).toContain('21 бронирование'); - expect(ru.count('assistant.attach.found', 5, { name: 'a.pdf' })).toContain('5 бронирований'); - expect(ru.text('assistant.attach.chip.keep')).toBe('Сохрани его в Файлы'); - expect(ru.text('assistant.no.such.key')).toBe('assistant.no.such.key'); + expect(ru.t('assistant.attach.found', { name: 'a.pdf', count: 21 })).toContain('21 бронирование'); + expect(ru.t('assistant.attach.found', { name: 'a.pdf', count: 5 })).toContain('5 бронирований'); + expect(ru.t('assistant.attach.chip.keep')).toBe('Сохрани его в Файлы'); + expect(ru.t('assistant.no.such.key')).toBe('assistant.no.such.key'); const unknown = await AssistantWords.of('xx'); - expect(unknown.text('assistant.attach.none', { name: 'b.pdf' })).toBe('I could not find a booking in b.pdf.'); - expect(unknown.count('assistant.no.such.key', 2)).toBe('assistant.no.such.key'); + expect(unknown.t('assistant.attach.none', { name: 'b.pdf' })).toBe('I could not find a booking in b.pdf.'); + expect(unknown.t('assistant.no.such.key', { count: 2 })).toBe('assistant.no.such.key'); }); it('ASTACH-006: a locale reads as itself, its language or English', () => {