mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-15 10:07:33 +02:00
fix(content-docs): allow translating doc labels in sidebars.js (#7634)
This commit is contained in:
parent
5fe33bef06
commit
20e8e90762
9 changed files with 159 additions and 6 deletions
|
@ -16,10 +16,18 @@ exports[`getLoadedContentTranslationFiles returns translation files 1`] = `
|
||||||
"description": "The generated-index page title for category Getting started in sidebar docs",
|
"description": "The generated-index page title for category Getting started in sidebar docs",
|
||||||
"message": "Getting started index title",
|
"message": "Getting started index title",
|
||||||
},
|
},
|
||||||
|
"sidebar.docs.doc.Second doc translatable": {
|
||||||
|
"description": "The label for the doc item Second doc translatable in sidebar docs, linking to the doc doc2",
|
||||||
|
"message": "Second doc translatable",
|
||||||
|
},
|
||||||
"sidebar.docs.link.Link label": {
|
"sidebar.docs.link.Link label": {
|
||||||
"description": "The label for link Link label in sidebar docs, linking to https://facebook.com",
|
"description": "The label for link Link label in sidebar docs, linking to https://facebook.com",
|
||||||
"message": "Link label",
|
"message": "Link label",
|
||||||
},
|
},
|
||||||
|
"sidebar.otherSidebar.doc.Fifth doc translatable": {
|
||||||
|
"description": "The label for the doc item Fifth doc translatable in sidebar otherSidebar, linking to the doc doc5",
|
||||||
|
"message": "Fifth doc translatable",
|
||||||
|
},
|
||||||
"version.label": {
|
"version.label": {
|
||||||
"description": "The label for version current",
|
"description": "The label for version current",
|
||||||
"message": "current label",
|
"message": "current label",
|
||||||
|
@ -41,10 +49,18 @@ exports[`getLoadedContentTranslationFiles returns translation files 1`] = `
|
||||||
"description": "The generated-index page title for category Getting started in sidebar docs",
|
"description": "The generated-index page title for category Getting started in sidebar docs",
|
||||||
"message": "Getting started index title",
|
"message": "Getting started index title",
|
||||||
},
|
},
|
||||||
|
"sidebar.docs.doc.Second doc translatable": {
|
||||||
|
"description": "The label for the doc item Second doc translatable in sidebar docs, linking to the doc doc2",
|
||||||
|
"message": "Second doc translatable",
|
||||||
|
},
|
||||||
"sidebar.docs.link.Link label": {
|
"sidebar.docs.link.Link label": {
|
||||||
"description": "The label for link Link label in sidebar docs, linking to https://facebook.com",
|
"description": "The label for link Link label in sidebar docs, linking to https://facebook.com",
|
||||||
"message": "Link label",
|
"message": "Link label",
|
||||||
},
|
},
|
||||||
|
"sidebar.otherSidebar.doc.Fifth doc translatable": {
|
||||||
|
"description": "The label for the doc item Fifth doc translatable in sidebar otherSidebar, linking to the doc doc5",
|
||||||
|
"message": "Fifth doc translatable",
|
||||||
|
},
|
||||||
"version.label": {
|
"version.label": {
|
||||||
"description": "The label for version 2.0.0",
|
"description": "The label for version 2.0.0",
|
||||||
"message": "2.0.0 label",
|
"message": "2.0.0 label",
|
||||||
|
@ -66,10 +82,18 @@ exports[`getLoadedContentTranslationFiles returns translation files 1`] = `
|
||||||
"description": "The generated-index page title for category Getting started in sidebar docs",
|
"description": "The generated-index page title for category Getting started in sidebar docs",
|
||||||
"message": "Getting started index title",
|
"message": "Getting started index title",
|
||||||
},
|
},
|
||||||
|
"sidebar.docs.doc.Second doc translatable": {
|
||||||
|
"description": "The label for the doc item Second doc translatable in sidebar docs, linking to the doc doc2",
|
||||||
|
"message": "Second doc translatable",
|
||||||
|
},
|
||||||
"sidebar.docs.link.Link label": {
|
"sidebar.docs.link.Link label": {
|
||||||
"description": "The label for link Link label in sidebar docs, linking to https://facebook.com",
|
"description": "The label for link Link label in sidebar docs, linking to https://facebook.com",
|
||||||
"message": "Link label",
|
"message": "Link label",
|
||||||
},
|
},
|
||||||
|
"sidebar.otherSidebar.doc.Fifth doc translatable": {
|
||||||
|
"description": "The label for the doc item Fifth doc translatable in sidebar otherSidebar, linking to the doc doc5",
|
||||||
|
"message": "Fifth doc translatable",
|
||||||
|
},
|
||||||
"version.label": {
|
"version.label": {
|
||||||
"description": "The label for version 1.0.0",
|
"description": "The label for version 1.0.0",
|
||||||
"message": "1.0.0 label",
|
"message": "1.0.0 label",
|
||||||
|
@ -214,6 +238,8 @@ exports[`translateLoadedContent returns translated loaded content 1`] = `
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "doc2",
|
"id": "doc2",
|
||||||
|
"label": "Second doc translatable (translated)",
|
||||||
|
"translatable": true,
|
||||||
"type": "doc",
|
"type": "doc",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -248,7 +274,9 @@ exports[`translateLoadedContent returns translated loaded content 1`] = `
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "doc5",
|
"id": "doc5",
|
||||||
"type": "doc",
|
"label": "Fifth doc translatable (translated)",
|
||||||
|
"translatable": true,
|
||||||
|
"type": "ref",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
@ -386,6 +414,8 @@ exports[`translateLoadedContent returns translated loaded content 1`] = `
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "doc2",
|
"id": "doc2",
|
||||||
|
"label": "Second doc translatable (translated)",
|
||||||
|
"translatable": true,
|
||||||
"type": "doc",
|
"type": "doc",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -420,7 +450,9 @@ exports[`translateLoadedContent returns translated loaded content 1`] = `
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "doc5",
|
"id": "doc5",
|
||||||
"type": "doc",
|
"label": "Fifth doc translatable (translated)",
|
||||||
|
"translatable": true,
|
||||||
|
"type": "ref",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
@ -558,6 +590,8 @@ exports[`translateLoadedContent returns translated loaded content 1`] = `
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "doc2",
|
"id": "doc2",
|
||||||
|
"label": "Second doc translatable (translated)",
|
||||||
|
"translatable": true,
|
||||||
"type": "doc",
|
"type": "doc",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -592,7 +626,9 @@ exports[`translateLoadedContent returns translated loaded content 1`] = `
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "doc5",
|
"id": "doc5",
|
||||||
"type": "doc",
|
"label": "Fifth doc translatable (translated)",
|
||||||
|
"translatable": true,
|
||||||
|
"type": "ref",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
|
@ -86,6 +86,8 @@ function createSampleVersion(
|
||||||
{
|
{
|
||||||
type: 'doc',
|
type: 'doc',
|
||||||
id: 'doc2',
|
id: 'doc2',
|
||||||
|
label: 'Second doc translatable',
|
||||||
|
translatable: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'link',
|
type: 'link',
|
||||||
|
@ -109,8 +111,10 @@ function createSampleVersion(
|
||||||
id: 'doc4',
|
id: 'doc4',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'doc',
|
type: 'ref',
|
||||||
id: 'doc5',
|
id: 'doc5',
|
||||||
|
label: 'Fifth doc translatable',
|
||||||
|
translatable: true,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,5 +1,52 @@
|
||||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||||
|
|
||||||
|
exports[`normalization adds a translatable marker for labels defined in sidebars.js 1`] = `
|
||||||
|
{
|
||||||
|
"sidebar": [
|
||||||
|
{
|
||||||
|
"id": "google",
|
||||||
|
"label": "Google",
|
||||||
|
"translatable": true,
|
||||||
|
"type": "doc",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"id": "doc1",
|
||||||
|
"type": "doc",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "doc2",
|
||||||
|
"type": "doc",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"label": "Category 1",
|
||||||
|
"type": "category",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"id": "doc3",
|
||||||
|
"type": "doc",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "doc4",
|
||||||
|
"type": "doc",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "msft",
|
||||||
|
"label": "Microsoft",
|
||||||
|
"translatable": true,
|
||||||
|
"type": "ref",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"label": "Category 2",
|
||||||
|
"type": "category",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`normalization normalizes shorthands 1`] = `
|
exports[`normalization normalizes shorthands 1`] = `
|
||||||
{
|
{
|
||||||
"sidebar": [
|
"sidebar": [
|
||||||
|
|
|
@ -91,4 +91,30 @@ describe('normalization', () => {
|
||||||
`"Invalid sidebar items collection \`"item"\` in sidebar sidebar: it must either be an array of sidebar items or a shorthand notation (which doesn't contain a \`type\` property). See https://docusaurus.io/docs/sidebar/items for all valid syntaxes."`,
|
`"Invalid sidebar items collection \`"item"\` in sidebar sidebar: it must either be an array of sidebar items or a shorthand notation (which doesn't contain a \`type\` property). See https://docusaurus.io/docs/sidebar/items for all valid syntaxes."`,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('adds a translatable marker for labels defined in sidebars.js', () => {
|
||||||
|
expect(
|
||||||
|
normalizeSidebars({
|
||||||
|
sidebar: [
|
||||||
|
{
|
||||||
|
type: 'doc',
|
||||||
|
id: 'google',
|
||||||
|
label: 'Google',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'Category 1': ['doc1', 'doc2'],
|
||||||
|
'Category 2': [
|
||||||
|
'doc3',
|
||||||
|
'doc4',
|
||||||
|
{
|
||||||
|
type: 'ref',
|
||||||
|
id: 'msft',
|
||||||
|
label: 'Microsoft',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
).toMatchSnapshot();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -44,6 +44,12 @@ export function normalizeItem(
|
||||||
// This will never throw anyways
|
// This will never throw anyways
|
||||||
return normalizeSidebar(item, 'sidebar items slice');
|
return normalizeSidebar(item, 'sidebar items slice');
|
||||||
}
|
}
|
||||||
|
if (
|
||||||
|
(item.type === 'doc' || item.type === 'ref') &&
|
||||||
|
typeof item.label === 'string'
|
||||||
|
) {
|
||||||
|
return [{...item, translatable: true}];
|
||||||
|
}
|
||||||
if (item.type === 'category') {
|
if (item.type === 'category') {
|
||||||
const normalizedCategory: NormalizedSidebarItemCategory = {
|
const normalizedCategory: NormalizedSidebarItemCategory = {
|
||||||
...item,
|
...item,
|
||||||
|
|
|
@ -27,6 +27,11 @@ export type SidebarItemDoc = SidebarItemBase & {
|
||||||
type: 'doc' | 'ref';
|
type: 'doc' | 'ref';
|
||||||
label?: string;
|
label?: string;
|
||||||
id: string;
|
id: string;
|
||||||
|
/**
|
||||||
|
* This is an internal marker. Items with labels defined in the config needs
|
||||||
|
* to be translated with JSON
|
||||||
|
*/
|
||||||
|
translatable?: true;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type SidebarItemHtml = SidebarItemBase & {
|
export type SidebarItemHtml = SidebarItemBase & {
|
||||||
|
@ -94,7 +99,7 @@ export type SidebarCategoriesShorthand = {
|
||||||
};
|
};
|
||||||
|
|
||||||
export type SidebarItemConfig =
|
export type SidebarItemConfig =
|
||||||
| SidebarItemDoc
|
| Omit<SidebarItemDoc, 'translatable'>
|
||||||
| SidebarItemHtml
|
| SidebarItemHtml
|
||||||
| SidebarItemLink
|
| SidebarItemLink
|
||||||
| SidebarItemAutogenerated
|
| SidebarItemAutogenerated
|
||||||
|
|
|
@ -81,6 +81,9 @@ export function collectSidebarCategories(
|
||||||
export function collectSidebarLinks(sidebar: Sidebar): SidebarItemLink[] {
|
export function collectSidebarLinks(sidebar: Sidebar): SidebarItemLink[] {
|
||||||
return collectSidebarItemsOfType('link', sidebar);
|
return collectSidebarItemsOfType('link', sidebar);
|
||||||
}
|
}
|
||||||
|
export function collectSidebarRefs(sidebar: Sidebar): SidebarItemDoc[] {
|
||||||
|
return collectSidebarItemsOfType('ref', sidebar);
|
||||||
|
}
|
||||||
|
|
||||||
// /!\ docId order matters for navigation!
|
// /!\ docId order matters for navigation!
|
||||||
export function collectSidebarDocIds(sidebar: Sidebar): string[] {
|
export function collectSidebarDocIds(sidebar: Sidebar): string[] {
|
||||||
|
|
|
@ -47,6 +47,7 @@ const sidebarItemDocSchema = sidebarItemBaseSchema.append<SidebarItemDoc>({
|
||||||
type: Joi.string().valid('doc', 'ref').required(),
|
type: Joi.string().valid('doc', 'ref').required(),
|
||||||
id: Joi.string().required(),
|
id: Joi.string().required(),
|
||||||
label: Joi.string(),
|
label: Joi.string(),
|
||||||
|
translatable: Joi.boolean(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const sidebarItemHtmlSchema = sidebarItemBaseSchema.append<SidebarItemHtml>({
|
const sidebarItemHtmlSchema = sidebarItemBaseSchema.append<SidebarItemHtml>({
|
||||||
|
|
|
@ -12,6 +12,8 @@ import {
|
||||||
collectSidebarCategories,
|
collectSidebarCategories,
|
||||||
transformSidebarItems,
|
transformSidebarItems,
|
||||||
collectSidebarLinks,
|
collectSidebarLinks,
|
||||||
|
collectSidebarDocItems,
|
||||||
|
collectSidebarRefs,
|
||||||
} from './sidebars/utils';
|
} from './sidebars/utils';
|
||||||
import type {
|
import type {
|
||||||
LoadedVersion,
|
LoadedVersion,
|
||||||
|
@ -111,7 +113,22 @@ function getSidebarTranslationFileContent(
|
||||||
]),
|
]),
|
||||||
);
|
);
|
||||||
|
|
||||||
return mergeTranslations([categoryContent, linksContent]);
|
const docs = collectSidebarDocItems(sidebar)
|
||||||
|
.concat(collectSidebarRefs(sidebar))
|
||||||
|
.filter((item) => item.translatable);
|
||||||
|
const docLinksContent: TranslationFileContent = Object.fromEntries(
|
||||||
|
docs.map((doc) => [
|
||||||
|
`sidebar.${sidebarName}.doc.${doc.label!}`,
|
||||||
|
{
|
||||||
|
message: doc.label!,
|
||||||
|
description: `The label for the doc item ${doc.label!} in sidebar ${sidebarName}, linking to the doc ${
|
||||||
|
doc.id
|
||||||
|
}`,
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
|
||||||
|
return mergeTranslations([categoryContent, linksContent, docLinksContent]);
|
||||||
}
|
}
|
||||||
|
|
||||||
function translateSidebar({
|
function translateSidebar({
|
||||||
|
@ -166,6 +183,14 @@ function translateSidebar({
|
||||||
?.message ?? item.label,
|
?.message ?? item.label,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
if ((item.type === 'doc' || item.type === 'ref') && item.translatable) {
|
||||||
|
return {
|
||||||
|
...item,
|
||||||
|
label:
|
||||||
|
sidebarsTranslations[`sidebar.${sidebarName}.doc.${item.label!}`]
|
||||||
|
?.message ?? item.label,
|
||||||
|
};
|
||||||
|
}
|
||||||
return item;
|
return item;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue