Skip to content

Commit 9af9f9c

Browse files
dmoranpslorber
andauthored
fix(content-docs): translate generated-index category titles in pagination links (facebook#11794)
Co-authored-by: sebastien <lorber.sebastien@gmail.com>
1 parent f60e255 commit 9af9f9c

3 files changed

Lines changed: 148 additions & 2 deletions

File tree

packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/translations.test.ts.snap

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ exports[`translateLoadedContent returns translated loaded content 1`] = `
168168
"sourceDirName": "",
169169
"tags": [],
170170
"title": "doc1 title",
171+
"unlisted": false,
171172
"version": "any",
172173
},
173174
{
@@ -188,6 +189,7 @@ exports[`translateLoadedContent returns translated loaded content 1`] = `
188189
"sourceDirName": "",
189190
"tags": [],
190191
"title": "doc2 title",
192+
"unlisted": false,
191193
"version": "any",
192194
},
193195
{
@@ -208,6 +210,7 @@ exports[`translateLoadedContent returns translated loaded content 1`] = `
208210
"sourceDirName": "",
209211
"tags": [],
210212
"title": "doc3 title",
213+
"unlisted": false,
211214
"version": "any",
212215
},
213216
{
@@ -228,6 +231,7 @@ exports[`translateLoadedContent returns translated loaded content 1`] = `
228231
"sourceDirName": "",
229232
"tags": [],
230233
"title": "doc4 title",
234+
"unlisted": false,
231235
"version": "any",
232236
},
233237
{
@@ -248,12 +252,14 @@ exports[`translateLoadedContent returns translated loaded content 1`] = `
248252
"sourceDirName": "",
249253
"tags": [],
250254
"title": "doc5 title",
255+
"unlisted": false,
251256
"version": "any",
252257
},
253258
],
254259
"drafts": [],
255260
"isLast": true,
256261
"label": "current label (translated)",
262+
"noIndex": false,
257263
"path": "/docs/",
258264
"routePriority": undefined,
259265
"sidebarFilePath": "any",
@@ -354,6 +360,7 @@ exports[`translateLoadedContent returns translated loaded content 1`] = `
354360
"sourceDirName": "",
355361
"tags": [],
356362
"title": "doc1 title",
363+
"unlisted": false,
357364
"version": "any",
358365
},
359366
{
@@ -374,6 +381,7 @@ exports[`translateLoadedContent returns translated loaded content 1`] = `
374381
"sourceDirName": "",
375382
"tags": [],
376383
"title": "doc2 title",
384+
"unlisted": false,
377385
"version": "any",
378386
},
379387
{
@@ -394,6 +402,7 @@ exports[`translateLoadedContent returns translated loaded content 1`] = `
394402
"sourceDirName": "",
395403
"tags": [],
396404
"title": "doc3 title",
405+
"unlisted": false,
397406
"version": "any",
398407
},
399408
{
@@ -414,6 +423,7 @@ exports[`translateLoadedContent returns translated loaded content 1`] = `
414423
"sourceDirName": "",
415424
"tags": [],
416425
"title": "doc4 title",
426+
"unlisted": false,
417427
"version": "any",
418428
},
419429
{
@@ -434,12 +444,14 @@ exports[`translateLoadedContent returns translated loaded content 1`] = `
434444
"sourceDirName": "",
435445
"tags": [],
436446
"title": "doc5 title",
447+
"unlisted": false,
437448
"version": "any",
438449
},
439450
],
440451
"drafts": [],
441452
"isLast": true,
442453
"label": "2.0.0 label (translated)",
454+
"noIndex": false,
443455
"path": "/docs/",
444456
"routePriority": undefined,
445457
"sidebarFilePath": "any",
@@ -540,6 +552,7 @@ exports[`translateLoadedContent returns translated loaded content 1`] = `
540552
"sourceDirName": "",
541553
"tags": [],
542554
"title": "doc1 title",
555+
"unlisted": false,
543556
"version": "any",
544557
},
545558
{
@@ -560,6 +573,7 @@ exports[`translateLoadedContent returns translated loaded content 1`] = `
560573
"sourceDirName": "",
561574
"tags": [],
562575
"title": "doc2 title",
576+
"unlisted": false,
563577
"version": "any",
564578
},
565579
{
@@ -580,6 +594,7 @@ exports[`translateLoadedContent returns translated loaded content 1`] = `
580594
"sourceDirName": "",
581595
"tags": [],
582596
"title": "doc3 title",
597+
"unlisted": false,
583598
"version": "any",
584599
},
585600
{
@@ -600,6 +615,7 @@ exports[`translateLoadedContent returns translated loaded content 1`] = `
600615
"sourceDirName": "",
601616
"tags": [],
602617
"title": "doc4 title",
618+
"unlisted": false,
603619
"version": "any",
604620
},
605621
{
@@ -620,12 +636,14 @@ exports[`translateLoadedContent returns translated loaded content 1`] = `
620636
"sourceDirName": "",
621637
"tags": [],
622638
"title": "doc5 title",
639+
"unlisted": false,
623640
"version": "any",
624641
},
625642
],
626643
"drafts": [],
627644
"isLast": true,
628645
"label": "1.0.0 label (translated)",
646+
"noIndex": false,
629647
"path": "/docs/",
630648
"routePriority": undefined,
631649
"sidebarFilePath": "any",

packages/docusaurus-plugin-content-docs/src/__tests__/translations.test.ts

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ import type {
1818
} from '@docusaurus/plugin-content-docs';
1919
import type {Sidebar} from '../sidebars/types';
2020

21-
function createSampleDoc(doc: Pick<DocMetadata, 'id'>): DocMetadata {
21+
function createSampleDoc(
22+
doc: Pick<DocMetadata, 'id'> & Partial<DocMetadata>,
23+
): DocMetadata {
2224
return {
2325
sourceDirName: '',
2426
draft: false,
27+
unlisted: false,
2528
tags: [],
2629
editUrl: 'any',
2730
lastUpdatedAt: 0,
@@ -50,6 +53,7 @@ function createSampleVersion(
5053
routePriority: undefined,
5154
sidebarFilePath: 'any',
5255
isLast: true,
56+
noIndex: false,
5357
contentPath: 'any',
5458
contentPathLocalized: 'any',
5559
tagsPath: '/tags/',
@@ -331,4 +335,80 @@ describe('translateLoadedContent', () => {
331335
translateLoadedContent(SampleLoadedContent, translationFiles),
332336
).toMatchSnapshot();
333337
});
338+
339+
it('translates pagination navigation titles for generated-index categories', () => {
340+
const content: LoadedContent = {
341+
loadedVersions: [
342+
createSampleVersion({
343+
versionName: CURRENT_VERSION_NAME,
344+
docs: [
345+
createSampleDoc({
346+
id: 'doc1',
347+
next: {
348+
title: 'Getting started',
349+
permalink: '/docs/category/getting-started-index-slug',
350+
},
351+
}),
352+
createSampleDoc({
353+
id: 'doc2',
354+
previous: {
355+
title: 'Getting started',
356+
permalink: '/docs/category/getting-started-index-slug',
357+
},
358+
next: {
359+
title: 'doc3 title',
360+
permalink: '/docs/doc3',
361+
},
362+
}),
363+
createSampleDoc({
364+
id: 'doc3',
365+
previous: {
366+
title: 'doc2 title',
367+
permalink: '/docs/doc2',
368+
},
369+
}),
370+
],
371+
}),
372+
],
373+
};
374+
375+
const translationFiles = getLoadedContentTranslationFiles(content);
376+
const translatedFiles = translationFiles.map((translationFile) =>
377+
updateTranslationFileMessages(
378+
translationFile,
379+
(message) => `${message} (translated)`,
380+
),
381+
);
382+
383+
const translated = translateLoadedContent(content, translatedFiles);
384+
const [doc1, doc2, doc3] = translated.loadedVersions[0]!.docs;
385+
386+
// doc1.next points to a generated-index category
387+
// => title should be translated
388+
expect(doc1!.next).toEqual({
389+
title: 'Getting started (translated)',
390+
permalink: '/docs/category/getting-started-index-slug',
391+
});
392+
393+
// doc2.previous points to a generated-index category
394+
// => title should be translated
395+
expect(doc2!.previous).toEqual({
396+
title: 'Getting started (translated)',
397+
permalink: '/docs/category/getting-started-index-slug',
398+
});
399+
400+
// doc2.next points to a regular doc
401+
// => title should NOT be changed, it's already translated from the i18n MDX
402+
expect(doc2!.next).toEqual({
403+
title: 'doc3 title',
404+
permalink: '/docs/doc3',
405+
});
406+
407+
// doc3.previous points to a regular doc
408+
// => title should NOT be changed, it's already translated from the i18n MDX
409+
expect(doc3!.previous).toEqual({
410+
title: 'doc2 title',
411+
permalink: '/docs/doc2',
412+
});
413+
});
334414
});

packages/docusaurus-plugin-content-docs/src/translations.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,16 +269,64 @@ function getVersionTranslationFiles(version: LoadedVersion): TranslationFile[] {
269269
},
270270
];
271271
}
272+
273+
// TODO Docusaurus v4 or later
274+
// this temporarily works, but it is not an ideal solution
275+
// The docs navigation can be computed and shouldn't be part of LoadedVersion
276+
// We need to derive the navigation from already translated content
277+
// See https://github.com/facebook/docusaurus/pull/11794
278+
function translateDocNavigation(
279+
docs: LoadedVersion['docs'],
280+
translatedSidebars: Sidebars,
281+
): LoadedVersion['docs'] {
282+
// Build a map of permalink -> translated label for generated-index categories
283+
const translatedLabelByPermalink = new Map<string, string>();
284+
for (const sidebar of Object.values(translatedSidebars)) {
285+
for (const category of collectSidebarCategories(sidebar)) {
286+
if (category.link?.type === 'generated-index') {
287+
translatedLabelByPermalink.set(category.link.permalink, category.label);
288+
}
289+
}
290+
}
291+
292+
if (translatedLabelByPermalink.size === 0) {
293+
return docs;
294+
}
295+
296+
return docs.map((doc) => {
297+
const previous =
298+
doc.previous && translatedLabelByPermalink.has(doc.previous.permalink)
299+
? {
300+
...doc.previous,
301+
title: translatedLabelByPermalink.get(doc.previous.permalink)!,
302+
}
303+
: doc.previous;
304+
const next =
305+
doc.next && translatedLabelByPermalink.has(doc.next.permalink)
306+
? {
307+
...doc.next,
308+
title: translatedLabelByPermalink.get(doc.next.permalink)!,
309+
}
310+
: doc.next;
311+
if (previous === doc.previous && next === doc.next) {
312+
return doc;
313+
}
314+
return {...doc, previous, next};
315+
});
316+
}
317+
272318
function translateVersion(
273319
version: LoadedVersion,
274320
translationFiles: {[fileName: string]: TranslationFile},
275321
): LoadedVersion {
276322
const versionTranslations =
277323
translationFiles[getVersionFileName(version.versionName)]!.content;
324+
const translatedSidebars = translateSidebars(version, versionTranslations);
278325
return {
279326
...version,
280327
label: versionTranslations['version.label']?.message ?? version.label,
281-
sidebars: translateSidebars(version, versionTranslations),
328+
sidebars: translatedSidebars,
329+
docs: translateDocNavigation(version.docs, translatedSidebars),
282330
};
283331
}
284332

0 commit comments

Comments
 (0)