Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
amannn committed Nov 2, 2022
1 parent bb3acc6 commit ccb9c8a
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 45 deletions.
1 change: 1 addition & 0 deletions packages/example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.next/
.DS_Store
tsconfig.tsbuildinfo
.vscode
4 changes: 0 additions & 4 deletions packages/example/next.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
module.exports = {
i18n: {
locales: ['en', 'de'],
defaultLocale: 'en'
},
experimental: {appDir: true}
};
42 changes: 42 additions & 0 deletions packages/example/src/app/[locale]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export function generateStaticParams() {
return ['de', 'en'].map((locale) => ({locale}));
}

type Props = {
params: {
locale: string;
};
};

export default function Index({params: {locale}}: Props) {
// TODO: Validate locale or redirect to default locale

return <p>Hello {locale}</p>;
}

// import {useTranslations} from 'next-intl';
// import LocaleSwitcher from 'components/LocaleSwitcher';
// import PageLayout from 'components/PageLayout';

// import {useContext} from 'react';
// import ServerOnlyContext from './ServerOnlyContext';

// export default function Index() {
// // TODO: Use middleware to redirect to a specific locale

// const serverOnly = useContext(ServerOnlyContext);
// // const t = useTranslations('Index');

// console.log('Index');

// // return <p>{t('title')}</p>;

// return <p>Hello {serverOnly.only.for.server + 10}</p>;

// // return (
// // <PageLayout title={t('title')}>
// // <p>{t('description')}</p>
// // <LocaleSwitcher />
// // </PageLayout>
// // );
// }
21 changes: 4 additions & 17 deletions packages/example/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import acceptLanguageParser from 'accept-language-parser';
import {headers} from 'next/headers';
import {ReactNode} from 'react';
// import Provider from './Provider';
import ServerOnlyContext from './ServerOnlyContext';
Expand All @@ -8,22 +6,11 @@ type Props = {
children: ReactNode;
};

function resolveLocale(requestHeaders: Headers) {
const supportedLanguages = ['en', 'de'];
const defaultLangauge = supportedLanguages[0];
const locale =
acceptLanguageParser.pick(
supportedLanguages,
requestHeaders.get('accept-language') || defaultLangauge
) || defaultLangauge;
export default function RootLayout({children, ...rest}: Props) {
console.log(rest);

return locale;
}

export default function RootLayout({children}: Props) {
const locale = resolveLocale(headers());

console.log(ServerOnlyContext);
// How to get this from the URL?
const locale = 'en';

return (
<html lang={locale}>
Expand Down
24 changes: 0 additions & 24 deletions packages/example/src/app/page.tsx

This file was deleted.

4 changes: 4 additions & 0 deletions packages/example/src/i18n.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
locales: ['en', 'de'],
defaultLocale: 'en'
};
26 changes: 26 additions & 0 deletions packages/example/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import acceptLanguageParser from 'accept-language-parser';
import {headers} from 'next/headers';
import {NextResponse} from 'next/server';
import type {NextRequest} from 'next/server';
import i18n from './i18n';

// Somehow not invoked?

function resolveLocale(requestHeaders: Headers) {
const locale =
acceptLanguageParser.pick(
i18n.locales,
requestHeaders.get('accept-language') || i18n.defaultLocale
) || i18n.defaultLocale;

return locale;
}

export function middleware(request: NextRequest) {
const locale = resolveLocale(headers());
return NextResponse.redirect(new URL('/' + locale, request.url));
}

export const config = {
matcher: '/test'
};

0 comments on commit ccb9c8a

Please sign in to comment.