dsh-cait 1.1.3 → 1.1.7
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/CHANGELOG.md +186 -0
- package/README.ja.md +51 -14
- package/README.md +137 -30
- package/README.zh-CN.md +51 -14
- package/client/components.js +2033 -48
- package/client/entry.js +2 -0
- package/client/helpers.js +12 -0
- package/client/locales.js +658 -8
- package/client/styles.js +111 -4
- package/client.js +2814 -58
- package/lib/aligner.js +6 -6
- package/lib/analysis.js +88 -0
- package/lib/api.js +46 -0
- package/lib/brief.js +275 -0
- package/lib/docx.js +155 -26
- package/lib/formats.js +37 -0
- package/lib/glossary.js +20 -4
- package/lib/import-export.js +212 -13
- package/lib/libraries.js +156 -0
- package/lib/library-export.js +7 -1
- package/lib/localization.js +1098 -0
- package/lib/lock.js +30 -0
- package/lib/notes.js +144 -0
- package/lib/persistence.js +54 -7
- package/lib/pptx.js +33 -1
- package/lib/pre-translate.js +62 -45
- package/lib/propagate.js +3 -0
- package/lib/qa.js +94 -4
- package/lib/replace.js +243 -0
- package/lib/review.js +131 -0
- package/lib/segment-edit.js +199 -0
- package/lib/store-utils.js +33 -6
- package/lib/store.js +507 -47
- package/lib/subtitles.js +213 -0
- package/lib/term-extract.js +105 -3
- package/lib/terms.js +4 -2
- package/lib/tm-maintenance.js +157 -0
- package/lib/tm.js +4 -1
- package/lib/tools.js +294 -0
- package/lib/xlsx.js +176 -66
- package/package.json +1 -1
package/lib/import-export.js
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
import { mkdirSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs'
|
|
2
2
|
import { join } from 'node:path'
|
|
3
3
|
import { CatError } from './errors.js'
|
|
4
|
-
import { MAX_DOCX_BASE64, MAX_IMPORT_CHARS, MAX_SEGMENTS, MAX_SEGMENT_CHARS, canonicalLanguage, cleanLang, cleanName, cleanText, csvCell, makeId, mdCell, now, safeBaseName } from './store-utils.js'
|
|
4
|
+
import { MAX_DOCX_BASE64, MAX_IMPORT_CHARS, MAX_SEGMENTS, MAX_SEGMENT_CHARS, canonicalLanguage, cleanLang, cleanName, cleanText, csvCell, makeId, mdCell, normalizeSegmentState, now, safeBaseName } from './store-utils.js'
|
|
5
5
|
import { stripMarkdown } from './markdown.js'
|
|
6
|
-
import { DOCX_MIME, buildDocx, buildDocxFromText, parseDocxDetailed } from './docx.js'
|
|
6
|
+
import { DOCX_MIME, buildBilingualDocx, buildDocx, buildDocxFromText, parseDocxDetailed } from './docx.js'
|
|
7
7
|
import { EPUB_MIME, buildEpub, buildEpubFromText, parseEpub } from './epub.js'
|
|
8
8
|
import { PPTX_MIME, buildPptx, parsePptxDetailed } from './pptx.js'
|
|
9
|
-
import { XLSX_MIME, buildXlsx, parseXlsx } from './xlsx.js'
|
|
9
|
+
import { XLSX_MIME, buildXlsx, parseXlsx, patchXlsx } from './xlsx.js'
|
|
10
10
|
import { buildTmx, parseTmx } from './tmx.js'
|
|
11
|
-
import { buildHtml, buildJson, buildPo, buildRtf, buildSrt, buildVtt, buildXliff, buildXliff2, parseCsv, parseHtml, parseJson, parsePo, parseRtf, parseSrt, parseVtt, parseXliff } from './formats.js'
|
|
11
|
+
import { buildHtml, buildJson, buildPo, buildRtf, buildSrt, buildVtt, buildXliff, buildXliff2, buildXliffReview, parseCsv, parseHtml, parseJson, parsePo, parseRtf, parseSrt, parseVtt, parseXliff } from './formats.js'
|
|
12
|
+
import { LOCALIZATION_EXPORT, LOCALIZATION_EXTENSIONS, buildLocalization, parseLocalization } from './localization.js'
|
|
12
13
|
import { decodeXml } from './xml.js'
|
|
13
14
|
|
|
14
15
|
const BINARY_SOURCES = {
|
|
15
16
|
docx: { label: 'DOCX', fallback: 'source.docx' },
|
|
16
17
|
pptx: { label: 'PPTX', fallback: 'source.pptx' },
|
|
18
|
+
xlsx: { label: 'XLSX', fallback: 'source.xlsx' },
|
|
17
19
|
epub: { label: 'EPUB', fallback: 'source.epub' },
|
|
18
20
|
}
|
|
19
21
|
|
|
@@ -143,6 +145,18 @@ export const importExportMethods = {
|
|
|
143
145
|
return readSource(this, doc, 'pptx')
|
|
144
146
|
},
|
|
145
147
|
|
|
148
|
+
attachXlsxSource(doc, buffer, name = 'source.xlsx') {
|
|
149
|
+
attachSource(this, doc, 'xlsx', buffer, name)
|
|
150
|
+
},
|
|
151
|
+
|
|
152
|
+
removeXlsxSource(doc) {
|
|
153
|
+
removeSource(this, doc, 'xlsx')
|
|
154
|
+
},
|
|
155
|
+
|
|
156
|
+
readXlsxSource(doc) {
|
|
157
|
+
return readSource(this, doc, 'xlsx')
|
|
158
|
+
},
|
|
159
|
+
|
|
146
160
|
attachEpubSource(doc, buffer, name = 'source.epub') {
|
|
147
161
|
attachSource(this, doc, 'epub', buffer, name)
|
|
148
162
|
},
|
|
@@ -243,6 +257,78 @@ export const importExportMethods = {
|
|
|
243
257
|
}
|
|
244
258
|
},
|
|
245
259
|
|
|
260
|
+
applyXliffReview({ documentId, text, overwrite = false } = {}) {
|
|
261
|
+
const doc = this.requireDocument(documentId)
|
|
262
|
+
if (!text || !String(text).trim()) throw new CatError('EMPTY_FILE', '审校包内容为空。')
|
|
263
|
+
const parsed = parseXliff(String(text))
|
|
264
|
+
const normalize = (value) => String(value ?? '').replace(/\s+/g, ' ').trim()
|
|
265
|
+
const byUid = new Map(doc.segments.filter((segment) => segment.uid).map((segment) => [segment.uid, segment]))
|
|
266
|
+
const byId = new Map(doc.segments.map((segment) => [segment.id, segment]))
|
|
267
|
+
const byXliffId = new Map(
|
|
268
|
+
doc.segments
|
|
269
|
+
.filter((segment) => segment.meta?.xliffId)
|
|
270
|
+
.map((segment) => [String(segment.meta.xliffId), segment]),
|
|
271
|
+
)
|
|
272
|
+
const bySource = new Map()
|
|
273
|
+
for (const segment of doc.segments) {
|
|
274
|
+
const key = normalize(segment.source)
|
|
275
|
+
if (!bySource.has(key)) bySource.set(key, [])
|
|
276
|
+
bySource.get(key).push(segment)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
let applied = 0
|
|
280
|
+
let changed = 0
|
|
281
|
+
let unmatched = 0
|
|
282
|
+
let sourceMismatch = 0
|
|
283
|
+
const now = new Date().toISOString()
|
|
284
|
+
for (const incoming of parsed.segments) {
|
|
285
|
+
const uid = incoming.meta?.caitUid
|
|
286
|
+
const xliffId = incoming.meta?.xliffId
|
|
287
|
+
let segment = uid ? byUid.get(uid) : null
|
|
288
|
+
if (!segment && xliffId) segment = byXliffId.get(String(xliffId))
|
|
289
|
+
if (!segment && xliffId) segment = byId.get(String(xliffId))
|
|
290
|
+
if (!segment) {
|
|
291
|
+
const matches = bySource.get(normalize(incoming.source)) || []
|
|
292
|
+
if (matches.length === 1) segment = matches[0]
|
|
293
|
+
}
|
|
294
|
+
if (!segment) {
|
|
295
|
+
unmatched += 1
|
|
296
|
+
continue
|
|
297
|
+
}
|
|
298
|
+
if (normalize(segment.source) !== normalize(incoming.source)) {
|
|
299
|
+
sourceMismatch += 1
|
|
300
|
+
continue
|
|
301
|
+
}
|
|
302
|
+
const target = String(incoming.target ?? '').trim()
|
|
303
|
+
if (!target) continue
|
|
304
|
+
if (!overwrite && segment.target === target) continue
|
|
305
|
+
segment.target = target
|
|
306
|
+
segment.suggestion = null
|
|
307
|
+
segment.suggestionReason = null
|
|
308
|
+
segment.origin = 'manual'
|
|
309
|
+
segment.confirmedAt = ['final', 'signed-off'].includes(incoming.meta?.targetState)
|
|
310
|
+
? now
|
|
311
|
+
: null
|
|
312
|
+
segment.updatedAt = now
|
|
313
|
+
normalizeSegmentState(segment)
|
|
314
|
+
applied += 1
|
|
315
|
+
changed += 1
|
|
316
|
+
}
|
|
317
|
+
if (changed) {
|
|
318
|
+
this.touch()
|
|
319
|
+
this.persist()
|
|
320
|
+
}
|
|
321
|
+
return {
|
|
322
|
+
documentId: doc.id,
|
|
323
|
+
documentName: doc.name,
|
|
324
|
+
total: parsed.segments.length,
|
|
325
|
+
applied,
|
|
326
|
+
changed,
|
|
327
|
+
unmatched,
|
|
328
|
+
sourceMismatch,
|
|
329
|
+
}
|
|
330
|
+
},
|
|
331
|
+
|
|
246
332
|
importFile({ name, text, base64, sourceLang, targetLang, importAs, csvSourceColumn, csvTargetColumn } = {}) {
|
|
247
333
|
const fileName = cleanName(name, '导入文档')
|
|
248
334
|
const extension = (fileName.match(/\.([a-z0-9]+)$/i)?.[1] ?? '').toLowerCase()
|
|
@@ -261,11 +347,13 @@ export const importExportMethods = {
|
|
|
261
347
|
name: fileName,
|
|
262
348
|
text: content.text,
|
|
263
349
|
paragraphMap: content.paragraphs.map((paragraph) => paragraph.index),
|
|
350
|
+
paragraphMeta: Object.fromEntries(content.paragraphs.map((paragraph) => [paragraph.index, paragraph.meta])),
|
|
264
351
|
docxSource: buffer,
|
|
265
352
|
docxName: fileName,
|
|
266
353
|
sourceLang,
|
|
267
354
|
targetLang,
|
|
268
355
|
format: 'docx',
|
|
356
|
+
formatMeta: content.formatMeta,
|
|
269
357
|
})
|
|
270
358
|
}
|
|
271
359
|
|
|
@@ -319,6 +407,7 @@ export const importExportMethods = {
|
|
|
319
407
|
targetLang,
|
|
320
408
|
format: extension,
|
|
321
409
|
...(extension === 'pptx' ? { pptxSource: buffer, pptxName: fileName } : {}),
|
|
410
|
+
...(extension === 'xlsx' ? { xlsxSource: buffer, xlsxName: fileName } : {}),
|
|
322
411
|
})
|
|
323
412
|
return {
|
|
324
413
|
...created,
|
|
@@ -339,6 +428,49 @@ export const importExportMethods = {
|
|
|
339
428
|
})
|
|
340
429
|
}
|
|
341
430
|
|
|
431
|
+
let localizationKind = LOCALIZATION_EXTENSIONS[extension]
|
|
432
|
+
if (!localizationKind && extension === 'xml' && /<resources\b/i.test(String(text ?? ''))) {
|
|
433
|
+
localizationKind = 'android'
|
|
434
|
+
}
|
|
435
|
+
if (localizationKind) {
|
|
436
|
+
const sourceText = String(text ?? '')
|
|
437
|
+
if (!sourceText.trim()) throw new CatError('EMPTY_FILE', '文件内容为空。')
|
|
438
|
+
if (sourceText.length > MAX_IMPORT_CHARS) throw new CatError('FILE_TOO_LARGE', '文件过大。')
|
|
439
|
+
let parsed
|
|
440
|
+
try {
|
|
441
|
+
parsed = parseLocalization(sourceText, localizationKind)
|
|
442
|
+
} catch (error) {
|
|
443
|
+
if (error instanceof CatError) throw error
|
|
444
|
+
throw new CatError(
|
|
445
|
+
`${localizationKind.toUpperCase()}_PARSE`,
|
|
446
|
+
`${localizationKind} 解析失败: ${error?.message ?? error}`,
|
|
447
|
+
)
|
|
448
|
+
}
|
|
449
|
+
const src = cleanLang(
|
|
450
|
+
sourceLang || parsed.sourceLang || this.settings.sourceLang,
|
|
451
|
+
this.settings.sourceLang,
|
|
452
|
+
)
|
|
453
|
+
const tgt = cleanLang(
|
|
454
|
+
targetLang || parsed.targetLang || this.settings.targetLang,
|
|
455
|
+
this.settings.targetLang,
|
|
456
|
+
)
|
|
457
|
+
const created = this.createDocument({
|
|
458
|
+
name: fileName,
|
|
459
|
+
segments: parsed.segments,
|
|
460
|
+
sourceLang: src,
|
|
461
|
+
targetLang: tgt,
|
|
462
|
+
format: parsed.format || localizationKind,
|
|
463
|
+
formatMeta: parsed.formatMeta,
|
|
464
|
+
})
|
|
465
|
+
return {
|
|
466
|
+
...created,
|
|
467
|
+
sourceLang: src,
|
|
468
|
+
targetLang: tgt,
|
|
469
|
+
units: parsed.segments.length,
|
|
470
|
+
warnings: parsed.warnings ?? [],
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
342
474
|
const structured = {
|
|
343
475
|
csv: { parser: parseCsv, format: 'csv' },
|
|
344
476
|
srt: { parser: parseSrt, format: 'srt' },
|
|
@@ -435,13 +567,26 @@ export const importExportMethods = {
|
|
|
435
567
|
|
|
436
568
|
if (format === 'docx') {
|
|
437
569
|
const grouped = new Map()
|
|
438
|
-
|
|
439
|
-
|
|
570
|
+
const byPart = new Map()
|
|
571
|
+
let hasPartMeta = false
|
|
572
|
+
for (const [index, segment] of doc.segments.entries()) {
|
|
573
|
+
const meta = segment.meta && typeof segment.meta === 'object' ? segment.meta : null
|
|
574
|
+
const part = meta?.docxPart || 'word/document.xml'
|
|
575
|
+
const ordinal = Number.isInteger(meta?.docxParagraph)
|
|
576
|
+
? meta.docxParagraph
|
|
577
|
+
: Number.isInteger(segment.paragraph)
|
|
578
|
+
? segment.paragraph
|
|
579
|
+
: index
|
|
580
|
+
if (meta?.docxPart && Number.isInteger(meta?.docxParagraph)) hasPartMeta = true
|
|
581
|
+
const key = `${part}\u0000${ordinal}`
|
|
440
582
|
if (!grouped.has(key)) grouped.set(key, [])
|
|
441
583
|
grouped.get(key).push(segment)
|
|
584
|
+
if (!byPart.has(part)) byPart.set(part, new Map())
|
|
585
|
+
const partMap = byPart.get(part)
|
|
586
|
+
if (!partMap.has(ordinal)) partMap.set(ordinal, [])
|
|
587
|
+
partMap.get(ordinal).push(segment)
|
|
442
588
|
}
|
|
443
|
-
const
|
|
444
|
-
for (const [key, segments] of grouped) {
|
|
589
|
+
const combine = (segments) => {
|
|
445
590
|
const translated = segments
|
|
446
591
|
.map((segment) => segment.target)
|
|
447
592
|
.filter(Boolean)
|
|
@@ -452,22 +597,42 @@ export const importExportMethods = {
|
|
|
452
597
|
.filter(Boolean)
|
|
453
598
|
.join(' ')
|
|
454
599
|
.trim()
|
|
455
|
-
|
|
600
|
+
return translated || fallback
|
|
601
|
+
}
|
|
602
|
+
const replacements = new Map()
|
|
603
|
+
for (const [key, segments] of grouped) {
|
|
604
|
+
const ordinal = Number(key.split('\u0000')[1])
|
|
605
|
+
const text = combine(segments)
|
|
606
|
+
if (text) replacements.set(ordinal, text)
|
|
607
|
+
}
|
|
608
|
+
for (const [part, partMap] of byPart) {
|
|
609
|
+
for (const [ordinal, segments] of partMap) {
|
|
610
|
+
const text = combine(segments)
|
|
611
|
+
if (text) partMap.set(ordinal, text)
|
|
612
|
+
else partMap.delete(ordinal)
|
|
613
|
+
}
|
|
456
614
|
}
|
|
457
615
|
|
|
458
616
|
let content = null
|
|
459
617
|
const template = this.readDocxSource(doc)
|
|
460
618
|
if (template) {
|
|
461
619
|
try {
|
|
462
|
-
content =
|
|
620
|
+
content = hasPartMeta
|
|
621
|
+
? buildDocx(template, null, byPart)
|
|
622
|
+
: buildDocx(template, replacements)
|
|
463
623
|
} catch (error) {
|
|
464
624
|
this.logger?.warn?.(`CAIT: DOCX 模板套用失败,改用纯文本 DOCX: ${error.message}`)
|
|
465
625
|
}
|
|
466
626
|
}
|
|
467
627
|
if (!content) {
|
|
468
628
|
const paragraphs = [...grouped.keys()]
|
|
469
|
-
.sort((left, right) =>
|
|
470
|
-
|
|
629
|
+
.sort((left, right) => {
|
|
630
|
+
const [leftPart, leftOrdinal] = left.split('\u0000')
|
|
631
|
+
const [rightPart, rightOrdinal] = right.split('\u0000')
|
|
632
|
+
if (leftPart !== rightPart) return leftPart.localeCompare(rightPart)
|
|
633
|
+
return Number(leftOrdinal) - Number(rightOrdinal)
|
|
634
|
+
})
|
|
635
|
+
.map((key) => replacements.get(Number(key.split('\u0000')[1])))
|
|
471
636
|
.filter(Boolean)
|
|
472
637
|
content = buildDocxFromText(paragraphs)
|
|
473
638
|
}
|
|
@@ -479,6 +644,19 @@ export const importExportMethods = {
|
|
|
479
644
|
}
|
|
480
645
|
}
|
|
481
646
|
|
|
647
|
+
if (format === 'bilingual-docx') {
|
|
648
|
+
const content = buildBilingualDocx(doc.segments, {
|
|
649
|
+
includeStatus: true,
|
|
650
|
+
title: doc.name,
|
|
651
|
+
})
|
|
652
|
+
return {
|
|
653
|
+
filename: `${base}.bilingual.docx`,
|
|
654
|
+
mime: DOCX_MIME,
|
|
655
|
+
content,
|
|
656
|
+
count: doc.segments.length,
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
|
|
482
660
|
if (format === 'epub') {
|
|
483
661
|
let content = null
|
|
484
662
|
const template = this.readEpubSource(doc)
|
|
@@ -498,6 +676,16 @@ export const importExportMethods = {
|
|
|
498
676
|
}
|
|
499
677
|
}
|
|
500
678
|
|
|
679
|
+
const localizationExport = LOCALIZATION_EXPORT[format]
|
|
680
|
+
if (localizationExport) {
|
|
681
|
+
return {
|
|
682
|
+
filename: `${base}.${doc.targetLang}.${localizationExport.extension}`,
|
|
683
|
+
mime: localizationExport.mime,
|
|
684
|
+
content: buildLocalization(doc, format),
|
|
685
|
+
count: doc.segments.length,
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
|
|
501
689
|
if (format === 'srt') {
|
|
502
690
|
return {
|
|
503
691
|
filename: `${base}.${doc.targetLang}.srt`,
|
|
@@ -525,6 +713,15 @@ export const importExportMethods = {
|
|
|
525
713
|
}
|
|
526
714
|
}
|
|
527
715
|
|
|
716
|
+
if (format === 'xliff-review') {
|
|
717
|
+
return {
|
|
718
|
+
filename: `${base}.review.xliff`,
|
|
719
|
+
mime: 'application/x-xliff+xml;charset=utf-8',
|
|
720
|
+
content: buildXliffReview(doc),
|
|
721
|
+
count: doc.segments.length,
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
|
|
528
725
|
if (format === 'xliff' || format === 'xlf' || format === 'sdlxliff') {
|
|
529
726
|
return {
|
|
530
727
|
filename: `${base}.${doc.targetLang}.${format === 'sdlxliff' ? 'sdlxliff' : 'xliff'}`,
|
|
@@ -574,10 +771,12 @@ export const importExportMethods = {
|
|
|
574
771
|
}
|
|
575
772
|
|
|
576
773
|
if (format === 'xlsx') {
|
|
774
|
+
const source = this.readXlsxSource(doc)
|
|
775
|
+
const patched = source ? patchXlsx(doc, source) : null
|
|
577
776
|
return {
|
|
578
777
|
filename: `${base}.${doc.targetLang}.xlsx`,
|
|
579
778
|
mime: XLSX_MIME,
|
|
580
|
-
content: buildXlsx(doc),
|
|
779
|
+
content: patched ? patched.buffer : buildXlsx(doc),
|
|
581
780
|
count: doc.segments.length,
|
|
582
781
|
}
|
|
583
782
|
}
|
package/lib/libraries.js
CHANGED
|
@@ -76,6 +76,152 @@ export const libraryMethods = {
|
|
|
76
76
|
defaultGlossaryId: this.mounts.defaultGlossaryId,
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
|
+
,
|
|
80
|
+
/**
|
|
81
|
+
* A document-level binding is either null (inherit the global mounts) or an
|
|
82
|
+
* explicit list. An explicit empty list is meaningful: this document must not
|
|
83
|
+
* read or write that kind of resource at all.
|
|
84
|
+
*/
|
|
85
|
+
normalizeResourceIds(kind, ids) {
|
|
86
|
+
if (ids === null || ids === undefined) return null
|
|
87
|
+
if (!Array.isArray(ids)) {
|
|
88
|
+
throw new CatError('INVALID_RESOURCE_IDS', '文档绑定的库列表必须是数组或 null。')
|
|
89
|
+
}
|
|
90
|
+
const unique = []
|
|
91
|
+
for (const raw of ids) {
|
|
92
|
+
const id = String(raw ?? '').trim()
|
|
93
|
+
if (!id || unique.includes(id)) continue
|
|
94
|
+
if (kind === 'tm') this.requireTmLibrary(id)
|
|
95
|
+
else this.requireGlossaryLibrary(id)
|
|
96
|
+
unique.push(id)
|
|
97
|
+
}
|
|
98
|
+
return unique
|
|
99
|
+
}
|
|
100
|
+
,
|
|
101
|
+
effectiveTmIds(doc) {
|
|
102
|
+
if (!doc || doc.tmIds === null || doc.tmIds === undefined) return [...this.mounts.tmIds]
|
|
103
|
+
const known = new Set(this.tmLibraries.map((library) => library.tmId))
|
|
104
|
+
return doc.tmIds.filter((id) => known.has(id))
|
|
105
|
+
}
|
|
106
|
+
,
|
|
107
|
+
effectiveGlossaryIds(doc) {
|
|
108
|
+
if (!doc || doc.glossaryIds === null || doc.glossaryIds === undefined) {
|
|
109
|
+
return [...this.mounts.glossaryIds]
|
|
110
|
+
}
|
|
111
|
+
const known = new Set(this.glossaryLibraries.map((library) => library.glossaryId))
|
|
112
|
+
return doc.glossaryIds.filter((id) => known.has(id))
|
|
113
|
+
}
|
|
114
|
+
,
|
|
115
|
+
effectiveDefaultTmId(doc) {
|
|
116
|
+
const ids = this.effectiveTmIds(doc)
|
|
117
|
+
const preferred = doc?.defaultWriteTmId
|
|
118
|
+
if (preferred && ids.includes(preferred)) return preferred
|
|
119
|
+
// Inheriting documents keep the global write preference. Explicit document
|
|
120
|
+
// bindings fall back to their own first library.
|
|
121
|
+
if ((doc?.tmIds === null || doc?.tmIds === undefined) && ids.includes(this.mounts.defaultTmId)) {
|
|
122
|
+
return this.mounts.defaultTmId
|
|
123
|
+
}
|
|
124
|
+
return ids[0] ?? null
|
|
125
|
+
}
|
|
126
|
+
,
|
|
127
|
+
documentWriteTmId(doc, requestedTmId) {
|
|
128
|
+
const ids = this.effectiveTmIds(doc)
|
|
129
|
+
if (!ids.length) {
|
|
130
|
+
throw new CatError(
|
|
131
|
+
'NO_TM_BOUND',
|
|
132
|
+
'当前文档没有绑定任何翻译记忆库,无法写入。请在文档资源里选择一个写入库。',
|
|
133
|
+
409,
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
let id = requestedTmId ? this.requireTmLibrary(requestedTmId) : this.effectiveDefaultTmId(doc)
|
|
137
|
+
if (requestedTmId && id && !this.mounts.tmIds.includes(id)) {
|
|
138
|
+
throw new CatError(
|
|
139
|
+
'TM_NOT_MOUNTED',
|
|
140
|
+
`翻译记忆库未挂载: ${id},请先在 CAIT 的库面板挂载它。`,
|
|
141
|
+
409,
|
|
142
|
+
)
|
|
143
|
+
}
|
|
144
|
+
if (!id || !ids.includes(id)) {
|
|
145
|
+
throw new CatError(
|
|
146
|
+
'TM_NOT_BOUND',
|
|
147
|
+
`翻译记忆库未绑定到当前文档: ${id ?? ''},请先在文档资源里选择它。`,
|
|
148
|
+
409,
|
|
149
|
+
)
|
|
150
|
+
}
|
|
151
|
+
return id
|
|
152
|
+
}
|
|
153
|
+
,
|
|
154
|
+
documentResourcesView(doc) {
|
|
155
|
+
return {
|
|
156
|
+
tmIds: doc.tmIds === null || doc.tmIds === undefined ? null : [...doc.tmIds],
|
|
157
|
+
glossaryIds:
|
|
158
|
+
doc.glossaryIds === null || doc.glossaryIds === undefined ? null : [...doc.glossaryIds],
|
|
159
|
+
defaultWriteTmId: doc.defaultWriteTmId ?? null,
|
|
160
|
+
effectiveTmIds: this.effectiveTmIds(doc),
|
|
161
|
+
effectiveGlossaryIds: this.effectiveGlossaryIds(doc),
|
|
162
|
+
effectiveDefaultTmId: this.effectiveDefaultTmId(doc),
|
|
163
|
+
inheritsTm: doc.tmIds === null || doc.tmIds === undefined,
|
|
164
|
+
inheritsGlossary: doc.glossaryIds === null || doc.glossaryIds === undefined,
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
,
|
|
168
|
+
setDocumentResources(input = {}) {
|
|
169
|
+
const { documentId, tmIds, glossaryIds, defaultWriteTmId } = input
|
|
170
|
+
const doc = this.requireDocument(documentId)
|
|
171
|
+
const hasTm = Object.prototype.hasOwnProperty.call(input, 'tmIds')
|
|
172
|
+
const hasGlossary = Object.prototype.hasOwnProperty.call(input, 'glossaryIds')
|
|
173
|
+
const hasDefault = Object.prototype.hasOwnProperty.call(input, 'defaultWriteTmId')
|
|
174
|
+
|
|
175
|
+
const nextTmIds = hasTm ? this.normalizeResourceIds('tm', tmIds) : doc.tmIds
|
|
176
|
+
const nextGlossaryIds = hasGlossary
|
|
177
|
+
? this.normalizeResourceIds('glossary', glossaryIds)
|
|
178
|
+
: doc.glossaryIds
|
|
179
|
+
let nextDefaultTmId = hasDefault ? defaultWriteTmId : doc.defaultWriteTmId
|
|
180
|
+
if (nextDefaultTmId === undefined || nextDefaultTmId === '') nextDefaultTmId = null
|
|
181
|
+
const effectiveTm = nextTmIds === null || nextTmIds === undefined
|
|
182
|
+
? this.mounts.tmIds
|
|
183
|
+
: nextTmIds
|
|
184
|
+
if (nextDefaultTmId) {
|
|
185
|
+
nextDefaultTmId = this.requireTmLibrary(nextDefaultTmId)
|
|
186
|
+
if (!effectiveTm.includes(nextDefaultTmId)) {
|
|
187
|
+
if (hasDefault) {
|
|
188
|
+
throw new CatError(
|
|
189
|
+
'TM_NOT_BOUND',
|
|
190
|
+
`翻译记忆库未绑定到当前文档: ${nextDefaultTmId},不能设为默认写入库。`,
|
|
191
|
+
409,
|
|
192
|
+
)
|
|
193
|
+
}
|
|
194
|
+
nextDefaultTmId = null
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const before = {
|
|
199
|
+
revision: this.revision,
|
|
200
|
+
updatedAt: doc.updatedAt,
|
|
201
|
+
tmIds: doc.tmIds,
|
|
202
|
+
glossaryIds: doc.glossaryIds,
|
|
203
|
+
defaultWriteTmId: doc.defaultWriteTmId,
|
|
204
|
+
}
|
|
205
|
+
doc.tmIds = nextTmIds
|
|
206
|
+
doc.glossaryIds = nextGlossaryIds
|
|
207
|
+
doc.defaultWriteTmId = nextDefaultTmId
|
|
208
|
+
doc.updatedAt = now()
|
|
209
|
+
this.touch()
|
|
210
|
+
try {
|
|
211
|
+
this.persist({ strict: true })
|
|
212
|
+
} catch (error) {
|
|
213
|
+
this.revision = before.revision
|
|
214
|
+
doc.updatedAt = before.updatedAt
|
|
215
|
+
doc.tmIds = before.tmIds
|
|
216
|
+
doc.glossaryIds = before.glossaryIds
|
|
217
|
+
doc.defaultWriteTmId = before.defaultWriteTmId
|
|
218
|
+
throw error
|
|
219
|
+
}
|
|
220
|
+
return {
|
|
221
|
+
document: this.documentSummary(doc),
|
|
222
|
+
resources: this.documentResourcesView(doc),
|
|
223
|
+
}
|
|
224
|
+
}
|
|
79
225
|
,
|
|
80
226
|
librariesView() {
|
|
81
227
|
const mountedTm = new Set(this.mounts.tmIds)
|
|
@@ -175,6 +321,16 @@ export const libraryMethods = {
|
|
|
175
321
|
this.mounts[idsKey] = remaining.length ? remaining : [list[0][idKey]]
|
|
176
322
|
if (this.mounts[defaultKey] === libraryId) this.mounts[defaultKey] = this.mounts[idsKey][0]
|
|
177
323
|
|
|
324
|
+
for (const doc of this.docs.values()) {
|
|
325
|
+
if (isTm && Array.isArray(doc.tmIds)) {
|
|
326
|
+
doc.tmIds = doc.tmIds.filter((id) => id !== libraryId)
|
|
327
|
+
}
|
|
328
|
+
if (!isTm && Array.isArray(doc.glossaryIds)) {
|
|
329
|
+
doc.glossaryIds = doc.glossaryIds.filter((id) => id !== libraryId)
|
|
330
|
+
}
|
|
331
|
+
if (isTm && doc.defaultWriteTmId === libraryId) doc.defaultWriteTmId = null
|
|
332
|
+
}
|
|
333
|
+
|
|
178
334
|
this.touch()
|
|
179
335
|
this.persist()
|
|
180
336
|
return { deleted: libraryId, mounts: this.mountsView(), libraries: this.librariesView() }
|
package/lib/library-export.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import { escapeXml } from './xml.js'
|
|
8
8
|
|
|
9
|
-
const CSV_COLUMNS = ['source', 'target', 'note', 'sourceLang', 'caseSensitive']
|
|
9
|
+
const CSV_COLUMNS = ['source', 'target', 'note', 'sourceLang', 'caseSensitive', 'forbidden', 'preferred']
|
|
10
10
|
|
|
11
11
|
function csvCell(value) {
|
|
12
12
|
const text = String(value ?? '')
|
|
@@ -23,6 +23,8 @@ export function glossaryToCsv(entries) {
|
|
|
23
23
|
csvCell(entry.note || ''),
|
|
24
24
|
csvCell(entry.sourceLang || ''),
|
|
25
25
|
entry.caseSensitive ? 'true' : 'false',
|
|
26
|
+
entry.forbidden ? 'true' : 'false',
|
|
27
|
+
entry.preferred ? 'true' : 'false',
|
|
26
28
|
].join(','),
|
|
27
29
|
)
|
|
28
30
|
}
|
|
@@ -37,6 +39,8 @@ export function glossaryToJson(entries) {
|
|
|
37
39
|
note: entry.note || '',
|
|
38
40
|
sourceLang: entry.sourceLang || '',
|
|
39
41
|
caseSensitive: Boolean(entry.caseSensitive),
|
|
42
|
+
forbidden: Boolean(entry.forbidden),
|
|
43
|
+
preferred: Boolean(entry.preferred),
|
|
40
44
|
})),
|
|
41
45
|
null,
|
|
42
46
|
2,
|
|
@@ -68,6 +72,8 @@ export function glossaryToTbx(entries, { sourceLang = 'en', targetLang = 'zh-CN'
|
|
|
68
72
|
lines.push(` <tig><term>${escapeXml(entry.target)}</term></tig>`)
|
|
69
73
|
lines.push(' </langSet>')
|
|
70
74
|
if (entry.note) lines.push(` <descrip type="note">${escapeXml(entry.note)}</descrip>`)
|
|
75
|
+
if (entry.forbidden) lines.push(' <descrip type="status">forbidden</descrip>')
|
|
76
|
+
else if (entry.preferred) lines.push(' <descrip type="status">preferred</descrip>')
|
|
71
77
|
lines.push(' </termEntry>')
|
|
72
78
|
}
|
|
73
79
|
lines.push(' </body>', '</tbx>', '')
|