diff --git a/packages/docusaurus-utils/src/__tests__/i18nUtils.test.ts b/packages/docusaurus-utils/src/__tests__/i18nUtils.test.ts index 55c0b52e7c..3ebeeaa03d 100644 --- a/packages/docusaurus-utils/src/__tests__/i18nUtils.test.ts +++ b/packages/docusaurus-utils/src/__tests__/i18nUtils.test.ts @@ -5,13 +5,15 @@ * LICENSE file in the root directory of this source tree. */ -import path from 'path'; +import * as path from 'path'; import { mergeTranslations, updateTranslationFileMessages, getPluginI18nPath, localizePath, + getCurrentLocaleConfig, } from '../i18nUtils'; +import type {I18n, I18nLocaleConfig} from '@docusaurus/types'; describe('mergeTranslations', () => { it('works', () => { @@ -179,3 +181,65 @@ describe('localizePath', () => { ).toBe('/baseUrl/'); }); }); + +describe('getCurrentLocaleConfig', () => { + const localeConfigEn: I18nLocaleConfig = { + path: 'path', + direction: 'rtl', + htmlLang: 'en', + calendar: 'calendar', + label: 'EN', + translate: true, + }; + const localeConfigFr: I18nLocaleConfig = { + path: 'path', + direction: 'rtl', + htmlLang: 'fr', + calendar: 'calendar', + label: 'FR', + translate: true, + }; + + function i18n(params: Partial): I18n { + return { + defaultLocale: 'en', + localeConfigs: {}, + locales: ['en'], + path: 'path', + currentLocale: 'en', + ...params, + }; + } + + it('returns single locale config', () => { + expect( + getCurrentLocaleConfig( + i18n({currentLocale: 'en', localeConfigs: {en: localeConfigEn}}), + ), + ).toEqual(localeConfigEn); + }); + + it('returns correct locale config among 2', () => { + expect( + getCurrentLocaleConfig( + i18n({ + currentLocale: 'fr', + localeConfigs: {en: localeConfigEn, fr: localeConfigFr}, + }), + ), + ).toEqual(localeConfigFr); + }); + + it('throws for locale config that does not exist', () => { + expect(() => + getCurrentLocaleConfig( + i18n({ + currentLocale: 'fr', + localeConfigs: {en: localeConfigEn}, + }), + ), + ).toThrowErrorMatchingInlineSnapshot( + `"Can't find locale config for locale \`fr\`"`, + ); + }); +}); diff --git a/packages/docusaurus-utils/src/i18nUtils.ts b/packages/docusaurus-utils/src/i18nUtils.ts index 15723648d7..3c5f3d4f79 100644 --- a/packages/docusaurus-utils/src/i18nUtils.ts +++ b/packages/docusaurus-utils/src/i18nUtils.ts @@ -7,12 +7,14 @@ import path from 'path'; import _ from 'lodash'; +import logger from '@docusaurus/logger'; import {DEFAULT_PLUGIN_ID} from './constants'; import {normalizeUrl} from './urlUtils'; import type { TranslationFileContent, TranslationFile, I18n, + I18nLocaleConfig, } from '@docusaurus/types'; /** @@ -112,3 +114,13 @@ export function localizePath({ // Url paths; add a trailing slash so it's a valid base URL return normalizeUrl([originalPath, i18n.currentLocale, '/']); } + +export function getCurrentLocaleConfig(i18n: I18n): I18nLocaleConfig { + const localeConfig = i18n.localeConfigs[i18n.currentLocale]; + if (!localeConfig) { + throw new Error( + `Can't find locale config for locale ${logger.code(i18n.currentLocale)}`, + ); + } + return localeConfig; +} diff --git a/packages/docusaurus-utils/src/index.ts b/packages/docusaurus-utils/src/index.ts index a38a6def09..df4afbed78 100644 --- a/packages/docusaurus-utils/src/index.ts +++ b/packages/docusaurus-utils/src/index.ts @@ -34,6 +34,7 @@ export { updateTranslationFileMessages, getPluginI18nPath, localizePath, + getCurrentLocaleConfig, } from './i18nUtils'; export {mapAsyncSequential, findAsyncSequential} from './jsUtils'; export {