feat(cms): localize translatable Globals fields with lossless migration (MITHOME-111)
Mark every translatable text/textarea field of the Home, About, Services,
Contact and Common globals as localized. Icons, row ids, the external
flag and the postal address stay shared; CTA hrefs are localized because
internal routes differ per locale. String lists (paragraphs, bullets,
features, spec items) are localized at array level so locales can have
different item counts; structured rows share order/icons and localize
only their inner text.
Existing data is not re-seeded (that would overwrite admin edits): a
Payload migration wraps legacy values into { hu: value } on the published
global and every draft/version record, idempotently, and runs on
production startup via prodMigrations. Paths are derived from the Global
configs themselves so the migration cannot drift from the schema.
OpenSpec change: openspec/changes/localize-globals-mithome-111
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
9b5ca94030
commit
829f543962
@@ -142,4 +142,55 @@ describe('Payload globals', () => {
|
||||
expect(field.localized).toBe(true)
|
||||
}
|
||||
})
|
||||
|
||||
describe('MITHOME-111 localization rule', () => {
|
||||
// DECISION (design D1): structural values shared across locales.
|
||||
const SHARED_TEXT_FIELDS = new Set(['icon', 'id', 'address'])
|
||||
|
||||
type Node = FieldLike & { localized?: boolean; fields?: Node[] }
|
||||
type Finding = { path: string; localized: boolean }
|
||||
|
||||
const isStringArray = (f: Node) =>
|
||||
f.type === 'array' && f.fields?.length === 1 && f.fields[0].name === 'value'
|
||||
|
||||
function walk(fields: Node[], prefix: string, out: Finding[]): Finding[] {
|
||||
for (const f of fields) {
|
||||
const path = `${prefix}${f.name}`
|
||||
if (isStringArray(f)) {
|
||||
out.push({ path: `${path}[]`, localized: f.localized === true })
|
||||
} else if (f.type === 'group' || f.type === 'array') {
|
||||
// Object arrays/groups themselves stay shared; only their leaves localize.
|
||||
out.push({ path: `${path}(container)`, localized: f.localized === true })
|
||||
walk(f.fields ?? [], `${path}.`, out)
|
||||
} else {
|
||||
out.push({ path, localized: f.localized === true })
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
function expected(finding: Finding): boolean {
|
||||
if (finding.path.endsWith('(container)')) return false
|
||||
if (finding.path.endsWith('[]')) return true
|
||||
const leaf = finding.path.split('.').pop()!
|
||||
if (leaf === 'external') return false
|
||||
return !SHARED_TEXT_FIELDS.has(leaf)
|
||||
}
|
||||
|
||||
it.each([Home, About, Services, Contact, Common].map((g) => [g.slug, g] as const))(
|
||||
'should localize exactly the translatable fields of the %s global',
|
||||
(_slug, global) => {
|
||||
const findings = walk(global.fields as Node[], '', [])
|
||||
const wrong = findings.filter((f) => f.localized !== expected(f)).map((f) => f.path)
|
||||
expect(wrong).toEqual([])
|
||||
},
|
||||
)
|
||||
|
||||
it('should localize the locale-dependent CTA hrefs but share the external flag', () => {
|
||||
const findings = walk(Home.fields as Node[], '', [])
|
||||
const byPath = Object.fromEntries(findings.map((f) => [f.path, f.localized]))
|
||||
expect(byPath['hero.cta.primary.href']).toBe(true)
|
||||
expect(byPath['hero.cta.primary.external']).toBe(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user