Skip to content

Commit

Permalink
Handle unsupported languages
Browse files Browse the repository at this point in the history
  • Loading branch information
justincy committed May 5, 2020
1 parent 0b9d9a9 commit 2598b75
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 3 additions & 1 deletion examples/with-dynamic-routes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"private": true,
"license": "MIT",
"scripts": {
"dev": "next dev"
"build": "next build",
"dev": "next dev",
"start": "next start"
},
"dependencies": {
"next": "9.3.5",
Expand Down
11 changes: 10 additions & 1 deletion examples/with-dynamic-routes/utils/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ async function importNamespaces(lang, namespaces = []) {

export async function getI18nProps(ctx, namespaces) {
const lang = ctx.params?.lang || defaultLanguage

if (!allLanguages.includes(lang)) {
if (ctx.res) {
console.log(ctx.req.url);
ctx.res.writeHead(302, { Location: ctx.req.url.replace(new RegExp(`^\/${lang}`), `/${defaultLanguage}`) });
ctx.res.end();
return;
} else {
throw new Error(`Locale ${lang} is not supported`);
}
}
return {
lang,
namespaces: await importNamespaces(lang, namespaces),
Expand Down

0 comments on commit 2598b75

Please sign in to comment.