Skip to content

Commit

Permalink
fix(v2): i18n should not crash theme without footer (#3940)
Browse files Browse the repository at this point in the history
* Fix theme translations when no footer

* fix TS issues
  • Loading branch information
slorber authored Dec 18, 2020
1 parent 3fc29f4 commit ef49c2b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,24 @@ describe('translateThemeConfig', () => {
).toMatchSnapshot();
});
});

describe('getTranslationFiles and translateThemeConfig isomorphism', () => {
function verifyIsomorphism(themeConfig: ThemeConfig) {
const translationFiles = getTranslationFiles({themeConfig});
const translatedThemeConfig = translateThemeConfig({
themeConfig,
translationFiles,
});
expect(translatedThemeConfig).toEqual(themeConfig);
}

test('should be verified for main sample', () => {
verifyIsomorphism(ThemeConfigSample);
});

// undefined footer should not make the translation code crash
// See https://github.com/facebook/docusaurus/issues/3936
test('should be verified for sample without footer', () => {
verifyIsomorphism({...ThemeConfigSample, footer: undefined});
});
});
18 changes: 12 additions & 6 deletions packages/docusaurus-theme-classic/src/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,17 @@ export function getTranslationFiles({
}: {
themeConfig: ThemeConfig;
}): TranslationFile[] {
return [
const translationFiles: (TranslationFile | undefined)[] = [
{path: 'navbar', content: getNavbarTranslationFile(themeConfig.navbar)},
{path: 'footer', content: getFooterTranslationFile(themeConfig.footer)},
themeConfig.footer
? {
path: 'footer',
content: getFooterTranslationFile(themeConfig.footer),
}
: undefined,
];

return translationFiles.filter(Boolean) as TranslationFile[];
}

export function translateThemeConfig({
Expand All @@ -153,9 +160,8 @@ export function translateThemeConfig({
themeConfig.navbar,
translationFilesMap.navbar.content,
),
footer: translateFooter(
themeConfig.footer,
translationFilesMap.footer.content,
),
footer: themeConfig.footer
? translateFooter(themeConfig.footer, translationFilesMap.footer.content)
: undefined,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export type ThemeConfig = {
colorMode: any;
announcementBar: any;
prism: any;
footer: Footer;
footer: Footer | undefined;
hideableSidebar: any;
};

Expand Down

0 comments on commit ef49c2b

Please sign in to comment.