From 32b6643fbc91336e6b9e4f2a0b1577775d1b19a5 Mon Sep 17 00:00:00 2001 From: Do Siki Date: Tue, 29 Sep 2026 10:58:07 +0200 Subject: [PATCH] feat(cms): English translation status with a public gate (MITHOME-113) Every page global and legal page gets a shared "English translation status" sidebar field (none / AI draft awaiting review / approved). The content adapter reads through readGated(): an English page renders English text only when its translation is approved, otherwise the Hungarian text of the same page, while links keep resolving to /en/... Co-Authored-By: Claude Opus 5.5 --- .../english-content-mithome-112-113/tasks.md | 4 +- proto/src/collections/LegalPages.ts | 2 + proto/src/globals/About.ts | 2 + proto/src/globals/Common.ts | 2 + proto/src/globals/Contact.ts | 2 + proto/src/globals/Home.ts | 2 + proto/src/globals/Services.ts | 2 + proto/src/globals/fields/translationStatus.ts | 35 ++++++++++++++++ proto/src/lib/payload-content.ts | 29 ++++++++----- proto/src/lib/translation-gate.test.ts | 42 +++++++++++++++++++ proto/src/lib/translation-gate.ts | 33 +++++++++++++++ proto/src/payload-config.test.ts | 18 ++++++++ 12 files changed, 160 insertions(+), 13 deletions(-) create mode 100644 proto/src/globals/fields/translationStatus.ts create mode 100644 proto/src/lib/translation-gate.test.ts create mode 100644 proto/src/lib/translation-gate.ts diff --git a/openspec/changes/english-content-mithome-112-113/tasks.md b/openspec/changes/english-content-mithome-112-113/tasks.md index 091d304..daf0b0c 100644 --- a/openspec/changes/english-content-mithome-112-113/tasks.md +++ b/openspec/changes/english-content-mithome-112-113/tasks.md @@ -7,8 +7,8 @@ ## 2. MITHOME-113 — állapotmező és kapu -- [ ] 2.1 A `translationStatusField()` elkészítése (`none`/`ai-draft`/`approved`, oldalsáv, figyelmeztető leírás), felvétele az 5 Globalra és a LegalPages-re, valamint a típusok újragenerálása. Ellenőrzés: config teszt (a mező mindenhol ott van, nem lokalizált, alapértéke `none`). -- [ ] 2.2 A kapu elkészítése a `payload-content.ts`-ben: `en` + nem `approved` esetén `hu` tartalom, `en` útvonalakkal. A logika tiszta függvénybe kerül, unit tesztekkel (approved → en, ai-draft/none/hiányzó → hu, a hu kérést nem érinti). Ellenőrzés: `npm test`. +- [x] 2.1 A `translationStatusField()` elkészítése (`none`/`ai-draft`/`approved`, oldalsáv, figyelmeztető leírás), felvétele az 5 Globalra és a LegalPages-re, valamint a típusok újragenerálása. Ellenőrzés: config teszt (a mező mindenhol ott van, nem lokalizált, alapértéke `none`). +- [x] 2.2 A kapu elkészítése a `payload-content.ts`-ben: `en` + nem `approved` esetén `hu` tartalom, `en` útvonalakkal. A logika tiszta függvénybe kerül, unit tesztekkel (approved → en, ai-draft/none/hiányzó → hu, a hu kérést nem érinti). Ellenőrzés: `npm test`. ## 3. MITHOME-113 — AI-draft angol tartalom diff --git a/proto/src/collections/LegalPages.ts b/proto/src/collections/LegalPages.ts index 9c3e9d2..8dd0963 100644 --- a/proto/src/collections/LegalPages.ts +++ b/proto/src/collections/LegalPages.ts @@ -1,6 +1,7 @@ import type { CollectionConfig } from 'payload' import { collectionVersions } from '../lib/payload-versions' +import { translationStatusField } from '../globals/fields/translationStatus' /** * Mirrors LegalPageContent (proto/src/content/types.ts) — MITHOME-88. @@ -55,5 +56,6 @@ export const LegalPages: CollectionConfig = { { name: 'content', type: 'textarea', required: true, localized: true }, ], }, + translationStatusField(), ], } diff --git a/proto/src/globals/About.ts b/proto/src/globals/About.ts index 7fd5094..225f692 100644 --- a/proto/src/globals/About.ts +++ b/proto/src/globals/About.ts @@ -2,6 +2,7 @@ import type { GlobalConfig } from 'payload' import { stringArrayField } from './fields/stringArray' import { globalVersions } from '../lib/payload-versions' +import { translationStatusField } from './fields/translationStatus' /** Mirrors AboutPageContent (proto/src/content/types.ts) — MITHOME-87. */ export const About: GlobalConfig = { @@ -71,5 +72,6 @@ export const About: GlobalConfig = { { name: 'button', type: 'text', required: true, localized: true }, ], }, + translationStatusField(), ], } diff --git a/proto/src/globals/Common.ts b/proto/src/globals/Common.ts index e040b34..873781f 100644 --- a/proto/src/globals/Common.ts +++ b/proto/src/globals/Common.ts @@ -1,6 +1,7 @@ import type { GlobalConfig } from 'payload' import { globalVersions } from '../lib/payload-versions' +import { translationStatusField } from './fields/translationStatus' /** * Mirrors CommonContent (proto/src/content/types.ts) — MITHOME-87. @@ -82,5 +83,6 @@ export const Common: GlobalConfig = { { name: 'closeMenu', type: 'text', required: true, localized: true }, ], }, + translationStatusField(), ], } diff --git a/proto/src/globals/Contact.ts b/proto/src/globals/Contact.ts index 683a7cc..3491422 100644 --- a/proto/src/globals/Contact.ts +++ b/proto/src/globals/Contact.ts @@ -1,6 +1,7 @@ import type { GlobalConfig } from 'payload' import { globalVersions } from '../lib/payload-versions' +import { translationStatusField } from './fields/translationStatus' /** * Mirrors ContactPageContent (proto/src/content/types.ts) — MITHOME-87. @@ -141,5 +142,6 @@ export const Contact: GlobalConfig = { }, ], }, + translationStatusField(), ], } diff --git a/proto/src/globals/Home.ts b/proto/src/globals/Home.ts index e131475..9755f1d 100644 --- a/proto/src/globals/Home.ts +++ b/proto/src/globals/Home.ts @@ -3,6 +3,7 @@ import type { GlobalConfig } from 'payload' import { stringArrayField } from './fields/stringArray' import { ctaLinkGroup } from './fields/ctaLink' import { globalVersions } from '../lib/payload-versions' +import { translationStatusField } from './fields/translationStatus' /** * Mirrors HomePageContent (proto/src/content/types.ts) — MITHOME-87. @@ -96,5 +97,6 @@ export const Home: GlobalConfig = { type: 'group', fields: [{ name: 'title', type: 'text', required: true, localized: true }], }, + translationStatusField(), ], } diff --git a/proto/src/globals/Services.ts b/proto/src/globals/Services.ts index 533f024..883a274 100644 --- a/proto/src/globals/Services.ts +++ b/proto/src/globals/Services.ts @@ -2,6 +2,7 @@ import type { GlobalConfig } from 'payload' import { stringArrayField } from './fields/stringArray' import { globalVersions } from '../lib/payload-versions' +import { translationStatusField } from './fields/translationStatus' /** Mirrors ServicesPageContent (proto/src/content/types.ts) — MITHOME-87. */ export const Services: GlobalConfig = { @@ -80,5 +81,6 @@ export const Services: GlobalConfig = { { name: 'secondaryButton', type: 'text', required: true, localized: true }, ], }, + translationStatusField(), ], } diff --git a/proto/src/globals/fields/translationStatus.ts b/proto/src/globals/fields/translationStatus.ts new file mode 100644 index 0000000..ed1a6ad --- /dev/null +++ b/proto/src/globals/fields/translationStatus.ts @@ -0,0 +1,35 @@ +import type { SelectField, SelectFieldSingleValidation } from 'payload' + +import { EN_TRANSLATION_STATUSES, type EnTranslationStatus } from '../../lib/translation-gate' + +const LABELS: Record = { + none: 'Nincs fordítás', + 'ai-draft': 'AI-draft, átnézésre vár', + approved: 'Jóváhagyva', +} + +/** + * MITHOME-113: English translation status of a page, shown in the admin + * sidebar. The public English page only shows English content once this is + * "approved" (src/lib/translation-gate.ts); until then it shows Hungarian. + * + * WHY not localized: only English has a translation status — a localized + * field would show a meaningless value in the Hungarian (source) view. + * `validate` lets LegalPages add its multi-model validation rule. + */ +export function translationStatusField(validate?: SelectFieldSingleValidation): SelectField { + return { + name: 'enTranslationStatus', + label: 'Angol fordítás állapota', + type: 'select', + defaultValue: 'none', + options: EN_TRANSLATION_STATUSES.map((value) => ({ label: LABELS[value], value })), + ...(validate ? { validate } : {}), + admin: { + position: 'sidebar', + description: + 'Az angol oldal csak „Jóváhagyva” állapotban jelenik meg; addig a látogatók a magyar szöveget látják. ' + + 'Az AI-draft gépi fordítás: jóváhagyás előtt ellenőrizni és szükség szerint javítani kell.', + }, + } +} diff --git a/proto/src/lib/payload-content.ts b/proto/src/lib/payload-content.ts index 0e5db62..abb4ac1 100644 --- a/proto/src/lib/payload-content.ts +++ b/proto/src/lib/payload-content.ts @@ -7,11 +7,16 @@ * Ezek a getterek visszaadaptálják az eredeti alakra, hogy a page * komponensek JSX-e (ami a régi content/types.ts formát várja) NE * változzon — csak az adatforrás. + * + * MITHOME-113: minden olvasás a fordítási kapun megy át (readGated) — az + * angol oldal csak jóváhagyott angol fordítást mutat, különben a magyart. + * A `locale` paraméter a linkfeloldáshoz (CTA) változatlanul a kért nyelv. */ import { getPayload } from 'payload' import config from '@payload-config' import type { Locale } from './i18n' import { resolveCtaHref, type CtaLink } from './cta-link' +import { readGated } from './translation-gate' let cached: ReturnType | undefined @@ -31,12 +36,12 @@ function unwrap(items: readonly { value: string }[] | null | undefined): string[ export async function getCommonContent(locale: Locale) { const payload = await payloadClient() - return payload.findGlobal({ slug: 'common', locale }) + return readGated(locale, (l) => payload.findGlobal({ slug: 'common', locale: l })) } export async function getHomeContent(locale: Locale) { const payload = await payloadClient() - const home = await payload.findGlobal({ slug: 'home', locale }) + const home = await readGated(locale, (l) => payload.findGlobal({ slug: 'home', locale: l })) return { ...home, hero: { @@ -59,7 +64,7 @@ export async function getHomeContent(locale: Locale) { export async function getAboutContent(locale: Locale) { const payload = await payloadClient() - const about = await payload.findGlobal({ slug: 'about', locale }) + const about = await readGated(locale, (l) => payload.findGlobal({ slug: 'about', locale: l })) return { ...about, story: { ...about.story, paragraphs: unwrap(about.story?.paragraphs) }, @@ -69,7 +74,7 @@ export async function getAboutContent(locale: Locale) { export async function getServicesContent(locale: Locale) { const payload = await payloadClient() - const services = await payload.findGlobal({ slug: 'services', locale }) + const services = await readGated(locale, (l) => payload.findGlobal({ slug: 'services', locale: l })) return { ...services, details: { @@ -84,18 +89,20 @@ export async function getServicesContent(locale: Locale) { export async function getContactContent(locale: Locale) { const payload = await payloadClient() - return payload.findGlobal({ slug: 'contact', locale }) + return readGated(locale, (l) => payload.findGlobal({ slug: 'contact', locale: l })) } export async function getLegalPage(slug: 'adatvedelem' | 'hasznalati-feltetelek', locale: Locale) { const payload = await payloadClient() - const result = await payload.find({ - collection: 'legal-pages', - where: { slug: { equals: slug } }, - locale, - limit: 1, + return readGated(locale, async (l) => { + const result = await payload.find({ + collection: 'legal-pages', + where: { slug: { equals: slug } }, + locale: l, + limit: 1, + }) + return result.docs[0] }) - return result.docs[0] } export type PartnerView = { name: string; url: string; logo: { url: string; alt: string } } diff --git a/proto/src/lib/translation-gate.test.ts b/proto/src/lib/translation-gate.test.ts new file mode 100644 index 0000000..59672bd --- /dev/null +++ b/proto/src/lib/translation-gate.test.ts @@ -0,0 +1,42 @@ +import { contentLocale, readGated } from './translation-gate' + +describe('contentLocale (MITHOME-113)', () => { + it('should show English only when the translation is approved', () => { + expect(contentLocale('en', 'approved')).toBe('en') + }) + + it.each(['ai-draft', 'none', null, undefined])('should fall back to Hungarian for status %s', (status) => { + expect(contentLocale('en', status)).toBe('hu') + }) + + it('should never change a Hungarian request', () => { + expect(contentLocale('hu', 'ai-draft')).toBe('hu') + expect(contentLocale('hu', 'approved')).toBe('hu') + }) +}) + +describe('readGated', () => { + const store = { + en: { title: 'Contact', enTranslationStatus: 'ai-draft' }, + hu: { title: 'Kapcsolat', enTranslationStatus: 'ai-draft' }, + } + + it('should re-read in Hungarian when the English translation is not approved', async () => { + const read = jest.fn(async (locale: 'hu' | 'en') => store[locale]) + await expect(readGated('en', read)).resolves.toEqual(store.hu) + expect(read.mock.calls.map(([l]) => l)).toEqual(['en', 'hu']) + }) + + it('should read once when approved or when Hungarian is requested', async () => { + const approved = { title: 'Contact', enTranslationStatus: 'approved' } + const read = jest.fn(async () => approved) + await expect(readGated('en', read)).resolves.toBe(approved) + await readGated('hu', read) + expect(read).toHaveBeenCalledTimes(2) + }) + + it('should pass a missing document through', async () => { + const read = jest.fn(async () => undefined) + await expect(readGated('en', read)).resolves.toBeUndefined() + }) +}) diff --git a/proto/src/lib/translation-gate.ts b/proto/src/lib/translation-gate.ts new file mode 100644 index 0000000..b0834d3 --- /dev/null +++ b/proto/src/lib/translation-gate.ts @@ -0,0 +1,33 @@ +/** + * MITHOME-113: public gate for English content. + * + * An English page renders English content only when its translation status + * is "approved"; otherwise it renders the Hungarian content of the same + * page. Only the text source changes — callers keep the requested locale + * for routing (CTA/nav links stay /en/...). + */ +import { DEFAULT_LOCALE, type Locale } from './i18n' + +export const EN_TRANSLATION_STATUSES = ['none', 'ai-draft', 'approved'] as const +export type EnTranslationStatus = (typeof EN_TRANSLATION_STATUSES)[number] + +type Gated = { enTranslationStatus?: string | null } + +/** The locale whose stored text should be shown for `locale`. */ +export function contentLocale(locale: Locale, status: string | null | undefined): Locale { + return locale === 'en' && status !== 'approved' ? DEFAULT_LOCALE : locale +} + +/** + * Reads a document in `locale` and, if its English translation is not + * approved, reads it again in the default locale. + * TRADEOFF: a second in-process Local API read for unapproved English pages. + */ +export async function readGated( + locale: Locale, + read: (locale: Locale) => Promise, +): Promise { + const doc = await read(locale) + const source = contentLocale(locale, doc?.enTranslationStatus) + return source === locale ? doc : read(source) +} diff --git a/proto/src/payload-config.test.ts b/proto/src/payload-config.test.ts index fbc1151..65e3375 100644 --- a/proto/src/payload-config.test.ts +++ b/proto/src/payload-config.test.ts @@ -221,3 +221,21 @@ describe('Payload globals', () => { }) }) }) + +describe('MITHOME-113 English translation status', () => { + it.each([Home, About, Services, Contact, Common, LegalPages].map((d) => [d.slug, d] as const))( + 'should give %s a shared sidebar status that defaults to "none"', + (_slug, doc) => { + const field = findField(doc.fields, 'enTranslationStatus') as unknown as { + type: string + localized?: boolean + defaultValue: string + options: { value: string }[] + admin: { position: string } + } + expect(field).toMatchObject({ type: 'select', defaultValue: 'none', admin: { position: 'sidebar' } }) + expect(field.localized).toBeUndefined() + expect(field.options.map((o) => o.value)).toEqual(['none', 'ai-draft', 'approved']) + }, + ) +})