mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-06 10:20:09 +02:00
fix: fix write-translations warning for theme-common translations (#5398)
This commit is contained in:
parent
631c4685fb
commit
0480afc75f
2 changed files with 28 additions and 2 deletions
|
@ -5,6 +5,7 @@
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
import {ConfigOptions, InitializedPlugin} from '@docusaurus/types';
|
import {ConfigOptions, InitializedPlugin} from '@docusaurus/types';
|
||||||
|
import path from 'path';
|
||||||
import {loadContext, loadPluginConfigs} from '../server';
|
import {loadContext, loadPluginConfigs} from '../server';
|
||||||
import initPlugins from '../server/plugins/init';
|
import initPlugins from '../server/plugins/init';
|
||||||
|
|
||||||
|
@ -15,9 +16,27 @@ import {
|
||||||
getPluginsDefaultCodeTranslationMessages,
|
getPluginsDefaultCodeTranslationMessages,
|
||||||
applyDefaultCodeTranslations,
|
applyDefaultCodeTranslations,
|
||||||
} from '../server/translations/translations';
|
} from '../server/translations/translations';
|
||||||
import {extractSiteSourceCodeTranslations} from '../server/translations/translationsExtractor';
|
import {
|
||||||
|
extractSiteSourceCodeTranslations,
|
||||||
|
globSourceCodeFilePaths,
|
||||||
|
} from '../server/translations/translationsExtractor';
|
||||||
import {getCustomBabelConfigFilePath, getBabelOptions} from '../webpack/utils';
|
import {getCustomBabelConfigFilePath, getBabelOptions} from '../webpack/utils';
|
||||||
|
|
||||||
|
// 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 plugin.getThemeTranslationPaths() ?)
|
||||||
|
// We just make an exception and assume that Docusaurus user is using an official theme
|
||||||
|
async function getExtraSourceCodeFilePaths(): Promise<string[]> {
|
||||||
|
try {
|
||||||
|
const themeCommonSourceDir = path.dirname(
|
||||||
|
require.resolve('@docusaurus/theme-common/lib'),
|
||||||
|
);
|
||||||
|
return globSourceCodeFilePaths([themeCommonSourceDir]);
|
||||||
|
} catch (e) {
|
||||||
|
return []; // User may not use a Docusaurus official theme? Quite unlikely...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function writePluginTranslationFiles({
|
async function writePluginTranslationFiles({
|
||||||
siteDir,
|
siteDir,
|
||||||
plugin,
|
plugin,
|
||||||
|
@ -80,6 +99,7 @@ Available locales are: ${context.i18n.locales.join(',')}.`,
|
||||||
siteDir,
|
siteDir,
|
||||||
plugins,
|
plugins,
|
||||||
babelOptions,
|
babelOptions,
|
||||||
|
await getExtraSourceCodeFilePaths(),
|
||||||
);
|
);
|
||||||
const defaultCodeMessages = await getPluginsDefaultCodeTranslationMessages(
|
const defaultCodeMessages = await getPluginsDefaultCodeTranslationMessages(
|
||||||
plugins,
|
plugins,
|
||||||
|
|
|
@ -80,6 +80,7 @@ export async function extractSiteSourceCodeTranslations(
|
||||||
siteDir: string,
|
siteDir: string,
|
||||||
plugins: InitializedPlugin[],
|
plugins: InitializedPlugin[],
|
||||||
babelOptions: TransformOptions,
|
babelOptions: TransformOptions,
|
||||||
|
extraSourceCodeFilePaths: string[] = [],
|
||||||
): Promise<TranslationFileContent> {
|
): Promise<TranslationFileContent> {
|
||||||
// Should we warn here if the same translation "key" is found in multiple source code files?
|
// Should we warn here if the same translation "key" is found in multiple source code files?
|
||||||
function toTranslationFileContent(
|
function toTranslationFileContent(
|
||||||
|
@ -92,8 +93,13 @@ export async function extractSiteSourceCodeTranslations(
|
||||||
|
|
||||||
const sourceCodeFilePaths = await getSourceCodeFilePaths(siteDir, plugins);
|
const sourceCodeFilePaths = await getSourceCodeFilePaths(siteDir, plugins);
|
||||||
|
|
||||||
|
const allSourceCodeFilePaths = [
|
||||||
|
...sourceCodeFilePaths,
|
||||||
|
...extraSourceCodeFilePaths,
|
||||||
|
];
|
||||||
|
|
||||||
const sourceCodeFilesTranslations = await extractAllSourceCodeFileTranslations(
|
const sourceCodeFilesTranslations = await extractAllSourceCodeFileTranslations(
|
||||||
sourceCodeFilePaths,
|
allSourceCodeFilePaths,
|
||||||
babelOptions,
|
babelOptions,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue