Files
openclaw/scripts/docs-i18n/relocalize.go
Mason Huang eaeedbf1f9 fix(docs): finalize i18n postprocess before skip (#92668)
Summary:
- Merged fix(docs): finalize i18n postprocess before skip after ClawSweeper review.

Automerge notes:
- No ClawSweeper repair was needed after automerge opt-in.

Validation:
- ClawSweeper review passed for head ad79445835.
- Required merge gates passed before the squash merge.

Prepared head SHA: ad79445835
Review: https://github.com/openclaw/openclaw/pull/92668#issuecomment-4698629026

Co-authored-by: Mason Huang <masonxhuang@tencent.com>
Co-authored-by: clawsweeper[bot] <274271284+clawsweeper[bot]@users.noreply.github.com>
Approved-by: hxy91819
Co-authored-by: hxy91819 <8814856+hxy91819@users.noreply.github.com>
2026-06-13 13:17:03 +00:00

95 lines
2.1 KiB
Go

package main
import (
"os"
"strings"
)
func postprocessLocalizedDocs(docsRoot, targetLang string, localizedFiles []string) error {
if targetLang == "" || targetLang == "en" || len(localizedFiles) == 0 {
return nil
}
routes, err := loadRouteIndex(docsRoot, targetLang)
if err != nil {
return err
}
for _, path := range localizedFiles {
content, err := os.ReadFile(path)
if err != nil {
return err
}
frontMatter, body := splitFrontMatter(string(content))
rewrittenBody := routes.localizeBodyLinks(body)
updatedFrontMatter := setPostprocessVersion(frontMatter, localizedLinkPostprocessVersion)
if rewrittenBody == body && updatedFrontMatter == frontMatter {
continue
}
output := rewrittenBody
if updatedFrontMatter != "" {
output = "---\n" + updatedFrontMatter + "\n---\n\n" + rewrittenBody
}
if err := os.WriteFile(path, []byte(output), 0o644); err != nil {
return err
}
}
return nil
}
func setPostprocessVersion(frontMatter, version string) string {
if strings.TrimSpace(frontMatter) == "" {
return frontMatter
}
lines := strings.Split(frontMatter, "\n")
inXI18N := false
xi18nLine := -1
insertAt := -1
childIndent := " "
for i, line := range lines {
trimmed := strings.TrimSpace(line)
if trimmed == "x-i18n:" {
inXI18N = true
xi18nLine = i
insertAt = len(lines)
continue
}
if !inXI18N {
continue
}
if trimmed == "" {
continue
}
indent := leadingWhitespace(line)
if len(indent) <= len(leadingWhitespace(lines[xi18nLine])) {
insertAt = i
break
}
childIndent = indent
if strings.HasPrefix(trimmed, "postprocess_version:") {
lines[i] = indent + "postprocess_version: " + version
return strings.Join(lines, "\n")
}
}
if xi18nLine == -1 {
return frontMatter
}
if insertAt == -1 {
insertAt = len(lines)
}
lines = append(lines[:insertAt], append([]string{childIndent + "postprocess_version: " + version}, lines[insertAt:]...)...)
return strings.Join(lines, "\n")
}
func leadingWhitespace(text string) string {
return text[:len(text)-len(strings.TrimLeft(text, " \t"))]
}