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:
Do Siki
2026-09-29 00:52:26 +02:00
co-authored by Claude Opus 5.5
parent 9b5ca94030
commit 829f543962
21 changed files with 839 additions and 111 deletions
+131
View File
@@ -0,0 +1,131 @@
import { collectLocalizedPaths, topLevelKeys, unwrapLocalized, wrapLocalized } from './localize-paths'
import { Home } from '../globals/Home'
import { Common } from '../globals/Common'
type Doc = Record<string, unknown>
const FIELDS = [
{
name: 'hero',
type: 'group',
fields: [
{ name: 'title', type: 'text', localized: true },
{ name: 'bullets', type: 'array', localized: true, fields: [{ name: 'value', type: 'text' }] },
],
},
{
name: 'items',
type: 'array',
fields: [
{ name: 'icon', type: 'text' },
{ name: 'title', type: 'text', localized: true },
{ name: 'features', type: 'array', localized: true, fields: [{ name: 'value', type: 'text' }] },
],
},
{ name: 'address', type: 'text' },
{ name: 'external', type: 'checkbox' },
]
function legacyDoc(): Doc {
return {
hero: { title: 'Cím', bullets: [{ id: 'b1', value: 'Egy' }] },
items: [
{ id: 'r1', icon: 'star', title: 'Első', features: [{ id: 'f1', value: 'A' }] },
{ id: 'r2', icon: 'bolt', title: 'Második', features: [] },
],
address: 'Budapest',
external: false,
}
}
describe('collectLocalizedPaths', () => {
it('should collect localized leaves through groups and shared array rows', () => {
expect(collectLocalizedPaths(FIELDS)).toEqual([
['hero', 'title'],
['hero', 'bullets'],
['items', '[]', 'title'],
['items', '[]', 'features'],
])
})
it('should throw on layout fields that could hide localized data', () => {
expect(() => collectLocalizedPaths([{ type: 'tabs' }])).toThrow('unsupported field type "tabs"')
})
it('should derive paths from the real Global configs', () => {
const homePaths = collectLocalizedPaths(Home.fields as never)
expect(homePaths).toContainEqual(['hero', 'trustBullets'])
expect(homePaths).toContainEqual(['services', 'items', '[]', 'features'])
expect(homePaths).not.toContainEqual(['about', 'usps', '[]', 'icon'])
expect(collectLocalizedPaths(Common.fields as never)).not.toContainEqual(['footer', 'address'])
})
})
describe('wrapLocalized', () => {
const paths = collectLocalizedPaths(FIELDS)
it('should wrap strings and arrays under the hu locale and keep shared values', () => {
const doc = legacyDoc()
const changed = wrapLocalized(doc, paths)
expect(changed).toBe(6)
expect(doc.hero).toEqual({ title: { hu: 'Cím' }, bullets: { hu: [{ id: 'b1', value: 'Egy' }] } })
expect(doc.items).toEqual([
{ id: 'r1', icon: 'star', title: { hu: 'Első' }, features: { hu: [{ id: 'f1', value: 'A' }] } },
{ id: 'r2', icon: 'bolt', title: { hu: 'Második' }, features: { hu: [] } },
])
expect(doc.address).toBe('Budapest')
expect(doc.external).toBe(false)
})
it('should be idempotent on already migrated data', () => {
const doc = legacyDoc()
wrapLocalized(doc, paths)
const snapshot = JSON.stringify(doc)
expect(wrapLocalized(doc, paths)).toBe(0)
expect(JSON.stringify(doc)).toBe(snapshot)
})
it('should leave existing locale maps (e.g. Common.buttons) untouched', () => {
const doc: Doc = { hero: { title: { hu: 'Cím', en: 'Title' } } }
expect(wrapLocalized(doc, paths)).toBe(0)
expect(doc).toEqual({ hero: { title: { hu: 'Cím', en: 'Title' } } })
})
it('should skip missing branches and null values', () => {
const doc: Doc = { hero: { title: null }, items: null }
expect(wrapLocalized(doc, paths)).toBe(0)
expect(doc).toEqual({ hero: { title: null }, items: null })
})
it('should not descend into a row path when the stored value is not an array', () => {
const doc: Doc = { items: { unexpected: true } }
expect(wrapLocalized(doc, paths)).toBe(0)
})
})
describe('unwrapLocalized', () => {
const paths = collectLocalizedPaths(FIELDS)
it('should restore the legacy shape from the hu value', () => {
const doc = legacyDoc()
wrapLocalized(doc, paths)
;(doc.hero as Doc).title = { hu: 'Cím', en: 'Title' }
expect(unwrapLocalized(doc, paths)).toBe(6)
expect(doc).toEqual(legacyDoc())
})
it('should fall back to null when the hu value is missing', () => {
const doc: Doc = { hero: { title: { en: 'Title' } } }
unwrapLocalized(doc, paths)
expect(doc).toEqual({ hero: { title: null } })
})
})
describe('topLevelKeys', () => {
it('should return unique first segments', () => {
expect(topLevelKeys(collectLocalizedPaths(FIELDS))).toEqual(['hero', 'items'])
})
})