From e7bae5edd76a0113a6d06555dabc685930c279a7 Mon Sep 17 00:00:00 2001 From: Sluijsens Date: Mon, 16 Nov 2026 00:30:01 +0100 Subject: [PATCH] Translation files: the form messages of the cms-client package come from nl.json Block cmsClient.contact holds the field errors, fallback, waiting time (one and other), too-large, captcha-required, failure and sending label. src/i18n/cmsClientMessages.ts builds the ContactMessages object for useContactForm; a test checks that it says the same as the package's own Dutch defaults, which stay as the fallback. Co-Authored-By: Claude Sonnet 5.5 --- docs/translations.md | 6 ++-- .../components/ContactForm/ContactForm.tsx | 3 ++ web/src/i18n/cmsClientMessages.test.ts | 33 +++++++++++++++++ web/src/i18n/cmsClientMessages.ts | 20 +++++++++++ web/src/i18n/i18n.test.ts | 1 + web/src/i18n/nl.json | 35 +++++++++++++++++++ 6 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 web/src/i18n/cmsClientMessages.test.ts create mode 100644 web/src/i18n/cmsClientMessages.ts diff --git a/docs/translations.md b/docs/translations.md index 0ae1d9a..4abbb7b 100644 --- a/docs/translations.md +++ b/docs/translations.md @@ -4,7 +4,7 @@ The texts of the site live in language files, not in the components. Dutch is th ## Where things are -- `web/src/i18n/nl.json`: all Dutch texts of the site in one file, in blocks per area in the order of the site: `common`, `site`, `home`, `about`, `contact`, `shoots`, `portfolio`, `instagram`, `testimonials`, `seo`. The file is the source of the types. A second language is `web/src/i18n/en.json` with the same keys. +- `web/src/i18n/nl.json`: all Dutch texts of the site in one file, in blocks per area in the order of the site: `common`, `site`, `home`, `about`, `contact`, `shoots`, `portfolio`, `instagram`, `testimonials`, `seo`, `cmsClient`. The file is the source of the types. A second language is `web/src/i18n/en.json` with the same keys. - `web/src/i18n/index.ts`: `t(key, params?)` for a text, `tList(key)` for a list of texts, `tPlural(key, count, params?)` for a text with a singular and a plural form, `locales`, `locale` and `Messages`. - `web/src/i18n/compare.ts`: the checks the tests use (same keys and parameters, nothing empty). - The typed content modules in `web/src/content` keep their structure, images and paths; the words come from the language files. @@ -40,7 +40,9 @@ Dotted paths: the block (`common`, `home`, ...), then the page or section, then | `portfolio.json`, `instagram.json`, `testimonials.json` | those pages and sections, with their photo alt texts and reviews | | `seo.json` | page titles, descriptions and link-preview alt texts per page (`seo.pages.`) | -Not here: the hero, the privacy statement, everything from the CMS, and the form validation messages of the copied `src/cms-client` package (`messages.nl.ts`). +The block `cmsClient` holds the texts of the shared `src/cms-client` package, per part of the package: `cmsClient.contact` has the form's field errors, fallback, waiting time (`rateLimited.one/other` with `{minutes}`), too-large, captcha-required, failure (`{address}`) and the sending label. `src/i18n/cmsClientMessages.ts` builds the `ContactMessages` object from it and `ContactForm` passes it to `useContactForm({ messages })`. The package keeps its own Dutch defaults (`messages.nl.ts`) as a fallback and a test checks that both say the same. Parts that come later (offerings) get their own part of the block. + +Not here: the hero, the privacy statement and everything from the CMS. ## Importing the language files diff --git a/web/src/components/ContactForm/ContactForm.tsx b/web/src/components/ContactForm/ContactForm.tsx index e57be47..abcb25c 100644 --- a/web/src/components/ContactForm/ContactForm.tsx +++ b/web/src/components/ContactForm/ContactForm.tsx @@ -25,6 +25,7 @@ import { import { contactClient } from '@/contact/contactClient'; import type { EmailAddress } from '@/content/types'; import useEmailAddress from '@/hooks/useEmailAddress'; +import { contactMessages } from '@/i18n/cmsClientMessages'; import { t } from '@/i18n'; import { mailtoLink } from '@/lib/mailLink'; import { trackContactEvent } from '@/lib/trackEvent'; @@ -108,6 +109,8 @@ export default function ContactForm({ token, refreshToken, fallbackAddress: address ?? '', + // The texts of the form come from the language file + messages: contactMessages, onEvent: (event) => trackContactEvent( event === 'sent' ? 'contact-sent' : 'contact-failed', diff --git a/web/src/i18n/cmsClientMessages.test.ts b/web/src/i18n/cmsClientMessages.test.ts new file mode 100644 index 0000000..4dca970 --- /dev/null +++ b/web/src/i18n/cmsClientMessages.test.ts @@ -0,0 +1,33 @@ +import { describe, expect, it } from 'vitest'; +import { messagesNl } from '@/cms-client/contact'; +import { contactMessages } from './cmsClientMessages'; + +// The site gives the form its texts from nl.json; the package keeps its own +// Dutch ones as a fallback. They have to say the same. +describe('contactMessages (from nl.json)', () => { + it('has the same field errors, fallback, limits and labels as the package', () => { + expect(contactMessages.fieldErrors).toEqual(messagesNl.fieldErrors); + expect(contactMessages.fallbackFieldError).toBe( + messagesNl.fallbackFieldError, + ); + expect(contactMessages.tooLarge).toBe(messagesNl.tooLarge); + expect(contactMessages.captchaRequired).toBe( + messagesNl.captchaRequired, + ); + expect(contactMessages.sendingLabel).toBe(messagesNl.sendingLabel); + }); + + it('says the same about the waiting time, singular and plural', () => { + for (const minutes of [1, 2, 5, 60]) { + expect(contactMessages.rateLimited(minutes)).toBe( + messagesNl.rateLimited(minutes), + ); + } + }); + + it('says the same when sending failed, with the address', () => { + expect(contactMessages.failed('info@voorbeeld.nl')).toBe( + messagesNl.failed('info@voorbeeld.nl'), + ); + }); +}); diff --git a/web/src/i18n/cmsClientMessages.ts b/web/src/i18n/cmsClientMessages.ts new file mode 100644 index 0000000..d38161a --- /dev/null +++ b/web/src/i18n/cmsClientMessages.ts @@ -0,0 +1,20 @@ +import type { ContactMessages } from '@/cms-client/contact'; +import { messages, tPlural, t } from './index.ts'; + +// The texts of the form of the shared cms-client package, from the language +// file (block cmsClient.contact), in the shape the package asks for +// (useContactForm({ messages })). The package keeps its own Dutch defaults +// (messages.nl.ts) as a fallback; a test checks that these are the same, so +// the two cannot drift apart unseen. +const contact = messages.cmsClient.contact; + +export const contactMessages: ContactMessages = { + fieldErrors: contact.fieldErrors, + fallbackFieldError: contact.fallbackFieldError, + rateLimited: (minutes) => + tPlural('cmsClient.contact.rateLimited', minutes, { minutes }), + tooLarge: contact.tooLarge, + captchaRequired: contact.captchaRequired, + failed: (address) => t('cmsClient.contact.failed', { address }), + sendingLabel: contact.sendingLabel, +}; diff --git a/web/src/i18n/i18n.test.ts b/web/src/i18n/i18n.test.ts index 0e7403e..4010703 100644 --- a/web/src/i18n/i18n.test.ts +++ b/web/src/i18n/i18n.test.ts @@ -96,6 +96,7 @@ describe('nl.json', () => { 'instagram', 'testimonials', 'seo', + 'cmsClient', ]); }); }); diff --git a/web/src/i18n/nl.json b/web/src/i18n/nl.json index 894e8cf..50e6b4b 100644 --- a/web/src/i18n/nl.json +++ b/web/src/i18n/nl.json @@ -887,5 +887,40 @@ "description": "Lees hoe Andrea Slop Fotografie omgaat met je persoonsgegevens en met de foto's die tijdens een fotoshoot gemaakt worden." } } + }, + "cmsClient": { + "contact": { + "fieldErrors": { + "name": { + "required": "Vul je naam in.", + "tooLong": "De naam is te lang.", + "invalid": "Deze naam is ongeldig." + }, + "email": { + "required": "Vul je e-mailadres in.", + "tooLong": "Het e-mailadres is te lang.", + "invalid": "Dit e-mailadres is ongeldig." + }, + "subject": { + "required": "Kies een onderwerp.", + "tooLong": "Dit onderwerp is te lang.", + "invalid": "Kies een onderwerp." + }, + "message": { + "required": "Schrijf hier je vraag.", + "tooLong": "Het bericht is te lang.", + "invalid": "Dit bericht bevat ongeldige tekens." + } + }, + "fallbackFieldError": "Controleer dit veld.", + "rateLimited": { + "one": "Je hebt net al een bericht gestuurd. Probeer het over {minutes} minuut opnieuw.", + "other": "Je hebt net al een bericht gestuurd. Probeer het over {minutes} minuten opnieuw." + }, + "tooLarge": "Je bericht is te groot.", + "captchaRequired": "Bevestig eerst dat je geen robot bent.", + "failed": "Versturen lukte niet. Probeer het later nog eens of mail naar {address}.", + "sendingLabel": "Versturen…" + } } }