Skip to content

Commit abe5450

Browse files
Josh-Cenaslorber
andauthored
feat(core): allow customizing the i18n directory path (#7386)
Co-authored-by: sebastienlorber <[email protected]>
1 parent c07a514 commit abe5450

File tree

26 files changed

+147
-166
lines changed

26 files changed

+147
-166
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const DefaultI18N: I18n = {
1919
currentLocale: 'en',
2020
locales: ['en'],
2121
defaultLocale: 'en',
22+
path: '1i8n',
2223
localeConfigs: {
2324
en: {
2425
label: 'English',

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ function getI18n(locale: string): I18n {
4747
currentLocale: locale,
4848
locales: [locale],
4949
defaultLocale: locale,
50+
path: 'i18n',
5051
localeConfigs: {
5152
[locale]: {
5253
calendar: 'gregory',
@@ -70,6 +71,7 @@ const getPlugin = async (
7071
i18n: I18n = DefaultI18N,
7172
) => {
7273
const generatedFilesDir: string = path.resolve(siteDir, '.docusaurus');
74+
const localizationDir = path.join(siteDir, i18n.path, i18n.currentLocale);
7375
const siteConfig = {
7476
title: 'Hello',
7577
baseUrl: '/',
@@ -81,6 +83,7 @@ const getPlugin = async (
8183
siteConfig,
8284
generatedFilesDir,
8385
i18n,
86+
localizationDir,
8487
} as LoadContext,
8588
validateOptions({
8689
validate: normalizePluginOptions as Validate<

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ export default async function pluginContentBlog(
5959
siteDir,
6060
siteConfig,
6161
generatedFilesDir,
62+
localizationDir,
6263
i18n: {currentLocale},
6364
} = context;
6465
const {onBrokenMarkdownLinks, baseUrl} = siteConfig;
6566

6667
const contentPaths: BlogContentPaths = {
6768
contentPath: path.resolve(siteDir, options.path),
6869
contentPathLocalized: getPluginI18nPath({
69-
siteDir,
70-
locale: currentLocale,
70+
localizationDir,
7171
pluginName: 'docusaurus-plugin-content-blog',
7272
pluginId: options.id,
7373
}),
Binary file not shown.

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,15 @@ export async function cliDocsVersionCommand(
8585

8686
await Promise.all(
8787
i18n.locales.map(async (locale) => {
88+
// TODO duplicated logic from core, so duplicate comment as well: we need
89+
// to support customization per-locale in the future
90+
const localizationDir = path.resolve(siteDir, i18n.path, locale);
8891
// Copy docs files.
8992
const docsDir =
9093
locale === i18n.defaultLocale
9194
? path.resolve(siteDir, docsPath)
9295
: getDocsDirPathLocalized({
93-
siteDir,
94-
locale,
96+
localizationDir,
9597
pluginId,
9698
versionName: CURRENT_VERSION_NAME,
9799
});
@@ -114,8 +116,7 @@ export async function cliDocsVersionCommand(
114116
locale === i18n.defaultLocale
115117
? getVersionDocsDirPath(siteDir, pluginId, version)
116118
: getDocsDirPathLocalized({
117-
siteDir,
118-
locale,
119+
localizationDir,
119120
pluginId,
120121
versionName: version,
121122
});

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
} from '@docusaurus/plugin-content-docs';
1818

1919
const DefaultI18N: I18n = {
20+
path: 'i18n',
2021
currentLocale: 'en',
2122
locales: ['en'],
2223
defaultLocale: 'en',
@@ -37,6 +38,7 @@ describe('readVersionsMetadata', () => {
3738
siteDir: simpleSiteDir,
3839
baseUrl: '/',
3940
i18n: DefaultI18N,
41+
localizationDir: path.join(simpleSiteDir, 'i18n/en'),
4042
} as LoadContext;
4143

4244
const vCurrent: VersionMetadata = {
@@ -198,6 +200,7 @@ describe('readVersionsMetadata', () => {
198200
siteDir: versionedSiteDir,
199201
baseUrl: '/',
200202
i18n: DefaultI18N,
203+
localizationDir: path.join(versionedSiteDir, 'i18n/en'),
201204
} as LoadContext;
202205

203206
const vCurrent: VersionMetadata = {
@@ -636,6 +639,7 @@ describe('readVersionsMetadata', () => {
636639
siteDir: versionedSiteDir,
637640
baseUrl: '/',
638641
i18n: DefaultI18N,
642+
localizationDir: path.join(versionedSiteDir, 'i18n/en'),
639643
} as LoadContext;
640644

641645
const vCurrent: VersionMetadata = {

packages/docusaurus-plugin-content-docs/src/versions/files.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,16 @@ export function getVersionSidebarsPath(
5555
}
5656

5757
export function getDocsDirPathLocalized({
58-
siteDir,
59-
locale,
58+
localizationDir,
6059
pluginId,
6160
versionName,
6261
}: {
63-
siteDir: string;
64-
locale: string;
62+
localizationDir: string;
6563
pluginId: string;
6664
versionName: string;
6765
}): string {
6866
return getPluginI18nPath({
69-
siteDir,
70-
locale,
67+
localizationDir,
7168
pluginName: 'docusaurus-plugin-content-docs',
7269
pluginId,
7370
subPaths: [
@@ -175,8 +172,7 @@ export async function getVersionMetadataPaths({
175172
> {
176173
const isCurrent = versionName === CURRENT_VERSION_NAME;
177174
const contentPathLocalized = getDocsDirPathLocalized({
178-
siteDir: context.siteDir,
179-
locale: context.i18n.currentLocale,
175+
localizationDir: context.localizationDir,
180176
pluginId: options.id,
181177
versionName,
182178
});

packages/docusaurus-plugin-content-docs/src/versions/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ function getVersionEditUrls({
4949
const editDirPath = options.editCurrentVersion ? options.path : contentPath;
5050
const editDirPathLocalized = options.editCurrentVersion
5151
? getDocsDirPathLocalized({
52-
siteDir: context.siteDir,
53-
locale: context.i18n.currentLocale,
52+
localizationDir: context.localizationDir,
5453
versionName: CURRENT_VERSION_NAME,
5554
pluginId: options.id,
5655
})

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,19 @@ exports[`docusaurus-plugin-content-pages loads simple pages 1`] = `
5555
exports[`docusaurus-plugin-content-pages loads simple pages with french translations 1`] = `
5656
[
5757
{
58-
"permalink": "/",
58+
"permalink": "/fr/",
5959
"source": "@site/src/pages/index.js",
6060
"type": "jsx",
6161
},
6262
{
63-
"permalink": "/typescript",
63+
"permalink": "/fr/typescript",
6464
"source": "@site/src/pages/typescript.tsx",
6565
"type": "jsx",
6666
},
6767
{
6868
"description": "Markdown index page",
6969
"frontMatter": {},
70-
"permalink": "/hello/",
70+
"permalink": "/fr/hello/",
7171
"source": "@site/src/pages/hello/index.md",
7272
"title": "Index",
7373
"type": "mdx",
@@ -78,26 +78,26 @@ exports[`docusaurus-plugin-content-pages loads simple pages with french translat
7878
"description": "my mdx page",
7979
"title": "mdx page",
8080
},
81-
"permalink": "/hello/mdxPage",
81+
"permalink": "/fr/hello/mdxPage",
8282
"source": "@site/src/pages/hello/mdxPage.mdx",
8383
"title": "mdx page",
8484
"type": "mdx",
8585
},
8686
{
87-
"permalink": "/hello/translatedJs",
87+
"permalink": "/fr/hello/translatedJs",
8888
"source": "@site/i18n/fr/docusaurus-plugin-content-pages/hello/translatedJs.js",
8989
"type": "jsx",
9090
},
9191
{
9292
"description": "translated markdown page (fr)",
9393
"frontMatter": {},
94-
"permalink": "/hello/translatedMd",
94+
"permalink": "/fr/hello/translatedMd",
9595
"source": "@site/i18n/fr/docusaurus-plugin-content-pages/hello/translatedMd.md",
9696
"title": undefined,
9797
"type": "mdx",
9898
},
9999
{
100-
"permalink": "/hello/world",
100+
"permalink": "/fr/hello/world",
101101
"source": "@site/src/pages/hello/world.js",
102102
"type": "jsx",
103103
},

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,9 @@ describe('docusaurus-plugin-content-pages', () => {
3232

3333
it('loads simple pages with french translations', async () => {
3434
const siteDir = path.join(__dirname, '__fixtures__', 'website');
35-
const context = await loadContext({siteDir});
35+
const context = await loadContext({siteDir, locale: 'fr'});
3636
const plugin = pluginContentPages(
37-
{
38-
...context,
39-
i18n: {
40-
...context.i18n,
41-
currentLocale: 'fr',
42-
},
43-
},
37+
context,
4438
validateOptions({
4539
validate: normalizePluginOptions,
4640
options: {

0 commit comments

Comments
 (0)