Skip to content

Commit 1808945

Browse files
authored
feat(core): add i18n.localeConfigs.translate + skip translation process if i18n/<locale> dir doesn't exist (#11304)
1 parent e0524a5 commit 1808945

File tree

36 files changed

+1062
-367
lines changed

36 files changed

+1062
-367
lines changed

packages/docusaurus-plugin-content-blog/src/__tests__/index.test.ts

Lines changed: 105 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
*/
77

88
import {jest} from '@jest/globals';
9-
import path from 'path';
9+
import * as path from 'path';
1010
import {normalizePluginOptions} from '@docusaurus/utils-validation';
1111
import {
1212
posixPath,
1313
getFileCommitDate,
1414
LAST_UPDATE_FALLBACK,
15+
getLocaleConfig,
1516
} from '@docusaurus/utils';
1617
import {DEFAULT_FUTURE_CONFIG} from '@docusaurus/core/src/server/configValidation';
1718
import pluginContentBlog from '../index';
@@ -22,6 +23,7 @@ import type {
2223
I18n,
2324
Validate,
2425
MarkdownConfig,
26+
I18nLocaleConfig,
2527
} from '@docusaurus/types';
2628
import type {
2729
BlogPost,
@@ -67,7 +69,10 @@ Available blog post titles are:\n- ${blogPosts
6769
return post;
6870
}
6971

70-
function getI18n(locale: string): I18n {
72+
function getI18n(
73+
locale: string,
74+
localeConfigOptions?: Partial<I18nLocaleConfig>,
75+
): I18n {
7176
return {
7277
currentLocale: locale,
7378
locales: [locale],
@@ -80,6 +85,8 @@ function getI18n(locale: string): I18n {
8085
htmlLang: locale,
8186
direction: 'ltr',
8287
path: locale,
88+
translate: true,
89+
...localeConfigOptions,
8390
},
8491
},
8592
};
@@ -94,13 +101,14 @@ const BaseEditUrl = 'https://baseEditUrl.com/edit';
94101
const getPlugin = async (
95102
siteDir: string,
96103
pluginOptions: Partial<PluginOptions> = {},
97-
i18n: I18n = DefaultI18N,
104+
i18nOptions: Partial<I18n> = {},
98105
) => {
106+
const i18n = {...DefaultI18N, ...i18nOptions};
99107
const generatedFilesDir: string = path.resolve(siteDir, '.docusaurus');
100108
const localizationDir = path.join(
101109
siteDir,
102110
i18n.path,
103-
i18n.localeConfigs[i18n.currentLocale]!.path,
111+
getLocaleConfig(i18n).path,
104112
);
105113
const siteConfig = {
106114
title: 'Hello',
@@ -153,20 +161,34 @@ const getBlogTags = async (
153161
};
154162

155163
describe('blog plugin', () => {
156-
it('getPathsToWatch returns right files', async () => {
157-
const siteDir = path.join(__dirname, '__fixtures__', 'website');
158-
const plugin = await getPlugin(siteDir);
159-
const pathsToWatch = plugin.getPathsToWatch!();
160-
const relativePathsToWatch = pathsToWatch.map((p) =>
161-
posixPath(path.relative(siteDir, p)),
162-
);
163-
expect(relativePathsToWatch).toEqual([
164-
'i18n/en/docusaurus-plugin-content-blog/authors.yml',
165-
'i18n/en/docusaurus-plugin-content-blog/tags.yml',
166-
'blog/tags.yml',
167-
'i18n/en/docusaurus-plugin-content-blog/**/*.{md,mdx}',
168-
'blog/**/*.{md,mdx}',
169-
]);
164+
describe('getPathsToWatch', () => {
165+
async function runTest({translate}: {translate: boolean}) {
166+
const siteDir = path.join(__dirname, '__fixtures__', 'website');
167+
const plugin = await getPlugin(siteDir, {}, getI18n('en', {translate}));
168+
const pathsToWatch = plugin.getPathsToWatch!();
169+
return pathsToWatch.map((p) => posixPath(path.relative(siteDir, p)));
170+
}
171+
172+
it('getPathsToWatch returns right files', async () => {
173+
const relativePathsToWatch = await runTest({translate: true});
174+
expect(relativePathsToWatch).toEqual([
175+
'i18n/en/docusaurus-plugin-content-blog/authors.yml',
176+
'i18n/en/docusaurus-plugin-content-blog/tags.yml',
177+
// 'blog/authors.yml', // TODO weird that it's not here but tags is?
178+
'blog/tags.yml',
179+
'i18n/en/docusaurus-plugin-content-blog/**/*.{md,mdx}',
180+
'blog/**/*.{md,mdx}',
181+
]);
182+
});
183+
184+
it('getPathsToWatch returns right files (translate: false)', async () => {
185+
const relativePathsToWatch = await runTest({translate: false});
186+
expect(relativePathsToWatch).toEqual([
187+
'blog/authors.yml',
188+
'blog/tags.yml',
189+
'blog/**/*.{md,mdx}',
190+
]);
191+
});
170192
});
171193

172194
it('builds a simple website', async () => {
@@ -377,6 +399,54 @@ describe('blog plugin', () => {
377399
});
378400
});
379401

402+
describe('i18n config translate is wired properly', () => {
403+
async function runTest({translate}: {translate: boolean}) {
404+
const siteDir = path.join(__dirname, '__fixtures__', 'website');
405+
const blogPosts = await getBlogPosts(
406+
siteDir,
407+
{},
408+
getI18n('en', {translate}),
409+
);
410+
411+
// Simpler to snapshot
412+
return blogPosts.map((post) => post.metadata.title);
413+
}
414+
415+
it('works with translate: false', async () => {
416+
await expect(runTest({translate: false})).resolves.toMatchInlineSnapshot(`
417+
[
418+
"test links",
419+
"MDX Blog Sample with require calls",
420+
"Full Blog Sample",
421+
"Complex Slug",
422+
"Simple Slug",
423+
"draft",
424+
"unlisted",
425+
"some heading",
426+
"date-matter",
427+
"Happy 1st Birthday Slash!",
428+
]
429+
`);
430+
});
431+
432+
it('works with translate: true', async () => {
433+
await expect(runTest({translate: true})).resolves.toMatchInlineSnapshot(`
434+
[
435+
"test links",
436+
"MDX Blog Sample with require calls",
437+
"Full Blog Sample",
438+
"Complex Slug",
439+
"Simple Slug",
440+
"draft",
441+
"unlisted",
442+
"some heading",
443+
"date-matter",
444+
"Happy 1st Birthday Slash! (translated)",
445+
]
446+
`);
447+
});
448+
});
449+
380450
it('handles edit URL with editLocalizedBlogs: true', async () => {
381451
const siteDir = path.join(__dirname, '__fixtures__', 'website');
382452
const blogPosts = await getBlogPosts(siteDir, {editLocalizedFiles: true});
@@ -390,6 +460,23 @@ describe('blog plugin', () => {
390460
);
391461
});
392462

463+
it('handles edit URL with editLocalizedBlogs: true and translate: false', async () => {
464+
const siteDir = path.join(__dirname, '__fixtures__', 'website');
465+
const blogPosts = await getBlogPosts(
466+
siteDir,
467+
{editLocalizedFiles: true},
468+
getI18n('en', {translate: false}),
469+
);
470+
471+
const localizedBlogPost = blogPosts.find(
472+
(v) => v.metadata.title === 'Happy 1st Birthday Slash!',
473+
)!;
474+
475+
expect(localizedBlogPost.metadata.editUrl).toBe(
476+
`${BaseEditUrl}/blog/2018-12-14-Happy-First-Birthday-Slash.md`,
477+
);
478+
});
479+
393480
it('handles edit URL with editUrl function', async () => {
394481
const siteDir = path.join(__dirname, '__fixtures__', 'website');
395482

packages/docusaurus-plugin-content-blog/src/blogUtils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,9 @@ async function processBlogSourceFile(
323323
} else if (typeof editUrl === 'string') {
324324
const isLocalized = blogDirPath === contentPaths.contentPathLocalized;
325325
const fileContentPath =
326-
isLocalized && options.editLocalizedFiles
326+
isLocalized &&
327+
options.editLocalizedFiles &&
328+
contentPaths.contentPathLocalized
327329
? contentPaths.contentPathLocalized
328330
: contentPaths.contentPath;
329331

packages/docusaurus-plugin-content-blog/src/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
getDataFilePath,
2020
DEFAULT_PLUGIN_ID,
2121
resolveMarkdownLinkPathname,
22+
getLocaleConfig,
2223
} from '@docusaurus/utils';
2324
import {getTagsFilePathsToWatch} from '@docusaurus/utils-validation';
2425
import {createMDXLoaderItem} from '@docusaurus/mdx-loader';
@@ -73,13 +74,16 @@ export default async function pluginContentBlog(
7374

7475
const {baseUrl} = siteConfig;
7576

77+
const shouldTranslate = getLocaleConfig(context.i18n).translate;
7678
const contentPaths: BlogContentPaths = {
7779
contentPath: path.resolve(siteDir, options.path),
78-
contentPathLocalized: getPluginI18nPath({
79-
localizationDir,
80-
pluginName: PluginName,
81-
pluginId: options.id,
82-
}),
80+
contentPathLocalized: shouldTranslate
81+
? getPluginI18nPath({
82+
localizationDir,
83+
pluginName: PluginName,
84+
pluginId: options.id,
85+
})
86+
: undefined,
8387
};
8488
const pluginId = options.id ?? DEFAULT_PLUGIN_ID;
8589

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

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,17 +2005,6 @@ exports[`simple website content: route config 1`] = `
20052005
]
20062006
`;
20072007

2008-
exports[`simple website getPathToWatch 1`] = `
2009-
[
2010-
"sidebars.json",
2011-
"i18n/en/docusaurus-plugin-content-docs/current/**/*.{md,mdx}",
2012-
"docs/**/*.{md,mdx}",
2013-
"i18n/en/docusaurus-plugin-content-docs/current/tags.yml",
2014-
"docs/tags.yml",
2015-
"docs/**/_category_.{json,yml,yaml}",
2016-
]
2017-
`;
2018-
20192008
exports[`site with custom sidebar items generator sidebar is autogenerated according to a custom sidebarItemsGenerator 1`] = `
20202009
{
20212010
"defaultSidebar": [
@@ -3327,23 +3316,6 @@ exports[`versioned website (community) content: route config 1`] = `
33273316
]
33283317
`;
33293318

3330-
exports[`versioned website (community) getPathToWatch 1`] = `
3331-
[
3332-
"community_sidebars.json",
3333-
"i18n/en/docusaurus-plugin-content-docs-community/current/**/*.{md,mdx}",
3334-
"community/**/*.{md,mdx}",
3335-
"i18n/en/docusaurus-plugin-content-docs-community/current/tags.yml",
3336-
"community/tags.yml",
3337-
"community/**/_category_.{json,yml,yaml}",
3338-
"community_versioned_sidebars/version-1.0.0-sidebars.json",
3339-
"i18n/en/docusaurus-plugin-content-docs-community/version-1.0.0/**/*.{md,mdx}",
3340-
"community_versioned_docs/version-1.0.0/**/*.{md,mdx}",
3341-
"i18n/en/docusaurus-plugin-content-docs-community/version-1.0.0/tags.yml",
3342-
"community_versioned_docs/version-1.0.0/tags.yml",
3343-
"community_versioned_docs/version-1.0.0/**/_category_.{json,yml,yaml}",
3344-
]
3345-
`;
3346-
33473319
exports[`versioned website content 1`] = `
33483320
{
33493321
"description": "This is next version of bar.",
@@ -5209,32 +5181,3 @@ exports[`versioned website content: withSlugs version sidebars 1`] = `
52095181
],
52105182
}
52115183
`;
5212-
5213-
exports[`versioned website getPathToWatch 1`] = `
5214-
[
5215-
"sidebars.json",
5216-
"i18n/en/docusaurus-plugin-content-docs/current/**/*.{md,mdx}",
5217-
"docs/**/*.{md,mdx}",
5218-
"i18n/en/docusaurus-plugin-content-docs/current/tags.yml",
5219-
"docs/tags.yml",
5220-
"docs/**/_category_.{json,yml,yaml}",
5221-
"versioned_sidebars/version-1.0.1-sidebars.json",
5222-
"i18n/en/docusaurus-plugin-content-docs/version-1.0.1/**/*.{md,mdx}",
5223-
"versioned_docs/version-1.0.1/**/*.{md,mdx}",
5224-
"i18n/en/docusaurus-plugin-content-docs/version-1.0.1/tags.yml",
5225-
"versioned_docs/version-1.0.1/tags.yml",
5226-
"versioned_docs/version-1.0.1/**/_category_.{json,yml,yaml}",
5227-
"versioned_sidebars/version-1.0.0-sidebars.json",
5228-
"i18n/en/docusaurus-plugin-content-docs/version-1.0.0/**/*.{md,mdx}",
5229-
"versioned_docs/version-1.0.0/**/*.{md,mdx}",
5230-
"i18n/en/docusaurus-plugin-content-docs/version-1.0.0/tags.yml",
5231-
"versioned_docs/version-1.0.0/tags.yml",
5232-
"versioned_docs/version-1.0.0/**/_category_.{json,yml,yaml}",
5233-
"versioned_sidebars/version-withSlugs-sidebars.json",
5234-
"i18n/en/docusaurus-plugin-content-docs/version-withSlugs/**/*.{md,mdx}",
5235-
"versioned_docs/version-withSlugs/**/*.{md,mdx}",
5236-
"i18n/en/docusaurus-plugin-content-docs/version-withSlugs/tags.yml",
5237-
"versioned_docs/version-withSlugs/tags.yml",
5238-
"versioned_docs/version-withSlugs/**/_category_.{json,yml,yaml}",
5239-
]
5240-
`;
384 Bytes
Binary file not shown.

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

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
posixPath,
1414
DEFAULT_PLUGIN_ID,
1515
LAST_UPDATE_FALLBACK,
16+
getLocaleConfig,
1617
} from '@docusaurus/utils';
1718
import {getTagsFile} from '@docusaurus/utils-validation';
1819
import {createSidebarsUtils} from '../sidebars/utils';
@@ -842,7 +843,11 @@ describe('simple site', () => {
842843

843844
describe('versioned site', () => {
844845
async function loadSite(
845-
loadSiteOptions: {options: Partial<PluginOptions>; locale?: string} = {
846+
loadSiteOptions: {
847+
options?: Partial<PluginOptions>;
848+
locale?: string;
849+
translate?: boolean;
850+
} = {
846851
options: {},
847852
},
848853
) {
@@ -851,6 +856,10 @@ describe('versioned site', () => {
851856
siteDir,
852857
locale: loadSiteOptions.locale,
853858
});
859+
860+
// hacky but gets the job done
861+
getLocaleConfig(context.i18n).translate = loadSiteOptions.translate ?? true;
862+
854863
const options = {
855864
id: DEFAULT_PLUGIN_ID,
856865
...DEFAULT_OPTIONS,
@@ -1055,6 +1064,43 @@ describe('versioned site', () => {
10551064
});
10561065
});
10571066

1067+
it('versioned docs - translate: false', async () => {
1068+
const {version100TestUtils} = await loadSite({
1069+
translate: false,
1070+
});
1071+
1072+
// This doc is translated, but we still read the original
1073+
await version100TestUtils.testMeta(path.join('hello.md'), {
1074+
id: 'hello',
1075+
sourceDirName: '.',
1076+
permalink: '/docs/1.0.0/',
1077+
slug: '/',
1078+
title: 'hello',
1079+
description: 'Hello 1.0.0 !',
1080+
frontMatter: {
1081+
slug: '/',
1082+
tags: ['inlineTag-v1.0.0', 'globalTag-v1.0.0'],
1083+
},
1084+
version: '1.0.0',
1085+
source: '@site/versioned_docs/version-1.0.0/hello.md',
1086+
tags: [
1087+
{
1088+
description: undefined,
1089+
inline: true,
1090+
label: 'inlineTag-v1.0.0',
1091+
permalink: '/docs/1.0.0/tags/inline-tag-v-1-0-0',
1092+
},
1093+
{
1094+
description: 'globalTag-v1.0.0 description',
1095+
inline: false,
1096+
label: 'globalTag-v1.0.0 label',
1097+
permalink: '/docs/1.0.0/tags/globalTag-v1.0.0 permalink',
1098+
},
1099+
],
1100+
unlisted: false,
1101+
});
1102+
});
1103+
10581104
it('next doc slugs', async () => {
10591105
const {currentVersionTestUtils} = await loadSite();
10601106

0 commit comments

Comments
 (0)