2.预览翻译文件时无法预览图片! 3.上传文件都要存到知识库中!可被问答 4.在SIP翻译专家agent下上传文件后默认给提示文案可直接发送
This commit is contained in:
1 parent
8c3d0ee31c
commit
90b843b4e8
9 files changed
+308
-75
No files matched your search
@@ -93,6 +93,7 @@ type Report struct {
|
||||
InlineTextCells int // worksheet inlineStr is blocks scanned
|
||||
DrawingBodies int // txBody blocks scanned in drawings/charts
|
||||
TranslatedUnits int // unique CJK text units queued for translation
|
||||
SheetNamesRenamed int // CJK worksheet names renamed (Excel rules applied)
|
||||
AppliedTranslations int // unique units actually resolved
|
||||
GlossaryHits int // units resolved from the glossary
|
||||
LLMCalls int // LLM batch calls issued
|
||||
@@ -116,6 +117,7 @@ func (r *Report) Markdown() string {
|
||||
r.TextCells, r.InlineTextCells, r.DrawingBodies)
|
||||
fmt.Fprintf(&b, "| 翻译单元(去重后) | %d(术语库命中 %d,LLM 批次 %d 次) |\n",
|
||||
r.TranslatedUnits, r.GlossaryHits, r.LLMCalls)
|
||||
fmt.Fprintf(&b, "| Sheet 名称改名 | %d 个 |\n", r.SheetNamesRenamed)
|
||||
fmt.Fprintf(&b, "| 残留中文 | %d 处 |\n", r.ResidualCJKCount)
|
||||
fmt.Fprintf(&b, "| 数字 / 料号改动 | %d / %d 处 |\n", len(r.NumberMismatches), len(r.PartMismatches))
|
||||
if len(r.ResidualCJKSamples) > 0 {
|
||||
@@ -142,8 +144,11 @@ func truncate(s string, n int) string {
|
||||
return s
|
||||
}
|
||||
|
||||
// sharedStringsEntry is the canonical path of the shared string table.
|
||||
const sharedStringsEntry = "xl/sharedStrings.xml"
|
||||
// sharedStringsEntry and workbookEntry are the canonical package paths.
|
||||
const (
|
||||
sharedStringsEntry = "xl/sharedStrings.xml"
|
||||
workbookEntry = "xl/workbook.xml"
|
||||
)
|
||||
|
||||
var (
|
||||
siBlockRe = regexp.MustCompile(`(?s)<si(?:/>|>.*?</si>)`)
|
||||
@@ -153,6 +158,9 @@ var (
|
||||
txBodyRe = regexp.MustCompile(`(?s)<[A-Za-z0-9]+:txBody[^>]*>.*?</[A-Za-z0-9]+:txBody>`)
|
||||
paraRe = regexp.MustCompile(`(?s)<a:p(?:\s[^>]*)?>.*?</a:p>`)
|
||||
aTTagRe = regexp.MustCompile(`(?s)<a:t(?:\s[^>]*)?>(.*?)</a:t>`)
|
||||
// sheetNameRe captures the name attribute value of every <sheet> element:
|
||||
// group 1 = tag prefix through the opening quote, 2 = value, 3 = quote.
|
||||
sheetNameRe = regexp.MustCompile(`(<sheet\b[^>]*?\bname=")([^"]*)(")`)
|
||||
)
|
||||
|
||||
// editKind distinguishes the run that carries the unit translation from the
|
||||
@@ -162,6 +170,9 @@ type editKind int
|
||||
const (
|
||||
editCarrier editKind = iota
|
||||
editClear
|
||||
// editSheetName rewrites the name="..." attribute value of a <sheet>
|
||||
// element in workbook.xml.
|
||||
editSheetName
|
||||
)
|
||||
|
||||
// locatedEdit is one byte-range rewrite on an entry. tag is the element name
|
||||
@@ -173,9 +184,19 @@ type locatedEdit struct {
|
||||
tag string
|
||||
}
|
||||
|
||||
// scanKind tags what an entry's units are, so pass 3 can post-process sheet
|
||||
// names (Excel naming rules + quoted formula references).
|
||||
type scanKind int
|
||||
|
||||
const (
|
||||
scanText scanKind = iota
|
||||
scanSheets
|
||||
)
|
||||
|
||||
// scanJob holds everything to rewrite for one zip entry.
|
||||
type scanJob struct {
|
||||
entry int
|
||||
kind scanKind
|
||||
units []string // translatable CJK unit texts, scan order
|
||||
edits []locatedEdit // byte ranges, scan order
|
||||
}
|
||||
@@ -207,6 +228,7 @@ func Translate(ctx context.Context, data []byte, opt *Options, tr Translator) ([
|
||||
// ---- Pass 1: collect translatable units per entry ----
|
||||
entryData := make([][]byte, len(reader.File))
|
||||
jobs := map[int]*scanJob{}
|
||||
var sheetJob *scanJob // workbook.xml job: units are sheet names
|
||||
for i, f := range reader.File {
|
||||
raw, err := readZip(f)
|
||||
if err != nil {
|
||||
@@ -214,6 +236,15 @@ func Translate(ctx context.Context, data []byte, opt *Options, tr Translator) ([
|
||||
}
|
||||
entryData[i] = raw
|
||||
switch {
|
||||
case f.Name == workbookEntry:
|
||||
job, renamed := scanSheetNames(string(raw))
|
||||
report.SheetNamesRenamed = renamed
|
||||
if len(job.units) > 0 {
|
||||
job.entry = i
|
||||
job.kind = scanSheets
|
||||
jobs[i] = job
|
||||
sheetJob = job
|
||||
}
|
||||
case f.Name == sharedStringsEntry:
|
||||
job, scanned := scanStringBlocks(string(raw), siBlockRe, "t")
|
||||
report.TextCells += scanned
|
||||
@@ -385,13 +416,34 @@ func Translate(ctx context.Context, data []byte, opt *Options, tr Translator) ([
|
||||
idx int
|
||||
data []byte
|
||||
}
|
||||
// Sheet names: apply Excel naming rules (charset, 31-char cap, unique)
|
||||
// and keep quoted formula references ('旧名'!) pointing at renamed sheets.
|
||||
var renames [][2]string
|
||||
if sheetJob != nil {
|
||||
used := map[string]struct{}{}
|
||||
for _, old := range sheetJob.units {
|
||||
newName, ok := translations[old]
|
||||
if !ok {
|
||||
continue // oversized/skipped: keep the original sheet name
|
||||
}
|
||||
newName = sanitizeSheetName(newName, used)
|
||||
translations[old] = newName
|
||||
renames = append(renames, [2]string{old, newName})
|
||||
}
|
||||
report.SheetNamesRenamed = len(renames)
|
||||
}
|
||||
|
||||
out := make([]outEntry, 0, len(reader.File))
|
||||
for i := range reader.File {
|
||||
data := entryData[i]
|
||||
data := string(entryData[i])
|
||||
if job, ok := jobs[i]; ok {
|
||||
data = []byte(applyJob(string(data), job, translations, alias))
|
||||
data = applyJob(data, job, translations, alias)
|
||||
}
|
||||
out = append(out, outEntry{idx: i, data: data})
|
||||
for _, r := range renames {
|
||||
// Quoted formula references ('旧名'!) must follow the rename.
|
||||
data = strings.ReplaceAll(data, "'"+r[0]+"'!", "'"+r[1]+"'!")
|
||||
}
|
||||
out = append(out, outEntry{idx: i, data: []byte(data)})
|
||||
}
|
||||
sort.SliceStable(out, func(a, b int) bool { return out[a].idx < out[b].idx })
|
||||
|
||||
@@ -426,6 +478,30 @@ func Translate(ctx context.Context, data []byte, opt *Options, tr Translator) ([
|
||||
// towards the scanned total; blocks whose joined <t> text contains CJK become
|
||||
// units with one carrier edit (first <t>) and one clear edit per sibling <t>.
|
||||
// Phonetic <rPh> hints are excluded from both joining and editing.
|
||||
// scanSheetNames collects CJK worksheet names from workbook.xml. Each name
|
||||
// becomes one unit with a single edit covering the attribute value span.
|
||||
// Returns the count of names queued for renaming.
|
||||
func scanSheetNames(src string) (*scanJob, int) {
|
||||
job := &scanJob{}
|
||||
renamed := 0
|
||||
for _, loc := range sheetNameRe.FindAllStringSubmatchIndex(src, -1) {
|
||||
name := unescapeXMLText(src[loc[4]:loc[5]])
|
||||
if !hasCJK(name) {
|
||||
continue
|
||||
}
|
||||
unitIdx := len(job.units)
|
||||
job.units = append(job.units, name)
|
||||
job.edits = append(job.edits, locatedEdit{
|
||||
start: loc[4],
|
||||
end: loc[5],
|
||||
kind: editSheetName,
|
||||
unitIdx: unitIdx,
|
||||
})
|
||||
renamed++
|
||||
}
|
||||
return job, renamed
|
||||
}
|
||||
|
||||
func scanStringBlocks(src string, blockRe *regexp.Regexp, tag string) (*scanJob, int) {
|
||||
job := &scanJob{}
|
||||
scanned := 0
|
||||
@@ -543,6 +619,13 @@ func applyJob(src string, job *scanJob, translations map[string]string, alias fu
|
||||
repls := make([]repl, 0, len(job.edits))
|
||||
for _, e := range job.edits {
|
||||
switch e.kind {
|
||||
case editSheetName:
|
||||
// Sheet names replace the bare attribute value, no tag wrap.
|
||||
t, ok := translations[alias(job.units[e.unitIdx])]
|
||||
if !ok {
|
||||
continue // oversized/skipped unit: keep the original bytes
|
||||
}
|
||||
repls = append(repls, repl{e.start, e.end, escapeXMLText(t)})
|
||||
case editCarrier:
|
||||
t, ok := translations[alias(job.units[e.unitIdx])]
|
||||
if !ok {
|
||||
|
||||
Reference in new issue
Block a user