Fix CLI unit tests + refactor getLocaleConfig method

This commit is contained in:
sebastien 2025-07-04 16:08:01 +02:00
parent b640334248
commit f6f3e6cdb4
12 changed files with 38 additions and 25 deletions

View file

@ -12,7 +12,7 @@ import {
posixPath, posixPath,
getFileCommitDate, getFileCommitDate,
LAST_UPDATE_FALLBACK, LAST_UPDATE_FALLBACK,
getCurrentLocaleConfig, getLocaleConfig,
} from '@docusaurus/utils'; } from '@docusaurus/utils';
import {DEFAULT_FUTURE_CONFIG} from '@docusaurus/core/src/server/configValidation'; import {DEFAULT_FUTURE_CONFIG} from '@docusaurus/core/src/server/configValidation';
import pluginContentBlog from '../index'; import pluginContentBlog from '../index';
@ -108,7 +108,7 @@ const getPlugin = async (
const localizationDir = path.join( const localizationDir = path.join(
siteDir, siteDir,
i18n.path, i18n.path,
getCurrentLocaleConfig(i18n).path, getLocaleConfig(i18n).path,
); );
const siteConfig = { const siteConfig = {
title: 'Hello', title: 'Hello',

View file

@ -19,7 +19,7 @@ import {
getDataFilePath, getDataFilePath,
DEFAULT_PLUGIN_ID, DEFAULT_PLUGIN_ID,
resolveMarkdownLinkPathname, resolveMarkdownLinkPathname,
getCurrentLocaleConfig, getLocaleConfig,
} from '@docusaurus/utils'; } from '@docusaurus/utils';
import {getTagsFilePathsToWatch} from '@docusaurus/utils-validation'; import {getTagsFilePathsToWatch} from '@docusaurus/utils-validation';
import {createMDXLoaderItem} from '@docusaurus/mdx-loader'; import {createMDXLoaderItem} from '@docusaurus/mdx-loader';
@ -74,7 +74,7 @@ export default async function pluginContentBlog(
const {baseUrl} = siteConfig; const {baseUrl} = siteConfig;
const shouldTranslate = getCurrentLocaleConfig(context.i18n).translate; const shouldTranslate = getLocaleConfig(context.i18n).translate;
const contentPaths: BlogContentPaths = { const contentPaths: BlogContentPaths = {
contentPath: path.resolve(siteDir, options.path), contentPath: path.resolve(siteDir, options.path),
contentPathLocalized: shouldTranslate contentPathLocalized: shouldTranslate

View file

@ -8,7 +8,7 @@
import fs from 'fs-extra'; import fs from 'fs-extra';
import path from 'path'; import path from 'path';
import logger from '@docusaurus/logger'; import logger from '@docusaurus/logger';
import {DEFAULT_PLUGIN_ID, getCurrentLocaleConfig} from '@docusaurus/utils'; import {DEFAULT_PLUGIN_ID, getLocaleConfig} from '@docusaurus/utils';
import { import {
getVersionsFilePath, getVersionsFilePath,
getVersionDocsDirPath, getVersionDocsDirPath,
@ -89,7 +89,7 @@ async function cliDocsVersionCommand(
const localizationDir = path.resolve( const localizationDir = path.resolve(
siteDir, siteDir,
i18n.path, i18n.path,
getCurrentLocaleConfig(i18n).path, getLocaleConfig(i18n, locale).path,
); );
// Copy docs files. // Copy docs files.
const docsDir = const docsDir =

View file

@ -9,8 +9,8 @@ import path from 'path';
import fs from 'fs-extra'; import fs from 'fs-extra';
import { import {
getPluginI18nPath, getPluginI18nPath,
getLocaleConfig,
DEFAULT_PLUGIN_ID, DEFAULT_PLUGIN_ID,
getCurrentLocaleConfig,
} from '@docusaurus/utils'; } from '@docusaurus/utils';
import { import {
VERSIONS_JSON_FILE, VERSIONS_JSON_FILE,
@ -191,7 +191,7 @@ export async function getVersionMetadataPaths({
> { > {
const isCurrent = versionName === CURRENT_VERSION_NAME; const isCurrent = versionName === CURRENT_VERSION_NAME;
const shouldTranslate = getCurrentLocaleConfig(context.i18n).translate; const shouldTranslate = getLocaleConfig(context.i18n).translate;
const contentPathLocalized = shouldTranslate const contentPathLocalized = shouldTranslate
? getDocsDirPathLocalized({ ? getDocsDirPathLocalized({
localizationDir: context.localizationDir, localizationDir: context.localizationDir,

View file

@ -22,8 +22,8 @@ import {
posixPath, posixPath,
getPluginI18nPath, getPluginI18nPath,
getContentPathList, getContentPathList,
getLocaleConfig,
type ContentPaths, type ContentPaths,
getCurrentLocaleConfig,
} from '@docusaurus/utils'; } from '@docusaurus/utils';
import {validatePageFrontMatter} from './frontMatter'; import {validatePageFrontMatter} from './frontMatter';
import type {LoadContext} from '@docusaurus/types'; import type {LoadContext} from '@docusaurus/types';
@ -42,7 +42,7 @@ export function createPagesContentPaths({
}): ContentPaths { }): ContentPaths {
const {siteDir, localizationDir} = context; const {siteDir, localizationDir} = context;
const shouldTranslate = getCurrentLocaleConfig(context.i18n).translate; const shouldTranslate = getLocaleConfig(context.i18n).translate;
return { return {
contentPath: path.resolve(siteDir, options.path), contentPath: path.resolve(siteDir, options.path),
contentPathLocalized: shouldTranslate contentPathLocalized: shouldTranslate

View file

@ -11,7 +11,7 @@ import {
updateTranslationFileMessages, updateTranslationFileMessages,
getPluginI18nPath, getPluginI18nPath,
localizePath, localizePath,
getCurrentLocaleConfig, getLocaleConfig,
} from '../i18nUtils'; } from '../i18nUtils';
import type {I18n, I18nLocaleConfig} from '@docusaurus/types'; import type {I18n, I18nLocaleConfig} from '@docusaurus/types';
@ -182,7 +182,7 @@ describe('localizePath', () => {
}); });
}); });
describe('getCurrentLocaleConfig', () => { describe('getLocaleConfig', () => {
const localeConfigEn: I18nLocaleConfig = { const localeConfigEn: I18nLocaleConfig = {
path: 'path', path: 'path',
direction: 'rtl', direction: 'rtl',
@ -213,7 +213,7 @@ describe('getCurrentLocaleConfig', () => {
it('returns single locale config', () => { it('returns single locale config', () => {
expect( expect(
getCurrentLocaleConfig( getLocaleConfig(
i18n({currentLocale: 'en', localeConfigs: {en: localeConfigEn}}), i18n({currentLocale: 'en', localeConfigs: {en: localeConfigEn}}),
), ),
).toEqual(localeConfigEn); ).toEqual(localeConfigEn);
@ -221,7 +221,7 @@ describe('getCurrentLocaleConfig', () => {
it('returns correct locale config among 2', () => { it('returns correct locale config among 2', () => {
expect( expect(
getCurrentLocaleConfig( getLocaleConfig(
i18n({ i18n({
currentLocale: 'fr', currentLocale: 'fr',
localeConfigs: {en: localeConfigEn, fr: localeConfigFr}, localeConfigs: {en: localeConfigEn, fr: localeConfigFr},
@ -230,9 +230,21 @@ describe('getCurrentLocaleConfig', () => {
).toEqual(localeConfigFr); ).toEqual(localeConfigFr);
}); });
it('accepts locale to look for as param', () => {
expect(
getLocaleConfig(
i18n({
currentLocale: 'fr',
localeConfigs: {en: localeConfigEn, fr: localeConfigFr},
}),
'en',
),
).toEqual(localeConfigEn);
});
it('throws for locale config that does not exist', () => { it('throws for locale config that does not exist', () => {
expect(() => expect(() =>
getCurrentLocaleConfig( getLocaleConfig(
i18n({ i18n({
currentLocale: 'fr', currentLocale: 'fr',
localeConfigs: {en: localeConfigEn}, localeConfigs: {en: localeConfigEn},

View file

@ -118,11 +118,12 @@ export function localizePath({
// TODO we may extract this to a separate package // TODO we may extract this to a separate package
// we want to use it on the frontend too // we want to use it on the frontend too
// but "docusaurus-utils-common" (agnostic utils) is not an ideal place since // but "docusaurus-utils-common" (agnostic utils) is not an ideal place since
export function getCurrentLocaleConfig(i18n: I18n): I18nLocaleConfig { export function getLocaleConfig(i18n: I18n, locale?: string): I18nLocaleConfig {
const localeConfig = i18n.localeConfigs[i18n.currentLocale]; const localeToLookFor = locale ?? i18n.currentLocale;
const localeConfig = i18n.localeConfigs[localeToLookFor];
if (!localeConfig) { if (!localeConfig) {
throw new Error( throw new Error(
`Can't find locale config for locale ${logger.code(i18n.currentLocale)}`, `Can't find locale config for locale ${logger.code(localeToLookFor)}`,
); );
} }
return localeConfig; return localeConfig;

View file

@ -34,7 +34,7 @@ export {
updateTranslationFileMessages, updateTranslationFileMessages,
getPluginI18nPath, getPluginI18nPath,
localizePath, localizePath,
getCurrentLocaleConfig, getLocaleConfig,
} from './i18nUtils'; } from './i18nUtils';
export {mapAsyncSequential, findAsyncSequential} from './jsUtils'; export {mapAsyncSequential, findAsyncSequential} from './jsUtils';
export { export {

View file

@ -6,7 +6,7 @@
*/ */
import {PerfLogger} from '@docusaurus/logger'; import {PerfLogger} from '@docusaurus/logger';
import {getCurrentLocaleConfig} from '@docusaurus/utils'; import {getLocaleConfig} from '@docusaurus/utils';
import {initPlugins} from './init'; import {initPlugins} from './init';
import {createBootstrapPlugin, createMDXFallbackPlugin} from './synthetic'; import {createBootstrapPlugin, createMDXFallbackPlugin} from './synthetic';
import {localizePluginTranslationFile} from '../translations/translations'; import {localizePluginTranslationFile} from '../translations/translations';
@ -82,7 +82,7 @@ async function executePluginContentLoading({
plugin.loadContent?.(), plugin.loadContent?.(),
); );
const shouldTranslate = getCurrentLocaleConfig(context.i18n).translate; const shouldTranslate = getLocaleConfig(context.i18n).translate;
if (shouldTranslate) { if (shouldTranslate) {
content = await PerfLogger.async('translatePluginContent()', () => content = await PerfLogger.async('translatePluginContent()', () =>

View file

@ -10,7 +10,7 @@ import {
localizePath, localizePath,
DEFAULT_BUILD_DIR_NAME, DEFAULT_BUILD_DIR_NAME,
GENERATED_FILES_DIR_NAME, GENERATED_FILES_DIR_NAME,
getCurrentLocaleConfig, getLocaleConfig,
} from '@docusaurus/utils'; } from '@docusaurus/utils';
import {PerfLogger} from '@docusaurus/logger'; import {PerfLogger} from '@docusaurus/logger';
import combinePromises from 'combine-promises'; import combinePromises from 'combine-promises';
@ -114,7 +114,7 @@ export async function loadContext(
const localizationDir = path.resolve( const localizationDir = path.resolve(
siteDir, siteDir,
i18n.path, i18n.path,
getCurrentLocaleConfig(i18n).path, getLocaleConfig(i18n).path,
); );
const siteConfig: DocusaurusConfig = {...initialSiteConfig, baseUrl}; const siteConfig: DocusaurusConfig = {...initialSiteConfig, baseUrl};

View file

@ -11,7 +11,7 @@ import {BundleAnalyzerPlugin} from 'webpack-bundle-analyzer';
import ReactLoadableSSRAddon from 'react-loadable-ssr-addon-v5-slorber'; import ReactLoadableSSRAddon from 'react-loadable-ssr-addon-v5-slorber';
import HtmlWebpackPlugin from 'html-webpack-plugin'; import HtmlWebpackPlugin from 'html-webpack-plugin';
import {getProgressBarPlugin} from '@docusaurus/bundler'; import {getProgressBarPlugin} from '@docusaurus/bundler';
import {getCurrentLocaleConfig} from '@docusaurus/utils'; import {getLocaleConfig} from '@docusaurus/utils';
import {createBaseConfig} from './base'; import {createBaseConfig} from './base';
import ChunkAssetPlugin from './plugins/ChunkAssetPlugin'; import ChunkAssetPlugin from './plugins/ChunkAssetPlugin';
import ForceTerminatePlugin from './plugins/ForceTerminatePlugin'; import ForceTerminatePlugin from './plugins/ForceTerminatePlugin';
@ -118,7 +118,7 @@ export async function createStartClientConfig({
headTags, headTags,
preBodyTags, preBodyTags,
postBodyTags, postBodyTags,
lang: getCurrentLocaleConfig(props.i18n).htmlLang, lang: getLocaleConfig(props.i18n).htmlLang,
}), }),
], ],
}, },