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 <noreply@anthropic.com>
This commit is contained in:
Do Siki
2026-09-29 10:58:07 +02:00
co-authored by Claude Opus 5.5
parent 0eb6594acc
commit 32b6643fbc
12 changed files with 160 additions and 13 deletions
@@ -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
+2
View File
@@ -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(),
],
}
+2
View File
@@ -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(),
],
}
+2
View File
@@ -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(),
],
}
+2
View File
@@ -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(),
],
}
+2
View File
@@ -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(),
],
}
+2
View File
@@ -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(),
],
}
@@ -0,0 +1,35 @@
import type { SelectField, SelectFieldSingleValidation } from 'payload'
import { EN_TRANSLATION_STATUSES, type EnTranslationStatus } from '../../lib/translation-gate'
const LABELS: Record<EnTranslationStatus, string> = {
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.',
},
}
}
+13 -6
View File
@@ -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<typeof getPayload> | 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()
return readGated(locale, async (l) => {
const result = await payload.find({
collection: 'legal-pages',
where: { slug: { equals: slug } },
locale,
locale: l,
limit: 1,
})
return result.docs[0]
})
}
export type PartnerView = { name: string; url: string; logo: { url: string; alt: string } }
+42
View File
@@ -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()
})
})
+33
View File
@@ -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<T extends Gated | undefined>(
locale: Locale,
read: (locale: Locale) => Promise<T>,
): Promise<T> {
const doc = await read(locale)
const source = contentLocale(locale, doc?.enTranslationStatus)
return source === locale ? doc : read(source)
}
+18
View File
@@ -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'])
},
)
})