Skip to content

Commit

Permalink
fix type + add localeConfigs test
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Dec 14, 2020
1 parent f92930c commit 64ee039
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/docusaurus-module-type-aliases/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ declare module '@generated/i18n' {
defaultLocale: string;
locales: [string, ...string[]];
currentLocale: string;
localeConfigs: Record<string, {label: string}>;
};
export default i18n;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Object {
"favicon": "img/docusaurus.ico",
"i18n": Object {
"defaultLocale": "en",
"localeConfigs": Object {},
"locales": Array [
"en",
],
Expand Down
46 changes: 45 additions & 1 deletion packages/docusaurus/src/server/__tests__/i18n.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -22,6 +27,7 @@ describe('loadI18n', () => {
defaultLocale: 'en',
locales: ['en'],
currentLocale: 'en',
localeConfigs: testLocaleConfigsFor(['en']),
});
});

Expand All @@ -33,13 +39,15 @@ describe('loadI18n', () => {
i18n: {
defaultLocale: 'fr',
locales: ['en', 'fr', 'de'],
localeConfigs: {},
},
},
),
).resolves.toEqual({
defaultLocale: 'fr',
locales: ['en', 'fr', 'de'],
currentLocale: 'fr',
localeConfigs: testLocaleConfigsFor(['en', 'fr', 'de']),
});
});

Expand All @@ -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'},
Expand All @@ -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'),
},
});
});

Expand All @@ -70,6 +108,7 @@ describe('loadI18n', () => {
i18n: {
defaultLocale: 'fr',
locales: ['en', 'fr', 'de'],
localeConfigs: {},
},
},
{locale: 'it'},
Expand All @@ -88,6 +127,7 @@ describe('localizePath', () => {
defaultLocale: 'en',
locales: ['en', 'fr'],
currentLocale: 'fr',
localeConfigs: {},
},
options: {localizePath: true},
}),
Expand All @@ -103,6 +143,7 @@ describe('localizePath', () => {
defaultLocale: 'en',
locales: ['en', 'fr'],
currentLocale: 'fr',
localeConfigs: {},
},
options: {localizePath: true},
}),
Expand All @@ -118,6 +159,7 @@ describe('localizePath', () => {
defaultLocale: 'en',
locales: ['en', 'fr'],
currentLocale: 'en',
localeConfigs: {},
},
options: {localizePath: true},
}),
Expand All @@ -133,6 +175,7 @@ describe('localizePath', () => {
defaultLocale: 'en',
locales: ['en', 'fr'],
currentLocale: 'en',
localeConfigs: {},
},
// options: {localizePath: true},
}),
Expand All @@ -148,6 +191,7 @@ describe('localizePath', () => {
defaultLocale: 'en',
locales: ['en', 'fr'],
currentLocale: 'en',
localeConfigs: {},
},
// options: {localizePath: true},
}),
Expand Down
13 changes: 7 additions & 6 deletions packages/docusaurus/src/server/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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} = {},
Expand All @@ -30,12 +36,7 @@ Note: Docusaurus only support running one local at a time.`,
const localeConfigOptions: Partial<I18nLocaleConfig> =
i18nConfig.localeConfigs[locale];

// Default values
const localeConfigDefaults: I18nLocaleConfig = {
label: locale,
};

return {...localeConfigDefaults, ...localeConfigOptions};
return {...defaultLocaleConfig(locale), ...localeConfigOptions};
}

const localeConfigs = i18nConfig.locales.reduce((acc, locale) => {
Expand Down

0 comments on commit 64ee039

Please sign in to comment.