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

View file

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

View file

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

View file

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

View file

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

View file

@ -11,7 +11,7 @@ import {
updateTranslationFileMessages,
getPluginI18nPath,
localizePath,
getCurrentLocaleConfig,
getLocaleConfig,
} from '../i18nUtils';
import type {I18n, I18nLocaleConfig} from '@docusaurus/types';
@ -182,7 +182,7 @@ describe('localizePath', () => {
});
});
describe('getCurrentLocaleConfig', () => {
describe('getLocaleConfig', () => {
const localeConfigEn: I18nLocaleConfig = {
path: 'path',
direction: 'rtl',
@ -213,7 +213,7 @@ describe('getCurrentLocaleConfig', () => {
it('returns single locale config', () => {
expect(
getCurrentLocaleConfig(
getLocaleConfig(
i18n({currentLocale: 'en', localeConfigs: {en: localeConfigEn}}),
),
).toEqual(localeConfigEn);
@ -221,7 +221,7 @@ describe('getCurrentLocaleConfig', () => {
it('returns correct locale config among 2', () => {
expect(
getCurrentLocaleConfig(
getLocaleConfig(
i18n({
currentLocale: 'fr',
localeConfigs: {en: localeConfigEn, fr: localeConfigFr},
@ -230,9 +230,21 @@ describe('getCurrentLocaleConfig', () => {
).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', () => {
expect(() =>
getCurrentLocaleConfig(
getLocaleConfig(
i18n({
currentLocale: 'fr',
localeConfigs: {en: localeConfigEn},

View file

@ -118,11 +118,12 @@ export function localizePath({
// TODO we may extract this to a separate package
// we want to use it on the frontend too
// but "docusaurus-utils-common" (agnostic utils) is not an ideal place since
export function getCurrentLocaleConfig(i18n: I18n): I18nLocaleConfig {
const localeConfig = i18n.localeConfigs[i18n.currentLocale];
export function getLocaleConfig(i18n: I18n, locale?: string): I18nLocaleConfig {
const localeToLookFor = locale ?? i18n.currentLocale;
const localeConfig = i18n.localeConfigs[localeToLookFor];
if (!localeConfig) {
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;

View file

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

View file

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

View file

@ -10,7 +10,7 @@ import {
localizePath,
DEFAULT_BUILD_DIR_NAME,
GENERATED_FILES_DIR_NAME,
getCurrentLocaleConfig,
getLocaleConfig,
} from '@docusaurus/utils';
import {PerfLogger} from '@docusaurus/logger';
import combinePromises from 'combine-promises';
@ -114,7 +114,7 @@ export async function loadContext(
const localizationDir = path.resolve(
siteDir,
i18n.path,
getCurrentLocaleConfig(i18n).path,
getLocaleConfig(i18n).path,
);
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 HtmlWebpackPlugin from 'html-webpack-plugin';
import {getProgressBarPlugin} from '@docusaurus/bundler';
import {getCurrentLocaleConfig} from '@docusaurus/utils';
import {getLocaleConfig} from '@docusaurus/utils';
import {createBaseConfig} from './base';
import ChunkAssetPlugin from './plugins/ChunkAssetPlugin';
import ForceTerminatePlugin from './plugins/ForceTerminatePlugin';
@ -118,7 +118,7 @@ export async function createStartClientConfig({
headTags,
preBodyTags,
postBodyTags,
lang: getCurrentLocaleConfig(props.i18n).htmlLang,
lang: getLocaleConfig(props.i18n).htmlLang,
}),
],
},