1.excel下面的sheet名称未被翻译
deploy-hxtesting / build-and-deploy (push) Successful in 1m15s

2.预览翻译文件时无法预览图片!
3.上传文件都要存到知识库中!可被问答
4.在SIP翻译专家agent下上传文件后默认给提示文案可直接发送
This commit is contained in:
runny committed 2026-12-01 06:26:12 +08:00
1 parent 8c3d0ee31c
commit 90b843b4e8
9 files changed
+308 -75

No files matched your search

+35
View File
@@ -148,6 +148,41 @@ func collapseWhitespace(s string) string {
return strings.Join(strings.Fields(s), " ")
}
// sanitizeSheetName enforces Excel worksheet name rules on a translated
// sheet name: no : \ / ? * [ ] (or apostrophes, which would break quoted
// formula references), at most 31 runes, unique within the workbook. An
// empty translation falls back to "Sheet".
func sanitizeSheetName(name string, used map[string]struct{}) string {
var b strings.Builder
for _, r := range name {
switch r {
case ':', '\\', '/', '?', '*', '[', ']', '\'', '\u0000':
continue
}
b.WriteRune(r)
}
out := collapseWhitespace(b.String())
if len(out) > 31 {
out = out[:31]
}
if strings.TrimSpace(out) == "" {
out = "Sheet"
}
base := out
for i := 2; ; i++ {
if _, exists := used[out]; !exists {
break
}
suffix := " " + itoa(i)
if len(base)+len(suffix) > 31 {
base = base[:31-len(suffix)]
}
out = base + suffix
}
used[out] = struct{}{}
return out
}
// itoa is a tiny helper so this file needs no strconv import noise.
func itoa(i int) string {
if i == 0 {
+88 -5
View File
@@ -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 {
+68 -60
View File
@@ -73,66 +73,74 @@ var glossary = map[string]string{
"检验条件": "Inspection conditions",
"檢驗條件": "Inspection conditions",
// Common hardware and inspection nouns seen in the drawing text boxes
"螺母": "NUT",
"螺丝": "SCREW",
"螺钉": "SCREW",
"螺栓": "BOLT",
"万向轮": "Caster",
"腳杯": "Leveling Foot",
"脚杯": "Leveling Foot",
"接地": "Grounding",
"接地区域": "Grounding area",
"接地螺丝": "Grounding screw",
"接地螺絲": "Grounding screw",
"支撑件": "Support",
"支撐件": "Support",
"水平仪": "Spirit level",
"钢直尺": "Steel ruler",
"鋼直尺": "Steel ruler",
"卷尺": "Tape measure",
"塞尺": "Feeler gauge",
"检具": "Inspection gauge",
"檢具": "Inspection gauge",
"手电筒": "Flashlight",
"手电筒/目视/手触摸": "Flashlight/Visual/Touch by hand",
"手触摸": "Touch by hand",
"对角线": "Diagonal",
"對角線": "Diagonal",
"平面度": "Flatness",
"平行度": "Parallelism",
"底板平面度": "Bottom plate flatness",
"顶板平面度": "Top plate flatness",
"外宽": "Outer width",
"內宽": "Inner width",
"内宽": "Inner width",
"前门": "Front door",
"后门": "Rear door",
"前門": "Front door",
"後門": "Rear door",
"侧板": "Side panel",
"側板": "Side panel",
"顶板": "Top panel",
"底板": "Bottom panel",
"立柱": "Vertical post",
"螺纹孔": "Threaded hole",
"螺紋孔": "Threaded hole",
"方孔": "Square hole",
"葫芦孔": "Keyhole",
"焊道": "Weld bead",
"焊点": "Weld point",
"铜箔纸": "Copper foil paper",
"防烤胶带": "High-temperature tape",
"标签": "Label",
"標籤": "Label",
"SN标签": "SN label",
"机柜": "Rack",
"機櫃": "Rack",
"机柜本体": "Rack body",
"包装": "Packing",
"出厂检验": "Outgoing inspection",
"出货检验": "Outgoing inspection",
"组装": "Assembly",
"組裝": "Assembly",
"螺母": "NUT",
"螺丝": "SCREW",
"螺钉": "SCREW",
"螺栓": "BOLT",
"万向轮": "Caster",
"腳杯": "Leveling Foot",
"脚杯": "Leveling Foot",
"接地": "Grounding",
"接地区域": "Grounding area",
"接地螺丝": "Grounding screw",
"接地螺絲": "Grounding screw",
"支撑件": "Support",
"支撐件": "Support",
"水平仪": "Spirit level",
"钢直尺": "Steel ruler",
"鋼直尺": "Steel ruler",
"卷尺": "Tape measure",
"塞尺": "Feeler gauge",
"检具": "Inspection gauge",
"檢具": "Inspection gauge",
"手电筒": "Flashlight",
"手电筒/目视/手触摸": "Flashlight/Visual/Touch by hand",
"手触摸": "Touch by hand",
"对角线": "Diagonal",
"對角線": "Diagonal",
"平面度": "Flatness",
"平行度": "Parallelism",
"底板平面度": "Bottom plate flatness",
"顶板平面度": "Top plate flatness",
"外宽": "Outer width",
"內宽": "Inner width",
"内宽": "Inner width",
"前门": "Front door",
"后门": "Rear door",
"前門": "Front door",
"後門": "Rear door",
"侧板": "Side panel",
"側板": "Side panel",
"顶板": "Top panel",
"底板": "Bottom panel",
"立柱": "Vertical post",
"螺纹孔": "Threaded hole",
"螺紋孔": "Threaded hole",
"方孔": "Square hole",
"葫芦孔": "Keyhole",
"焊道": "Weld bead",
"焊点": "Weld point",
"铜箔纸": "Copper foil paper",
"防烤胶带": "High-temperature tape",
"标签": "Label",
"標籤": "Label",
"SN标签": "SN label",
"机柜": "Rack",
"機櫃": "Rack",
"机柜本体": "Rack body",
"包装": "Packing",
"出厂检验": "Outgoing inspection",
"出货检验": "Outgoing inspection",
"组装": "Assembly",
"組裝": "Assembly",
// Worksheet (sheet tab) names — workbook sheet tabs are renamed too
"更改履历": "Revision History",
"更改記錄": "Revision History",
"前置加工": "Pre-processing",
"全检项目": "Full Inspection Items",
"全檢項目": "Full Inspection Items",
"出货包装": "Outgoing Packaging",
"出貨包裝": "Outgoing Packaging",
"检验": "Inspection",
"檢驗": "Inspection",
"检查": "Check",
+5
View File
@@ -85,6 +85,11 @@ func collectPackageTexts(reader *zip.Reader) []string {
for _, loc := range re.FindAllStringIndex(string(raw), -1) {
appendBlockText(string(raw)[loc[0]:loc[1]])
}
case f.Name == workbookEntry:
src := string(raw)
for _, loc := range sheetNameRe.FindAllStringSubmatchIndex(src, -1) {
texts = append(texts, unescapeXMLText(src[loc[4]:loc[5]]))
}
case isDrawingXML(f.Name):
src := string(raw)
for _, bodyLoc := range txBodyRe.FindAllStringIndex(src, -1) {