mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-01-03 06:42:27 +08:00
* i18n support added
* fix: align i18n implementation with Next.js 16 guide
- Rename middleware.ts to proxy.ts (Next.js 16 convention)
- Fix params type to Promise<{lang: string}> for layout/metadata
- Add 'server-only' directive and dynamic imports to dictionaries.ts
- Add hasLocale type guard and notFound() for invalid locales
- Wrap LanguageToggle in Suspense for useSearchParams
- Fix dictionary key mismatch (learnmore -> learnMore)
- Improve Chinese translations per Gemini review:
- loading ellipsis, new -> 新建, styledMode -> 精致
- goodResponse/badResponse -> 有帮助/无帮助
- closeProtection -> 关闭确认, fileExceeds phrasing
- Improve Japanese translations per Gemini review:
- closeProtection -> ページ離脱確認
- invalidAccessCode phrasing, appendDiagram -> に追加
- styledMode -> スタイル付き
---------
Co-authored-by: dayuan.jiang <jdy.toh@gmail.com>
64 lines
1.9 KiB
TypeScript
64 lines
1.9 KiB
TypeScript
import { match as matchLocale } from "@formatjs/intl-localematcher"
|
|
import Negotiator from "negotiator"
|
|
import type { NextRequest } from "next/server"
|
|
import { NextResponse } from "next/server"
|
|
import { i18n } from "./lib/i18n/config"
|
|
|
|
function getLocale(request: NextRequest): string | undefined {
|
|
// Negotiator expects plain object so we need to transform headers
|
|
const negotiatorHeaders: Record<string, string> = {}
|
|
request.headers.forEach((value, key) => {
|
|
negotiatorHeaders[key] = value
|
|
})
|
|
|
|
// @ts-expect-error locales are readonly
|
|
const locales: string[] = i18n.locales
|
|
|
|
// Use negotiator and intl-localematcher to get best locale
|
|
const languages = new Negotiator({ headers: negotiatorHeaders }).languages(
|
|
locales,
|
|
)
|
|
|
|
const locale = matchLocale(languages, locales, i18n.defaultLocale)
|
|
|
|
return locale
|
|
}
|
|
|
|
export function proxy(request: NextRequest) {
|
|
const pathname = request.nextUrl.pathname
|
|
|
|
// Skip API routes, static files, and Next.js internals
|
|
if (
|
|
pathname.startsWith("/api/") ||
|
|
pathname.startsWith("/_next/") ||
|
|
pathname.includes("/favicon") ||
|
|
/\.(.*)$/.test(pathname)
|
|
) {
|
|
return
|
|
}
|
|
|
|
// Check if there is any supported locale in the pathname
|
|
const pathnameIsMissingLocale = i18n.locales.every(
|
|
(locale) =>
|
|
!pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}`,
|
|
)
|
|
|
|
// Redirect if there is no locale
|
|
if (pathnameIsMissingLocale) {
|
|
const locale = getLocale(request)
|
|
|
|
// Redirect to localized path
|
|
return NextResponse.redirect(
|
|
new URL(
|
|
`/${locale}${pathname.startsWith("/") ? "" : "/"}${pathname}`,
|
|
request.url,
|
|
),
|
|
)
|
|
}
|
|
}
|
|
|
|
export const config = {
|
|
// Matcher ignoring `/_next/` and `/api/`
|
|
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
|
|
}
|