@nocode-factory/sanity-kit 2.29.2 → 2.30.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/ncf-kit.mjs +192 -0
- package/dist/index.d.ts +14 -1
- package/dist/index.js +753 -278
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/actions/PropagatePublish.tsx +63 -64
- package/src/form/TranslationTodoInput.tsx +2 -0
- package/src/lib/translateDocument.ts +202 -0
- package/src/lib/translationTrace.ts +55 -0
- package/src/schema/translationTodo.ts +10 -0
- package/src/tools/pages/LanguagesSync.tsx +262 -0
- package/src/tools/pages/PagesTool.tsx +5 -0
- package/src/version.ts +1 -1
package/package.json
CHANGED
|
@@ -8,7 +8,8 @@ import {hasChanges, propagate, type PropagationResult, type SchemaTypeLike} from
|
|
|
8
8
|
import {TRANSLATION_TODO_TYPE, translationTodoId} from '../schema/translationTodo'
|
|
9
9
|
import {collectTexts, setAtKey, valueAtKey} from '../lib/aiTexts'
|
|
10
10
|
import {translateValues} from '../lib/aiTranslate'
|
|
11
|
-
import {
|
|
11
|
+
import {createTranslatedVersion, translationsOf} from '../lib/translateDocument'
|
|
12
|
+
import {isReplaceable, readTrace, recordTrace} from '../lib/translationTrace'
|
|
12
13
|
|
|
13
14
|
type Doc = Record<string, unknown>
|
|
14
15
|
type Client = ReturnType<typeof useKitClient>
|
|
@@ -18,6 +19,8 @@ interface Target {
|
|
|
18
19
|
id: string | null
|
|
19
20
|
result: PropagationResult
|
|
20
21
|
selected: boolean
|
|
22
|
+
/** Textes à mettre à jour mais modifiés à la main dans cette langue (gardés par défaut). */
|
|
23
|
+
edited: string[]
|
|
21
24
|
}
|
|
22
25
|
type Mode = 'update' | 'create'
|
|
23
26
|
|
|
@@ -72,7 +75,7 @@ export function withPropagation(
|
|
|
72
75
|
.then((missing) => {
|
|
73
76
|
if (!missing.length) return
|
|
74
77
|
const result = propagate({}, {}, after, documentType)
|
|
75
|
-
setRun({mode: 'create', after, targets: missing.map((language) => ({language, id: null, result, selected: true}))})
|
|
78
|
+
setRun({mode: 'create', after, targets: missing.map((language) => ({language, id: null, result, selected: true, edited: []}))})
|
|
76
79
|
})
|
|
77
80
|
.catch(() => {})
|
|
78
81
|
}
|
|
@@ -108,14 +111,6 @@ export function withPropagation(
|
|
|
108
111
|
return Wrapped
|
|
109
112
|
}
|
|
110
113
|
|
|
111
|
-
async function translationsOf(client: Client, id: string): Promise<{language: string; id: string}[]> {
|
|
112
|
-
const list = await client.fetch<{language?: string; key?: string; id?: string}[] | null>(
|
|
113
|
-
`*[_type == "translation.metadata" && references($id)][0].translations[]{language, "key": _key, "id": value._ref}`,
|
|
114
|
-
{id},
|
|
115
|
-
)
|
|
116
|
-
return (list ?? []).map((t) => ({language: t.language ?? t.key ?? '', id: t.id ?? ''})).filter((t) => t.language && t.id)
|
|
117
|
-
}
|
|
118
|
-
|
|
119
114
|
async function findTargets(client: Client, id: string, source: string, before: Doc, after: Doc, documentType: SchemaTypeLike | undefined): Promise<Target[]> {
|
|
120
115
|
const list: Target[] = []
|
|
121
116
|
for (const t of await translationsOf(client, id)) {
|
|
@@ -123,7 +118,13 @@ async function findTargets(client: Client, id: string, source: string, before: D
|
|
|
123
118
|
const target = (await client.getDocument<Doc>(`drafts.${t.id}`)) ?? (await client.getDocument<Doc>(t.id)) ?? null
|
|
124
119
|
if (!target) continue
|
|
125
120
|
const result = propagate(target, before, after, documentType)
|
|
126
|
-
|
|
121
|
+
const trace = await readTrace(client, t.id)
|
|
122
|
+
const edited = result.toTranslate.filter((path) => {
|
|
123
|
+
const src = valueAtKey(after, path)
|
|
124
|
+
if (!collectTexts(src).some((x) => x.trim())) return false
|
|
125
|
+
return !isReplaceable(valueAtKey(result.document, path), src, path, trace, (v) => !collectTexts(v).some((x) => x.trim()))
|
|
126
|
+
})
|
|
127
|
+
list.push({language: t.language, id: t.id, result, selected: hasChanges(result), edited})
|
|
127
128
|
}
|
|
128
129
|
return list
|
|
129
130
|
}
|
|
@@ -133,35 +134,6 @@ async function findMissing(client: Client, id: string, source: string, locales:
|
|
|
133
134
|
return locales.filter((l) => l !== source && !existing.has(l))
|
|
134
135
|
}
|
|
135
136
|
|
|
136
|
-
const randomId = () =>
|
|
137
|
-
typeof crypto !== 'undefined' && 'randomUUID' in crypto ? crypto.randomUUID() : `${Date.now().toString(36)}${Math.random().toString(36).slice(2)}`
|
|
138
|
-
|
|
139
|
-
/** Relie les nouvelles versions à la page (translation.metadata de document-internationalization). */
|
|
140
|
-
async function linkTranslations(client: Client, sourceId: string, sourceLanguage: string, type: string, created: {language: string; id: string}[]) {
|
|
141
|
-
if (!created.length) return
|
|
142
|
-
const item = (language: string, ref: string) => ({
|
|
143
|
-
_key: language,
|
|
144
|
-
_type: 'internationalizedArrayReferenceValue',
|
|
145
|
-
language,
|
|
146
|
-
value: {_type: 'reference', _ref: ref, _weak: true, _strengthenOnPublish: {type}},
|
|
147
|
-
})
|
|
148
|
-
const meta = await client.fetch<{_id: string} | null>(`*[_type == "translation.metadata" && references($id)][0]{_id}`, {id: sourceId})
|
|
149
|
-
if (meta) {
|
|
150
|
-
await client
|
|
151
|
-
.patch(meta._id)
|
|
152
|
-
.setIfMissing({translations: []})
|
|
153
|
-
.append('translations', created.map((c) => item(c.language, c.id)))
|
|
154
|
-
.commit()
|
|
155
|
-
} else {
|
|
156
|
-
await client.create({
|
|
157
|
-
_id: randomId(),
|
|
158
|
-
_type: 'translation.metadata',
|
|
159
|
-
schemaTypes: [type],
|
|
160
|
-
translations: [item(sourceLanguage, sourceId), ...created.map((c) => item(c.language, c.id))],
|
|
161
|
-
})
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
137
|
function PropagateDialog(props: {
|
|
166
138
|
config: ResolvedKitConfig
|
|
167
139
|
id: string
|
|
@@ -173,15 +145,18 @@ function PropagateDialog(props: {
|
|
|
173
145
|
}) {
|
|
174
146
|
const {config, id, type, mode, after, onDone} = props
|
|
175
147
|
const client = useKitClient()
|
|
148
|
+
const schema = useSchema()
|
|
176
149
|
const field = config.i18n?.field ?? 'language'
|
|
177
150
|
const source = String(after[field] ?? '')
|
|
178
|
-
const pageType = config.pageTypes.find((t) => t.type === type)
|
|
179
151
|
const [targets, setTargets] = useState<Target[]>(props.targets)
|
|
180
152
|
const [busy, setBusy] = useState(false)
|
|
181
153
|
const toast = useToast()
|
|
182
154
|
const [error, setError] = useState<string | null>(null)
|
|
183
155
|
// Traduction par l'IA (service du kit) et publication directe.
|
|
184
156
|
const [ai, setAi] = useState(Boolean(config.history))
|
|
157
|
+
// Textes modifiés à la main dans une langue : gardés, sauf demande explicite.
|
|
158
|
+
const [overwrite, setOverwrite] = useState(false)
|
|
159
|
+
const editedCount = targets.reduce((n, t) => n + (t.selected ? t.edited.length : 0), 0)
|
|
185
160
|
const hasTexts = targets.some((t) => t.result.toTranslate.length)
|
|
186
161
|
|
|
187
162
|
const apply = async () => {
|
|
@@ -190,41 +165,56 @@ function PropagateDialog(props: {
|
|
|
190
165
|
try {
|
|
191
166
|
const published: string[] = []
|
|
192
167
|
const drafted: string[] = []
|
|
193
|
-
const created: {language: string; id: string}[] = []
|
|
194
168
|
let aiError: string | null = null
|
|
195
169
|
for (const t of targets) {
|
|
196
170
|
if (!t.selected) continue
|
|
171
|
+
if (mode === 'create') {
|
|
172
|
+
const created = await createTranslatedVersion({
|
|
173
|
+
client,
|
|
174
|
+
config,
|
|
175
|
+
type,
|
|
176
|
+
schemaType: schema.get(type) as unknown as SchemaTypeLike | undefined,
|
|
177
|
+
source: after,
|
|
178
|
+
sourceId: id,
|
|
179
|
+
sourceLanguage: source,
|
|
180
|
+
language: t.language,
|
|
181
|
+
ai,
|
|
182
|
+
})
|
|
183
|
+
if (created.aiError) aiError = created.aiError
|
|
184
|
+
;(created.published ? published : drafted).push(t.language.toUpperCase())
|
|
185
|
+
continue
|
|
186
|
+
}
|
|
197
187
|
const {_id, _rev, _createdAt, _updatedAt, ...doc} = structuredClone(t.result.document) as Doc
|
|
198
|
-
const targetId = t.id
|
|
188
|
+
const targetId = t.id as string
|
|
189
|
+
// Textes modifiés à la main dans cette langue : gardés (et marqués « To
|
|
190
|
+
// translate », texte source affiché), sauf case « Overwrite » cochée.
|
|
191
|
+
const kept = overwrite ? [] : t.edited
|
|
199
192
|
let remaining = t.result.toTranslate
|
|
200
|
-
let
|
|
193
|
+
let failed = false
|
|
194
|
+
const written: {path: string; value: unknown}[] = []
|
|
201
195
|
if (ai && config.history && remaining.length) {
|
|
196
|
+
const toDo = remaining.filter((p) => !kept.includes(p))
|
|
202
197
|
try {
|
|
203
|
-
const values =
|
|
198
|
+
const values = toDo.map((path) => valueAtKey(after, path))
|
|
204
199
|
// Texte vidé dans la version principale : vidé aussi (suit la valeur par défaut).
|
|
205
200
|
const toSend = values.map((v) => (collectTexts(v).some((s) => s.trim()) ? v : undefined))
|
|
206
201
|
const translated = await translateValues(config.history, source, t.language, toSend.map((v) => v ?? ''))
|
|
207
|
-
|
|
208
|
-
|
|
202
|
+
const done = new Set<string>()
|
|
203
|
+
toDo.forEach((path, n) => {
|
|
204
|
+
const value = toSend[n] === undefined ? undefined : translated[n]
|
|
205
|
+
if (setAtKey(doc, path, value)) {
|
|
206
|
+
done.add(path)
|
|
207
|
+
if (value !== undefined) written.push({path, value})
|
|
208
|
+
}
|
|
209
|
+
})
|
|
210
|
+
remaining = remaining.filter((p) => !done.has(p))
|
|
209
211
|
} catch (cause) {
|
|
212
|
+
failed = true
|
|
210
213
|
aiError = cause instanceof Error ? cause.message : String(cause)
|
|
211
214
|
}
|
|
212
215
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
const slugField = pageType?.slugField
|
|
216
|
-
const titleField = pageType?.titleField ?? 'title'
|
|
217
|
-
const title = typeof doc[titleField] === 'string' ? (doc[titleField] as string) : ''
|
|
218
|
-
const sourceSlug = slugField ? (after[slugField] as {current?: string} | undefined)?.current : undefined
|
|
219
|
-
if (slugField && (title || sourceSlug)) {
|
|
220
|
-
// Slug unique dans la langue : suffixe -2, -3... si déjà pris.
|
|
221
|
-
const base = slugify(title) || String(sourceSlug)
|
|
222
|
-
let slug = base
|
|
223
|
-
for (let n = 2; await client.fetch<boolean>(`count(*[_type == $type && ${slugField}.current == $slug && ${field} == $language && !(_id in path("drafts.**"))]) > 0`, {type, slug, language: t.language}); n++) slug = `${base}-${n}`
|
|
224
|
-
doc[slugField] = {_type: 'slug', current: slug}
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
const publish = ai && translatedOk
|
|
216
|
+
// Publié si l'IA a tout traduit (les textes gardés à la main ne bloquent pas).
|
|
217
|
+
const publish = ai && !failed && remaining.every((p) => kept.includes(p))
|
|
228
218
|
const tx = client.transaction()
|
|
229
219
|
if (publish) {
|
|
230
220
|
tx.createOrReplace({...doc, _id: targetId, _type: type} as {_id: string; _type: string})
|
|
@@ -240,10 +230,9 @@ function PropagateDialog(props: {
|
|
|
240
230
|
const paths = [...new Set([...existing.filter((p) => !done.has(p)), ...remaining])]
|
|
241
231
|
if (paths.length || existing.length)
|
|
242
232
|
await client.createOrReplace({_id: todoId, _type: TRANSLATION_TODO_TYPE, documentId: targetId, sourceId: id, sourceLanguage: source, paths})
|
|
243
|
-
|
|
233
|
+
await recordTrace(client, targetId, written, {sourceId: id, sourceLanguage: source})
|
|
244
234
|
;(publish ? published : drafted).push(t.language.toUpperCase())
|
|
245
235
|
}
|
|
246
|
-
await linkTranslations(client, id, source, type, created)
|
|
247
236
|
// Fini : la fenêtre se ferme seule, le résultat s'affiche en notification.
|
|
248
237
|
toast.push({
|
|
249
238
|
status: aiError ? 'warning' : 'success',
|
|
@@ -279,6 +268,15 @@ function PropagateDialog(props: {
|
|
|
279
268
|
<Text size={1}>Translate with AI and publish</Text>
|
|
280
269
|
</Flex>
|
|
281
270
|
)}
|
|
271
|
+
{mode === 'update' && editedCount > 0 && (
|
|
272
|
+
<Flex as="label" align="center" gap={2}>
|
|
273
|
+
<Checkbox checked={overwrite} onChange={() => setOverwrite(!overwrite)} />
|
|
274
|
+
<Text size={1}>
|
|
275
|
+
Overwrite {editedCount} text{editedCount > 1 ? 's' : ''} edited by hand in other languages (kept otherwise, marked
|
|
276
|
+
"To translate")
|
|
277
|
+
</Text>
|
|
278
|
+
</Flex>
|
|
279
|
+
)}
|
|
282
280
|
<Stack gap={3}>
|
|
283
281
|
{targets.map((t, index) => (
|
|
284
282
|
<Card key={`${t.language}-${t.id ?? 'new'}`} padding={3} radius={2} border>
|
|
@@ -295,6 +293,7 @@ function PropagateDialog(props: {
|
|
|
295
293
|
</Text>
|
|
296
294
|
<Text size={1} muted>
|
|
297
295
|
{mode === 'create' ? `New version (${t.result.toTranslate.length} texts)` : summary(t.result)}
|
|
296
|
+
{t.edited.length ? ` · ${t.edited.length} edited by hand` : ''}
|
|
298
297
|
</Text>
|
|
299
298
|
</Stack>
|
|
300
299
|
</Box>
|
|
@@ -6,6 +6,7 @@ import {useKitClient} from '../hooks/useKitClient'
|
|
|
6
6
|
import {fieldPathKey, useTranslationTodo} from '../hooks/useTranslationTodo'
|
|
7
7
|
import {collectTexts, valueAtKey} from '../lib/aiTexts'
|
|
8
8
|
import {translateValues} from '../lib/aiTranslate'
|
|
9
|
+
import {recordTrace} from '../lib/translationTrace'
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* Sous un champ d'une traduction modifié dans la version principale (propagation) :
|
|
@@ -64,6 +65,7 @@ export function withTranslationTodo(Inner: ComponentType<InputProps>, config: Re
|
|
|
64
65
|
else {
|
|
65
66
|
const [translated] = await translateValues(config.history, todo.sourceLanguage, language, [value])
|
|
66
67
|
props.onChange(set(translated))
|
|
68
|
+
if (id) await recordTrace(client, id, [{path: key, value: translated}], {sourceId: todo.sourceId, sourceLanguage: todo.sourceLanguage})
|
|
67
69
|
}
|
|
68
70
|
await client
|
|
69
71
|
.patch(todo._id)
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import type {SanityClient} from 'sanity'
|
|
2
|
+
import type {ResolvedKitConfig} from '../config/types'
|
|
3
|
+
import {TRANSLATION_TODO_TYPE, translationTodoId} from '../schema/translationTodo'
|
|
4
|
+
import {collectTexts, setAtKey, valueAtKey} from './aiTexts'
|
|
5
|
+
import {translateValues} from './aiTranslate'
|
|
6
|
+
import {propagate, type SchemaTypeLike} from './propagate'
|
|
7
|
+
import {slugify} from './slugify'
|
|
8
|
+
import {recordTrace} from './translationTrace'
|
|
9
|
+
|
|
10
|
+
type Doc = Record<string, unknown>
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Versions traduites d'un document (création), partagé par la fenêtre de
|
|
14
|
+
* publication (« Create the other language versions? ») et la synchronisation
|
|
15
|
+
* des langues (onglet Pages > Languages) : même résultat dans les deux cas.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export const randomId = (): string =>
|
|
19
|
+
typeof crypto !== 'undefined' && 'randomUUID' in crypto
|
|
20
|
+
? crypto.randomUUID()
|
|
21
|
+
: `${Date.now().toString(36)}${Math.random().toString(36).slice(2)}`
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Identifiant de la nouvelle version. Document à identifiant lisible (singleton,
|
|
25
|
+
* `homePage`, `homePage-fr`, `componentDefaults`) : `<base>-<langue>`, la
|
|
26
|
+
* convention des Studios NCF (structure par langue). Sinon : identifiant aléatoire.
|
|
27
|
+
*/
|
|
28
|
+
export function translationIdFor(sourceId: string, sourceLanguage: string, language: string): string {
|
|
29
|
+
if (!/^[A-Za-z][A-Za-z0-9_]*(-[a-z]{2}(-[A-Za-z]{2})?)?$/.test(sourceId)) return randomId()
|
|
30
|
+
const base = sourceId.endsWith(`-${sourceLanguage}`) ? sourceId.slice(0, -sourceLanguage.length - 1) : sourceId
|
|
31
|
+
return `${base}-${language}`
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Traductions reliées d'un document (translation.metadata). */
|
|
35
|
+
export async function translationsOf(client: SanityClient, id: string): Promise<{language: string; id: string}[]> {
|
|
36
|
+
const list = await client.fetch<{language?: string; key?: string; id?: string}[] | null>(
|
|
37
|
+
`*[_type == "translation.metadata" && references($id)][0].translations[]{language, "key": _key, "id": value._ref}`,
|
|
38
|
+
{id},
|
|
39
|
+
)
|
|
40
|
+
return (list ?? []).map((t) => ({language: t.language ?? t.key ?? '', id: t.id ?? ''})).filter((t) => t.language && t.id)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Relie des versions au document source (translation.metadata de document-internationalization). */
|
|
44
|
+
export async function linkTranslations(
|
|
45
|
+
client: SanityClient,
|
|
46
|
+
sourceId: string,
|
|
47
|
+
sourceLanguage: string,
|
|
48
|
+
type: string,
|
|
49
|
+
created: {language: string; id: string}[],
|
|
50
|
+
): Promise<void> {
|
|
51
|
+
if (!created.length) return
|
|
52
|
+
const item = (language: string, ref: string) => ({
|
|
53
|
+
_key: language,
|
|
54
|
+
_type: 'internationalizedArrayReferenceValue',
|
|
55
|
+
language,
|
|
56
|
+
value: {_type: 'reference', _ref: ref, _weak: true, _strengthenOnPublish: {type}},
|
|
57
|
+
})
|
|
58
|
+
const meta = await client.fetch<{_id: string; keys?: string[]} | null>(
|
|
59
|
+
`*[_type == "translation.metadata" && references($id)][0]{_id, "keys": translations[]._key}`,
|
|
60
|
+
{id: sourceId},
|
|
61
|
+
)
|
|
62
|
+
if (meta) {
|
|
63
|
+
const fresh = created.filter((c) => !(meta.keys ?? []).includes(c.language))
|
|
64
|
+
if (!fresh.length) return
|
|
65
|
+
await client
|
|
66
|
+
.patch(meta._id)
|
|
67
|
+
.setIfMissing({translations: []})
|
|
68
|
+
.append('translations', fresh.map((c) => item(c.language, c.id)))
|
|
69
|
+
.commit()
|
|
70
|
+
} else {
|
|
71
|
+
await client.create({
|
|
72
|
+
_id: randomId(),
|
|
73
|
+
_type: 'translation.metadata',
|
|
74
|
+
schemaTypes: [type],
|
|
75
|
+
translations: [item(sourceLanguage, sourceId), ...created.map((c) => item(c.language, c.id))],
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Crée la version `language` de `source` : même structure et mêmes valeurs, textes
|
|
82
|
+
* traduits par l'IA si `ai` (service du kit), slug traduit et unique, publiée si
|
|
83
|
+
* tout est traduit (sinon brouillon et textes « To translate »). Relie la version.
|
|
84
|
+
*/
|
|
85
|
+
export async function createTranslatedVersion(opts: {
|
|
86
|
+
client: SanityClient
|
|
87
|
+
config: ResolvedKitConfig
|
|
88
|
+
type: string
|
|
89
|
+
schemaType: SchemaTypeLike | undefined
|
|
90
|
+
source: Doc
|
|
91
|
+
sourceId: string
|
|
92
|
+
sourceLanguage: string
|
|
93
|
+
language: string
|
|
94
|
+
ai: boolean
|
|
95
|
+
}): Promise<{id: string; published: boolean; aiError: string | null}> {
|
|
96
|
+
const {client, config, type, schemaType, source, sourceId, sourceLanguage, language, ai} = opts
|
|
97
|
+
const field = config.i18n?.field ?? 'language'
|
|
98
|
+
const result = propagate({}, {}, source, schemaType)
|
|
99
|
+
const {_id, _rev, _createdAt, _updatedAt, ...doc} = structuredClone(result.document) as Doc
|
|
100
|
+
const id = translationIdFor(sourceId, sourceLanguage, language)
|
|
101
|
+
let remaining = result.toTranslate
|
|
102
|
+
let aiError: string | null = null
|
|
103
|
+
const written: {path: string; value: unknown}[] = []
|
|
104
|
+
if (ai && config.history && remaining.length) {
|
|
105
|
+
try {
|
|
106
|
+
const values = remaining.map((path) => valueAtKey(source, path))
|
|
107
|
+
const toSend = values.map((v) => (collectTexts(v).some((s) => s.trim()) ? v : undefined))
|
|
108
|
+
const translated = await translateValues(config.history, sourceLanguage, language, toSend.map((v) => v ?? ''))
|
|
109
|
+
remaining = remaining.filter((path, n) => {
|
|
110
|
+
const value = toSend[n] === undefined ? undefined : translated[n]
|
|
111
|
+
const ok = setAtKey(doc, path, value)
|
|
112
|
+
if (ok && value !== undefined) written.push({path, value})
|
|
113
|
+
return !ok
|
|
114
|
+
})
|
|
115
|
+
} catch (cause) {
|
|
116
|
+
aiError = cause instanceof Error ? cause.message : String(cause)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
doc[field] = language
|
|
120
|
+
// Slug : traduit depuis le titre, unique dans la langue (-2, -3...).
|
|
121
|
+
const pageType = config.pageTypes.find((t) => t.type === type)
|
|
122
|
+
const slugField = pageType?.slugField ?? ((source.slug as {current?: string} | undefined)?.current ? 'slug' : null)
|
|
123
|
+
if (slugField && (source[slugField] as {current?: string} | undefined)?.current) {
|
|
124
|
+
const titleField = pageType?.titleField ?? 'title'
|
|
125
|
+
const title = typeof doc[titleField] === 'string' ? (doc[titleField] as string) : ''
|
|
126
|
+
const base = slugify(title) || String((source[slugField] as {current: string}).current)
|
|
127
|
+
let slug = base
|
|
128
|
+
for (
|
|
129
|
+
let n = 2;
|
|
130
|
+
await client.fetch<boolean>(
|
|
131
|
+
`count(*[_type == $type && ${slugField}.current == $slug && ${field} == $language && !(_id in path("drafts.**"))]) > 0`,
|
|
132
|
+
{type, slug, language},
|
|
133
|
+
);
|
|
134
|
+
n++
|
|
135
|
+
)
|
|
136
|
+
slug = `${base}-${n}`
|
|
137
|
+
doc[slugField] = {_type: 'slug', current: slug}
|
|
138
|
+
}
|
|
139
|
+
const published = ai && !remaining.length && !aiError
|
|
140
|
+
await client.createOrReplace({...doc, _id: published ? id : `drafts.${id}`, _type: type} as {_id: string; _type: string})
|
|
141
|
+
if (remaining.length)
|
|
142
|
+
await client.createOrReplace({
|
|
143
|
+
_id: translationTodoId(id),
|
|
144
|
+
_type: TRANSLATION_TODO_TYPE,
|
|
145
|
+
documentId: id,
|
|
146
|
+
sourceId,
|
|
147
|
+
sourceLanguage,
|
|
148
|
+
paths: remaining,
|
|
149
|
+
})
|
|
150
|
+
await recordTrace(client, id, written, {sourceId, sourceLanguage})
|
|
151
|
+
await linkTranslations(client, sourceId, sourceLanguage, type, [{language, id}])
|
|
152
|
+
return {id, published, aiError}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Complète la version `target` (valeurs par défaut des composants d'une langue)
|
|
157
|
+
* depuis `source` : un bloc absent est recopié, un texte vide ou resté identique au
|
|
158
|
+
* texte source est traduit. Les textes déjà traduits ne sont jamais touchés.
|
|
159
|
+
* Rend le document complété et le nombre de textes traduits.
|
|
160
|
+
*/
|
|
161
|
+
export async function fillTranslation(opts: {
|
|
162
|
+
config: ResolvedKitConfig
|
|
163
|
+
schemaType: SchemaTypeLike | undefined
|
|
164
|
+
source: Doc
|
|
165
|
+
target: Doc
|
|
166
|
+
sourceLanguage: string
|
|
167
|
+
language: string
|
|
168
|
+
}): Promise<{document: Doc; translated: number; written: {path: string; value: unknown}[]}> {
|
|
169
|
+
const {config, schemaType, source, sourceLanguage, language} = opts
|
|
170
|
+
const document = structuredClone(opts.target) as Doc
|
|
171
|
+
// Blocs (champs de premier niveau) absents de la cible : recopiés tels quels.
|
|
172
|
+
for (const [key, value] of Object.entries(source)) {
|
|
173
|
+
if (key.startsWith('_') || key === (config.i18n?.field ?? 'language')) continue
|
|
174
|
+
if (document[key] === undefined || document[key] === null) document[key] = structuredClone(value)
|
|
175
|
+
}
|
|
176
|
+
const paths = untranslatedPaths(source, document, schemaType)
|
|
177
|
+
if (!paths.length || !config.history) return {document, translated: 0, written: []}
|
|
178
|
+
const translated = await translateValues(
|
|
179
|
+
config.history,
|
|
180
|
+
sourceLanguage,
|
|
181
|
+
language,
|
|
182
|
+
paths.map((p) => valueAtKey(source, p)),
|
|
183
|
+
)
|
|
184
|
+
const written: {path: string; value: unknown}[] = []
|
|
185
|
+
paths.forEach((p, n) => {
|
|
186
|
+
if (setAtKey(document, p, translated[n])) written.push({path: p, value: translated[n]})
|
|
187
|
+
})
|
|
188
|
+
return {document, translated: written.length, written}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Textes de `source` absents ou non traduits dans `target` (vides, ou identiques au
|
|
193
|
+
* texte source). Un texte déjà traduit, même à la main, n'y figure jamais.
|
|
194
|
+
*/
|
|
195
|
+
export function untranslatedPaths(source: Doc, target: Doc | null, schemaType: SchemaTypeLike | undefined): string[] {
|
|
196
|
+
return propagate({}, {}, source, schemaType).toTranslate.filter((path) => {
|
|
197
|
+
const src = valueAtKey(source, path)
|
|
198
|
+
if (!collectTexts(src).some((t) => t.trim())) return false
|
|
199
|
+
const cur = target ? valueAtKey(target, path) : undefined
|
|
200
|
+
return !collectTexts(cur).some((t) => t.trim()) || JSON.stringify(cur) === JSON.stringify(src)
|
|
201
|
+
})
|
|
202
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type {SanityClient} from 'sanity'
|
|
2
|
+
import {TRANSLATION_TODO_TYPE, translationTodoId} from '../schema/translationTodo'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Trace des textes écrits par l'IA dans une traduction (champ `machine` du document
|
|
6
|
+
* `ncfTodo.<id>`). Sert à ne JAMAIS écraser un texte modifié à la main dans une
|
|
7
|
+
* langue : à la propagation, un texte dont l'empreinte diffère de celle que l'IA a
|
|
8
|
+
* écrite est un texte édité, gardé sauf demande explicite.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** Empreinte courte et stable d'une valeur (FNV-1a 32 bits sur son JSON). */
|
|
12
|
+
export function hashValue(value: unknown): string {
|
|
13
|
+
const text = JSON.stringify(value ?? null)
|
|
14
|
+
let h = 0x811c9dc5
|
|
15
|
+
for (let i = 0; i < text.length; i++) {
|
|
16
|
+
h ^= text.charCodeAt(i)
|
|
17
|
+
h = Math.imul(h, 0x01000193)
|
|
18
|
+
}
|
|
19
|
+
return (h >>> 0).toString(36)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function readTrace(client: SanityClient, documentId: string): Promise<Map<string, string>> {
|
|
23
|
+
const list = await client.fetch<{path?: string; hash?: string}[] | null>(`*[_id == $id][0].machine`, {id: translationTodoId(documentId)})
|
|
24
|
+
return new Map((list ?? []).filter((e) => e.path && e.hash).map((e) => [e.path as string, e.hash as string]))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Enregistre les textes que l'IA vient d'écrire (fusion avec la trace existante). */
|
|
28
|
+
export async function recordTrace(
|
|
29
|
+
client: SanityClient,
|
|
30
|
+
documentId: string,
|
|
31
|
+
entries: {path: string; value: unknown}[],
|
|
32
|
+
meta: {sourceId: string; sourceLanguage: string},
|
|
33
|
+
): Promise<void> {
|
|
34
|
+
if (!entries.length) return
|
|
35
|
+
const id = translationTodoId(documentId)
|
|
36
|
+
const trace = await readTrace(client, documentId)
|
|
37
|
+
for (const e of entries) trace.set(e.path, hashValue(e.value))
|
|
38
|
+
const machine = [...trace].map(([path, hash]) => ({_key: `k${hashValue(path)}`, _type: 'ncfMachineText', path, hash}))
|
|
39
|
+
await client
|
|
40
|
+
.transaction()
|
|
41
|
+
.createIfNotExists({_id: id, _type: TRANSLATION_TODO_TYPE, documentId: documentId.replace(/^drafts\./, ''), ...meta, paths: []})
|
|
42
|
+
.patch(id, (p) => p.set({machine}))
|
|
43
|
+
.commit()
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Un texte de traduction peut-il être remplacé par une nouvelle traduction ?
|
|
48
|
+
* Oui s'il est vide, s'il est encore le texte source (jamais traduit) ou s'il est
|
|
49
|
+
* exactement ce que l'IA avait écrit. Non s'il a été modifié à la main.
|
|
50
|
+
*/
|
|
51
|
+
export function isReplaceable(current: unknown, source: unknown, path: string, trace: Map<string, string>, isEmpty: (v: unknown) => boolean): boolean {
|
|
52
|
+
if (isEmpty(current)) return true
|
|
53
|
+
if (JSON.stringify(current) === JSON.stringify(source)) return true
|
|
54
|
+
return trace.get(path) === hashValue(current)
|
|
55
|
+
}
|
|
@@ -5,6 +5,9 @@ import {defineField, defineType} from 'sanity'
|
|
|
5
5
|
* la version principale). Un document par traduction : `ncfTodo.<id de la traduction>`
|
|
6
6
|
* (point = jamais servi par l'API publique). Jamais créé ni édité à la main : la
|
|
7
7
|
* pastille du formulaire le lit et « Marquer comme traduit » retire le champ.
|
|
8
|
+
* `machine` : empreinte de chaque texte écrit par l'IA (chemin + empreinte). Un texte
|
|
9
|
+
* dont l'empreinte ne correspond plus a été modifié à la main dans cette langue : la
|
|
10
|
+
* propagation ne l'écrase pas sans la case « Overwrite texts edited by hand ».
|
|
8
11
|
*/
|
|
9
12
|
export const TRANSLATION_TODO_TYPE = 'ncfTranslationTodo'
|
|
10
13
|
export const translationTodoId = (documentId: string) => `ncfTodo.${documentId.replace(/^drafts\./, '')}`
|
|
@@ -18,6 +21,13 @@ export const translationTodoType = defineType({
|
|
|
18
21
|
defineField({name: 'sourceId', title: 'Main version', type: 'string', readOnly: true}),
|
|
19
22
|
defineField({name: 'sourceLanguage', title: 'Main language', type: 'string', readOnly: true}),
|
|
20
23
|
defineField({name: 'paths', title: 'Fields to translate', type: 'array', of: [{type: 'string'}], readOnly: true}),
|
|
24
|
+
defineField({
|
|
25
|
+
name: 'machine',
|
|
26
|
+
title: 'Texts written by AI',
|
|
27
|
+
type: 'array',
|
|
28
|
+
readOnly: true,
|
|
29
|
+
of: [{type: 'object', name: 'ncfMachineText', fields: [{name: 'path', type: 'string'}, {name: 'hash', type: 'string'}]}],
|
|
30
|
+
}),
|
|
21
31
|
],
|
|
22
32
|
preview: {select: {title: 'documentId'}, prepare: ({title}) => ({title: `To translate: ${title}`})},
|
|
23
33
|
})
|