Skip to content

Commit

Permalink
Support redirectToDefaultLang in static mode (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
justincy committed Mar 13, 2020
1 parent e91b055 commit f6729b8
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion cli/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
finalPagesDir = 'pages',
localesPath = 'locales',
pages = {},
redirectToDefaultLang = false,
} = require(process.cwd() + '/i18n.json') || {}

function readDirR(dir) {
Expand All @@ -35,6 +36,10 @@ async function createPagesDir(langs = []) {
execSync(`mkdir ${finalPagesDir}/${lang}`)
})

if (redirectToDefaultLang) {
fs.writeFileSync(`${finalPagesDir}/[...path].js`, getCatchAllTemplate())
}

console.log(`Building pages | from ${currentPagesDir} to ${finalPagesDir}`)
readPageNamespaces(langs)
}
Expand Down Expand Up @@ -140,7 +145,7 @@ function buildPageInAllLocales(pagePath, namespaces, langs) {
})

// For default lang
if (langs.includes(defaultLanguage)) {
if (langs.includes(defaultLanguage) && !redirectToDefaultLang) {
buildPageLocale({
lang: defaultLanguage,
namespaces,
Expand All @@ -150,3 +155,18 @@ function buildPageInAllLocales(pagePath, namespaces, langs) {
})
}
}

function getCatchAllTemplate() {
return `import { useRouter } from 'next/router';
const CatchAll = () => {
const router = useRouter();
if (router.query.path) {
router.push(\`/${defaultLanguage}/\${router.query.path}\`);
}
return <></>;
};
export default CatchAll;
`
}

0 comments on commit f6729b8

Please sign in to comment.