feat(core): add i18n.localeConfigs.translate + skip translation process if i18n/<locale> dir doesn't exist (#11304)

This commit is contained in:
Sébastien Lorber 2025-07-07 14:55:46 +02:00 committed by GitHub
parent e0524a5c84
commit 1808945c1f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
36 changed files with 1061 additions and 366 deletions

View file

@ -70,6 +70,76 @@ exports[`docusaurus-plugin-content-pages loads simple pages 1`] = `
]
`;
exports[`docusaurus-plugin-content-pages loads simple pages with french translations (translate: false) 1`] = `
[
{
"permalink": "/fr/",
"source": "@site/src/pages/index.js",
"type": "jsx",
},
{
"permalink": "/fr/typescript",
"source": "@site/src/pages/typescript.tsx",
"type": "jsx",
},
{
"description": "Markdown index page",
"editUrl": undefined,
"frontMatter": {
"custom_frontMatter": "added by parseFrontMatter",
},
"lastUpdatedAt": undefined,
"lastUpdatedBy": undefined,
"permalink": "/fr/hello/",
"source": "@site/src/pages/hello/index.md",
"title": "Index",
"type": "mdx",
"unlisted": false,
},
{
"description": "my MDX page",
"editUrl": undefined,
"frontMatter": {
"custom_frontMatter": "added by parseFrontMatter",
"description": "my MDX page",
"slug": "/custom-mdx/slug",
"title": "MDX page",
},
"lastUpdatedAt": undefined,
"lastUpdatedBy": undefined,
"permalink": "/fr/custom-mdx/slug",
"source": "@site/src/pages/hello/mdxPage.mdx",
"title": "MDX page",
"type": "mdx",
"unlisted": false,
},
{
"permalink": "/fr/hello/translatedJs",
"source": "@site/src/pages/hello/translatedJs.js",
"type": "jsx",
},
{
"description": "translated Markdown page",
"editUrl": undefined,
"frontMatter": {
"custom_frontMatter": "added by parseFrontMatter",
},
"lastUpdatedAt": undefined,
"lastUpdatedBy": undefined,
"permalink": "/fr/hello/translatedMd",
"source": "@site/src/pages/hello/translatedMd.md",
"title": undefined,
"type": "mdx",
"unlisted": false,
},
{
"permalink": "/fr/hello/world",
"source": "@site/src/pages/hello/world.js",
"type": "jsx",
},
]
`;
exports[`docusaurus-plugin-content-pages loads simple pages with french translations 1`] = `
[
{

View file

@ -47,6 +47,25 @@ describe('docusaurus-plugin-content-pages', () => {
expect(pagesMetadata).toMatchSnapshot();
});
it('loads simple pages with french translations (translate: false)', async () => {
const siteDir = path.join(__dirname, '__fixtures__', 'website');
const context = await loadContext({siteDir, locale: 'fr'});
context.i18n.localeConfigs.fr.translate = false;
const plugin = await pluginContentPages(
context,
validateOptions({
validate: normalizePluginOptions,
options: {
path: 'src/pages',
},
}),
);
const pagesMetadata = await plugin.loadContent!();
expect(pagesMetadata).toMatchSnapshot();
});
it('loads simple pages with last update', async () => {
const siteDir = path.join(__dirname, '__fixtures__', 'website');
const context = await loadContext({siteDir});

View file

@ -21,10 +21,12 @@ import {
getEditUrl,
posixPath,
getPluginI18nPath,
getContentPathList,
getLocaleConfig,
type ContentPaths,
} from '@docusaurus/utils';
import {validatePageFrontMatter} from './frontMatter';
import type {LoadContext} from '@docusaurus/types';
import type {PagesContentPaths} from './types';
import type {
PluginOptions,
Metadata,
@ -37,29 +39,29 @@ export function createPagesContentPaths({
}: {
context: LoadContext;
options: PluginOptions;
}): PagesContentPaths {
}): ContentPaths {
const {siteDir, localizationDir} = context;
const shouldTranslate = getLocaleConfig(context.i18n).translate;
return {
contentPath: path.resolve(siteDir, options.path),
contentPathLocalized: getPluginI18nPath({
localizationDir,
pluginName: 'docusaurus-plugin-content-pages',
pluginId: options.id,
}),
contentPathLocalized: shouldTranslate
? getPluginI18nPath({
localizationDir,
pluginName: 'docusaurus-plugin-content-pages',
pluginId: options.id,
})
: undefined,
};
}
export function getContentPathList(contentPaths: PagesContentPaths): string[] {
return [contentPaths.contentPathLocalized, contentPaths.contentPath];
}
const isMarkdownSource = (source: string) =>
source.endsWith('.md') || source.endsWith('.mdx');
type LoadContentParams = {
context: LoadContext;
options: PluginOptions;
contentPaths: PagesContentPaths;
contentPaths: ContentPaths;
};
export async function loadPagesContent(
@ -158,7 +160,9 @@ async function processPageSourceFile(
} else if (typeof editUrl === 'string') {
const isLocalized = pagesDirPath === contentPaths.contentPathLocalized;
const fileContentPath =
isLocalized && options.editLocalizedFiles
isLocalized &&
options.editLocalizedFiles &&
contentPaths.contentPathLocalized
? contentPaths.contentPathLocalized
: contentPaths.contentPath;

View file

@ -12,15 +12,12 @@ import {
docuHash,
addTrailingPathSeparator,
createAbsoluteFilePathMatcher,
getContentPathList,
DEFAULT_PLUGIN_ID,
} from '@docusaurus/utils';
import {createMDXLoaderRule} from '@docusaurus/mdx-loader';
import {createAllRoutes} from './routes';
import {
createPagesContentPaths,
getContentPathList,
loadPagesContent,
} from './content';
import {createPagesContentPaths, loadPagesContent} from './content';
import type {LoadContext, Plugin} from '@docusaurus/types';
import type {
PluginOptions,

View file

@ -1,11 +0,0 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
export type PagesContentPaths = {
contentPath: string;
contentPathLocalized: string;
};