Skip to content

Commit efd98bb

Browse files
fix: make docs i18n frontmatter translation resilient
1 parent 019ffcf commit efd98bb

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

scripts/docs-i18n/main_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,31 @@ func TestTranslateSnippetFallsBackWhenFrontmatterTranslatorFails(t *testing.T) {
278278
}
279279
}
280280

281+
func TestTranslateSnippetCachesDocumentSourcePath(t *testing.T) {
282+
t.Parallel()
283+
284+
tm := &TranslationMemory{entries: map[string]TMEntry{}}
285+
source := "Gateway"
286+
segmentID := "gateway/index.md:frontmatter:title"
287+
288+
translated, err := translateSnippet(context.Background(), fakeDocsTranslator{}, tm, segmentID, source, "en", "zh-CN")
289+
if err != nil {
290+
t.Fatalf("translateSnippet returned error: %v", err)
291+
}
292+
if translated != source {
293+
t.Fatalf("unexpected translation %q", translated)
294+
}
295+
296+
cacheKey := cacheKey(cacheNamespace(), "en", "zh-CN", segmentID, hashText(source))
297+
entry, ok := tm.Get(cacheKey)
298+
if !ok {
299+
t.Fatal("expected successful frontmatter translation to be cached")
300+
}
301+
if entry.SourcePath != "gateway/index.md" {
302+
t.Fatalf("expected document source path, got %q", entry.SourcePath)
303+
}
304+
}
305+
281306
func TestValidateNoTranslationTranscriptArtifacts(t *testing.T) {
282307
t.Parallel()
283308

scripts/docs-i18n/process.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,14 @@ func translateSnippet(ctx context.Context, translator docsTranslator, tm *Transl
223223
translated = textValue
224224
shouldCache = false
225225
}
226+
sourcePath := segmentID
227+
if path, _, ok := strings.Cut(segmentID, ":frontmatter:"); ok {
228+
sourcePath = path
229+
}
226230
entry := TMEntry{
227231
CacheKey: ck,
228232
SegmentID: segmentID,
229-
SourcePath: segmentID,
233+
SourcePath: sourcePath,
230234
TextHash: textHash,
231235
Text: textValue,
232236
Translated: translated,

0 commit comments

Comments
 (0)