feat(i18n): add locale-based routing setup
Add next-intl routing config, proxy middleware, and update request config to use requestLocale from middleware. Add @/i18n/* path alias to tsconfig. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
parent
c14d2d6f26
commit
c4a35e97c3
4 changed files with 26 additions and 10 deletions
|
|
@ -1,14 +1,12 @@
|
|||
import { getRequestConfig } from "next-intl/server";
|
||||
import { headers } from "next/headers";
|
||||
import { getRequestConfig } from 'next-intl/server';
|
||||
import { hasLocale } from 'next-intl';
|
||||
import { routing } from './routing';
|
||||
|
||||
export default getRequestConfig(async () => {
|
||||
const headersList = await headers();
|
||||
const acceptLanguage = headersList.get("accept-language");
|
||||
|
||||
const browserLocale = acceptLanguage?.split(",")[0]?.split("-")[0];
|
||||
|
||||
const supportedLocales = ["en", "pl"];
|
||||
const locale = browserLocale && supportedLocales.includes(browserLocale) ? browserLocale : "en";
|
||||
export default getRequestConfig(async ({ requestLocale }) => {
|
||||
const requested = await requestLocale;
|
||||
const locale = hasLocale(routing.locales, requested)
|
||||
? requested
|
||||
: routing.defaultLocale;
|
||||
|
||||
return {
|
||||
locale,
|
||||
|
|
|
|||
7
src/i18n/routing.ts
Normal file
7
src/i18n/routing.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import { defineRouting } from 'next-intl/routing';
|
||||
|
||||
export const routing = defineRouting({
|
||||
locales: ['en', 'pl'],
|
||||
defaultLocale: 'en',
|
||||
localePrefix: 'as-needed',
|
||||
});
|
||||
10
src/proxy.ts
Normal file
10
src/proxy.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import createMiddleware from 'next-intl/middleware';
|
||||
import { routing } from './i18n/routing';
|
||||
|
||||
export default createMiddleware(routing);
|
||||
|
||||
export const config = {
|
||||
matcher: [
|
||||
'/((?!api|_next|_vercel|.*\\..*).*)',
|
||||
],
|
||||
};
|
||||
|
|
@ -24,6 +24,7 @@
|
|||
"@/lib/*": ["./src/lib/*"],
|
||||
"@/hooks/*": ["./src/hooks/*"],
|
||||
"@/app/*": ["./src/app/*"],
|
||||
"@/i18n/*": ["./src/i18n/*"],
|
||||
"@/constants": ["./src/constants.ts"]
|
||||
}
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in a new issue