Skip to content

Commit

Permalink
ability to translate generated-index title/description
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Dec 3, 2021
1 parent 3983181 commit 2daa173
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ Array [
"description": "The label for category Getting started in sidebar docs",
"message": "Getting started",
},
"sidebar.docs.category.Getting started.link.generated-index.description": Object {
"description": "The generated-index page description for category Getting started in sidebar docs",
"message": "Getting started index description",
},
"sidebar.docs.category.Getting started.link.generated-index.title": Object {
"description": "The generated-index page title for category Getting started in sidebar docs",
"message": "Getting started index title",
},
"sidebar.docs.link.Link label": Object {
"description": "The label for link Link label in sidebar docs, linking to https://facebook.com",
"message": "Link label",
Expand All @@ -25,6 +33,14 @@ Array [
"description": "The label for category Getting started in sidebar docs",
"message": "Getting started",
},
"sidebar.docs.category.Getting started.link.generated-index.description": Object {
"description": "The generated-index page description for category Getting started in sidebar docs",
"message": "Getting started index description",
},
"sidebar.docs.category.Getting started.link.generated-index.title": Object {
"description": "The generated-index page title for category Getting started in sidebar docs",
"message": "Getting started index title",
},
"sidebar.docs.link.Link label": Object {
"description": "The label for link Link label in sidebar docs, linking to https://facebook.com",
"message": "Link label",
Expand All @@ -42,6 +58,14 @@ Array [
"description": "The label for category Getting started in sidebar docs",
"message": "Getting started",
},
"sidebar.docs.category.Getting started.link.generated-index.description": Object {
"description": "The generated-index page description for category Getting started in sidebar docs",
"message": "Getting started index description",
},
"sidebar.docs.category.Getting started.link.generated-index.title": Object {
"description": "The generated-index page title for category Getting started in sidebar docs",
"message": "Getting started index title",
},
"sidebar.docs.link.Link label": Object {
"description": "The label for link Link label in sidebar docs, linking to https://facebook.com",
"message": "Link label",
Expand Down Expand Up @@ -177,6 +201,13 @@ Object {
},
],
"label": "Getting started (translated)",
"link": Object {
"description": "Getting started index description (translated)",
"permalink": "/docs/category/getting-started-index-slug",
"slug": "/category/getting-started-index-slug",
"title": "Getting started index title (translated)",
"type": "generated-index",
},
"type": "category",
},
Object {
Expand Down Expand Up @@ -317,6 +348,13 @@ Object {
},
],
"label": "Getting started (translated)",
"link": Object {
"description": "Getting started index description (translated)",
"permalink": "/docs/category/getting-started-index-slug",
"slug": "/category/getting-started-index-slug",
"title": "Getting started index title (translated)",
"type": "generated-index",
},
"type": "category",
},
Object {
Expand Down Expand Up @@ -457,6 +495,13 @@ Object {
},
],
"label": "Getting started (translated)",
"link": Object {
"description": "Getting started index description (translated)",
"permalink": "/docs/category/getting-started-index-slug",
"slug": "/category/getting-started-index-slug",
"title": "Getting started index title (translated)",
"type": "generated-index",
},
"type": "category",
},
Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ function createSampleVersion(
type: 'category',
label: 'Getting started',
collapsed: false,
link: {
type: 'generated-index',
slug: '/category/getting-started-index-slug',
permalink: '/docs/category/getting-started-index-slug',
title: 'Getting started index title',
description: 'Getting started index description',
},
items: [
{
type: 'doc',
Expand Down
82 changes: 74 additions & 8 deletions packages/docusaurus-plugin-content-docs/src/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
*/

import type {LoadedVersion, LoadedContent} from './types';
import type {Sidebar, Sidebars} from './sidebars/types';
import type {
Sidebar,
SidebarItemCategory,
SidebarItemCategoryLink,
Sidebars,
} from './sidebars/types';

import {chain, mapValues, keyBy} from 'lodash';
import {
Expand All @@ -21,6 +26,7 @@ import type {
} from '@docusaurus/types';
import {mergeTranslations} from '@docusaurus/utils';
import {CURRENT_VERSION_NAME} from './constants';
import {TranslationMessage} from '@docusaurus/types';

function getVersionFileName(versionName: string): string {
if (versionName === CURRENT_VERSION_NAME) {
Expand Down Expand Up @@ -96,14 +102,48 @@ function getSidebarTranslationFileContent(
sidebar: Sidebar,
sidebarName: string,
): TranslationFileContent {
type TranslationMessageEntry = [string, TranslationMessage];

const categories = collectSidebarCategories(sidebar);
const categoryContent: TranslationFileContent = chain(categories)
.keyBy((category) => `sidebar.${sidebarName}.category.${category.label}`)
.mapValues((category) => ({
message: category.label,
description: `The label for category ${category.label} in sidebar ${sidebarName}`,
}))
.value();

const categoryContent: TranslationFileContent = Object.fromEntries(
categories.flatMap((category) => {
const entries: TranslationMessageEntry[] = [];

entries.push([
`sidebar.${sidebarName}.category.${category.label}`,
{
message: category.label,
description: `The label for category ${category.label} in sidebar ${sidebarName}`,
},
]);

if (category.link) {
if (category.link.type === 'generated-index') {
if (category.link.title) {
entries.push([
`sidebar.${sidebarName}.category.${category.label}.link.generated-index.title`,
{
message: category.link.title,
description: `The generated-index page title for category ${category.label} in sidebar ${sidebarName}`,
},
]);
}
if (category.link.description) {
entries.push([
`sidebar.${sidebarName}.category.${category.label}.link.generated-index.description`,
{
message: category.link.description,
description: `The generated-index page description for category ${category.label} in sidebar ${sidebarName}`,
},
]);
}
}
}

return entries;
}),
);

const links = collectSidebarLinks(sidebar);
const linksContent: TranslationFileContent = chain(links)
Expand All @@ -126,13 +166,39 @@ function translateSidebar({
sidebarName: string;
sidebarsTranslations: TranslationFileContent;
}): Sidebar {
function transformSidebarCategoryLink(
category: SidebarItemCategory,
): SidebarItemCategoryLink | undefined {
if (!category.link) {
return undefined;
}
if (category.link.type === 'generated-index') {
const title =
sidebarsTranslations[
`sidebar.${sidebarName}.category.${category.label}.link.generated-index.title`
]?.message ?? category.link.title;
const description =
sidebarsTranslations[
`sidebar.${sidebarName}.category.${category.label}.link.generated-index.description`
]?.message ?? category.link.description;
return {
...category.link,
title,
description,
};
}
return category.link;
}

return transformSidebarItems(sidebar, (item) => {
if (item.type === 'category') {
const link = transformSidebarCategoryLink(item);
return {
...item,
label:
sidebarsTranslations[`sidebar.${sidebarName}.category.${item.label}`]
?.message ?? item.label,
...(link && {link}),
};
}
if (item.type === 'link') {
Expand Down

0 comments on commit 2daa173

Please sign in to comment.