From 64ee0392be93d82cb568fcf8ac539fcdf426f1bc Mon Sep 17 00:00:00 2001 From: slorber Date: Mon, 14 Dec 2020 18:10:08 +0100 Subject: [PATCH] fix type + add localeConfigs test --- .../src/index.d.ts | 1 + .../__snapshots__/config.test.ts.snap | 1 + .../src/server/__tests__/i18n.test.ts | 46 ++++++++++++++++++- packages/docusaurus/src/server/i18n.ts | 13 +++--- 4 files changed, 54 insertions(+), 7 deletions(-) diff --git a/packages/docusaurus-module-type-aliases/src/index.d.ts b/packages/docusaurus-module-type-aliases/src/index.d.ts index aa70ea2cd691..a40b0cdd7f77 100644 --- a/packages/docusaurus-module-type-aliases/src/index.d.ts +++ b/packages/docusaurus-module-type-aliases/src/index.d.ts @@ -52,6 +52,7 @@ declare module '@generated/i18n' { defaultLocale: string; locales: [string, ...string[]]; currentLocale: string; + localeConfigs: Record; }; export default i18n; } diff --git a/packages/docusaurus/src/server/__tests__/__snapshots__/config.test.ts.snap b/packages/docusaurus/src/server/__tests__/__snapshots__/config.test.ts.snap index f68ff8b45978..d0d71639874e 100644 --- a/packages/docusaurus/src/server/__tests__/__snapshots__/config.test.ts.snap +++ b/packages/docusaurus/src/server/__tests__/__snapshots__/config.test.ts.snap @@ -23,6 +23,7 @@ Object { "favicon": "img/docusaurus.ico", "i18n": Object { "defaultLocale": "en", + "localeConfigs": Object {}, "locales": Array [ "en", ], diff --git a/packages/docusaurus/src/server/__tests__/i18n.test.ts b/packages/docusaurus/src/server/__tests__/i18n.test.ts index 39ab34c4b327..7014ac860124 100644 --- a/packages/docusaurus/src/server/__tests__/i18n.test.ts +++ b/packages/docusaurus/src/server/__tests__/i18n.test.ts @@ -5,9 +5,14 @@ * LICENSE file in the root directory of this source tree. */ -import {loadI18n, localizePath} from '../i18n'; +import {loadI18n, localizePath, defaultLocaleConfig} from '../i18n'; import {DEFAULT_I18N_CONFIG} from '../configValidation'; import path from 'path'; +import {chain, identity} from 'lodash'; + +function testLocaleConfigsFor(locales: string[]) { + return chain(locales).keyBy(identity).mapValues(defaultLocaleConfig).value(); +} describe('loadI18n', () => { test('should load I18n for default config', async () => { @@ -22,6 +27,7 @@ describe('loadI18n', () => { defaultLocale: 'en', locales: ['en'], currentLocale: 'en', + localeConfigs: testLocaleConfigsFor(['en']), }); }); @@ -33,6 +39,7 @@ describe('loadI18n', () => { i18n: { defaultLocale: 'fr', locales: ['en', 'fr', 'de'], + localeConfigs: {}, }, }, ), @@ -40,6 +47,7 @@ describe('loadI18n', () => { defaultLocale: 'fr', locales: ['en', 'fr', 'de'], currentLocale: 'fr', + localeConfigs: testLocaleConfigsFor(['en', 'fr', 'de']), }); }); @@ -51,6 +59,31 @@ describe('loadI18n', () => { i18n: { defaultLocale: 'fr', locales: ['en', 'fr', 'de'], + localeConfigs: {}, + }, + }, + {locale: 'de'}, + ), + ).resolves.toEqual({ + defaultLocale: 'fr', + locales: ['en', 'fr', 'de'], + currentLocale: 'de', + localeConfigs: testLocaleConfigsFor(['en', 'fr', 'de']), + }); + }); + + test('should load I18n for multi-locale config with some xcustom locale configs', async () => { + await expect( + loadI18n( + { + i18n: { + defaultLocale: 'fr', + locales: ['en', 'fr', 'de'], + localeConfigs: { + fr: {label: 'Français'}, + // @ts-expect-error: empty on purpose + en: {}, + }, }, }, {locale: 'de'}, @@ -59,6 +92,11 @@ describe('loadI18n', () => { defaultLocale: 'fr', locales: ['en', 'fr', 'de'], currentLocale: 'de', + localeConfigs: { + fr: {label: 'Français'}, + en: defaultLocaleConfig('en'), + de: defaultLocaleConfig('de'), + }, }); }); @@ -70,6 +108,7 @@ describe('loadI18n', () => { i18n: { defaultLocale: 'fr', locales: ['en', 'fr', 'de'], + localeConfigs: {}, }, }, {locale: 'it'}, @@ -88,6 +127,7 @@ describe('localizePath', () => { defaultLocale: 'en', locales: ['en', 'fr'], currentLocale: 'fr', + localeConfigs: {}, }, options: {localizePath: true}, }), @@ -103,6 +143,7 @@ describe('localizePath', () => { defaultLocale: 'en', locales: ['en', 'fr'], currentLocale: 'fr', + localeConfigs: {}, }, options: {localizePath: true}, }), @@ -118,6 +159,7 @@ describe('localizePath', () => { defaultLocale: 'en', locales: ['en', 'fr'], currentLocale: 'en', + localeConfigs: {}, }, options: {localizePath: true}, }), @@ -133,6 +175,7 @@ describe('localizePath', () => { defaultLocale: 'en', locales: ['en', 'fr'], currentLocale: 'en', + localeConfigs: {}, }, // options: {localizePath: true}, }), @@ -148,6 +191,7 @@ describe('localizePath', () => { defaultLocale: 'en', locales: ['en', 'fr'], currentLocale: 'en', + localeConfigs: {}, }, // options: {localizePath: true}, }), diff --git a/packages/docusaurus/src/server/i18n.ts b/packages/docusaurus/src/server/i18n.ts index 64c64f9ada0b..52a33e5271d9 100644 --- a/packages/docusaurus/src/server/i18n.ts +++ b/packages/docusaurus/src/server/i18n.ts @@ -8,6 +8,12 @@ import {I18n, DocusaurusConfig, I18nLocaleConfig} from '@docusaurus/types'; import path from 'path'; import {normalizeUrl} from '@docusaurus/utils'; +export function defaultLocaleConfig(locale: string): I18nLocaleConfig { + return { + label: locale, + }; +} + export async function loadI18n( config: DocusaurusConfig, options: {locale?: string} = {}, @@ -30,12 +36,7 @@ Note: Docusaurus only support running one local at a time.`, const localeConfigOptions: Partial = i18nConfig.localeConfigs[locale]; - // Default values - const localeConfigDefaults: I18nLocaleConfig = { - label: locale, - }; - - return {...localeConfigDefaults, ...localeConfigOptions}; + return {...defaultLocaleConfig(locale), ...localeConfigOptions}; } const localeConfigs = i18nConfig.locales.reduce((acc, locale) => {