-
-
Notifications
You must be signed in to change notification settings - Fork 8k
/
writeTranslations.ts
133 lines (118 loc) · 3.78 KB
/
writeTranslations.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* 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.
*/
import fs from 'fs-extra';
import path from 'path';
import {loadContext, type LoadContextOptions} from '../server';
import {initPlugins} from '../server/plugins/init';
import {
writePluginTranslations,
writeCodeTranslations,
type WriteTranslationsOptions,
getPluginsDefaultCodeTranslationMessages,
applyDefaultCodeTranslations,
} from '../server/translations/translations';
import {
extractSiteSourceCodeTranslations,
globSourceCodeFilePaths,
} from '../server/translations/translationsExtractor';
import {getCustomBabelConfigFilePath, getBabelOptions} from '../webpack/utils';
import type {InitializedPlugin} from '@docusaurus/types';
export type WriteTranslationsCLIOptions = Pick<
LoadContextOptions,
'config' | 'locale'
> &
WriteTranslationsOptions;
function resolveThemeCommonLibDir(): string | undefined {
try {
return path.dirname(require.resolve('@docusaurus/theme-common'));
} catch {
return undefined;
}
}
/**
* This is a hack, so that @docusaurus/theme-common translations are extracted!
* A theme doesn't have a way to express that one of its dependency (like
* @docusaurus/theme-common) also has translations to extract.
* Instead of introducing a new lifecycle (like `getThemeTranslationPaths()`?)
* We just make an exception and assume that user is using an official theme
*/
async function getExtraSourceCodeFilePaths(): Promise<string[]> {
const themeCommonLibDir = resolveThemeCommonLibDir();
if (!themeCommonLibDir) {
return []; // User may not use a Docusaurus official theme? Quite unlikely...
}
return globSourceCodeFilePaths([themeCommonLibDir]);
}
async function writePluginTranslationFiles({
localizationDir,
plugin,
options,
}: {
localizationDir: string;
plugin: InitializedPlugin;
options: WriteTranslationsOptions;
}) {
if (plugin.getTranslationFiles) {
const content = await plugin.loadContent?.();
const translationFiles = await plugin.getTranslationFiles({
content,
});
await Promise.all(
translationFiles.map(async (translationFile) => {
await writePluginTranslations({
localizationDir,
plugin,
translationFile,
options,
});
}),
);
}
}
export async function writeTranslations(
siteDirParam: string = '.',
options: Partial<WriteTranslationsCLIOptions> = {},
): Promise<void> {
const siteDir = await fs.realpath(siteDirParam);
const context = await loadContext({
siteDir,
config: options.config,
locale: options.locale,
});
const {localizationDir} = context;
const plugins = await initPlugins(context);
const locale = options.locale ?? context.i18n.defaultLocale;
if (!context.i18n.locales.includes(locale)) {
throw new Error(
`Can't write-translation for locale "${locale}" that is not in the locale configuration file.
Available locales are: ${context.i18n.locales.join(',')}.`,
);
}
const babelOptions = getBabelOptions({
isServer: true,
babelOptions: await getCustomBabelConfigFilePath(siteDir),
});
const extractedCodeTranslations = await extractSiteSourceCodeTranslations(
siteDir,
plugins,
babelOptions,
await getExtraSourceCodeFilePaths(),
);
const defaultCodeMessages = await getPluginsDefaultCodeTranslationMessages(
plugins,
);
const codeTranslations = applyDefaultCodeTranslations({
extractedCodeTranslations,
defaultCodeMessages,
});
await writeCodeTranslations({localizationDir}, codeTranslations, options);
await Promise.all(
plugins.map(async (plugin) => {
await writePluginTranslationFiles({localizationDir, plugin, options});
}),
);
}