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

@ -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 {