fix: fix write-translations warning for theme-common translations (#5398)

This commit is contained in:
Sébastien Lorber 2021-08-20 18:47:43 +02:00 committed by GitHub
parent 631c4685fb
commit 0480afc75f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 2 deletions

View file

@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
import {ConfigOptions, InitializedPlugin} from '@docusaurus/types';
import path from 'path';
import {loadContext, loadPluginConfigs} from '../server';
import initPlugins from '../server/plugins/init';
@ -15,9 +16,27 @@ import {
getPluginsDefaultCodeTranslationMessages,
applyDefaultCodeTranslations,
} from '../server/translations/translations';
import {extractSiteSourceCodeTranslations} from '../server/translations/translationsExtractor';
import {
extractSiteSourceCodeTranslations,
globSourceCodeFilePaths,
} from '../server/translations/translationsExtractor';
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({
siteDir,
plugin,
@ -80,6 +99,7 @@ Available locales are: ${context.i18n.locales.join(',')}.`,
siteDir,
plugins,
babelOptions,
await getExtraSourceCodeFilePaths(),
);
const defaultCodeMessages = await getPluginsDefaultCodeTranslationMessages(
plugins,

View file

@ -80,6 +80,7 @@ export async function extractSiteSourceCodeTranslations(
siteDir: string,
plugins: InitializedPlugin[],
babelOptions: TransformOptions,
extraSourceCodeFilePaths: string[] = [],
): Promise<TranslationFileContent> {
// Should we warn here if the same translation "key" is found in multiple source code files?
function toTranslationFileContent(
@ -92,8 +93,13 @@ export async function extractSiteSourceCodeTranslations(
const sourceCodeFilePaths = await getSourceCodeFilePaths(siteDir, plugins);
const allSourceCodeFilePaths = [
...sourceCodeFilePaths,
...extraSourceCodeFilePaths,
];
const sourceCodeFilesTranslations = await extractAllSourceCodeFileTranslations(
sourceCodeFilePaths,
allSourceCodeFilePaths,
babelOptions,
);