refactor: centralize locale constants to prevent breaking changes
Created src/lib/i18n/locales.ts as single source of truth for: - SUPPORTED_LOCALES array - LOCALE_COOKIE name - DEFAULT_LOCALE - LOCALE_CONFIG (labels, flags, Saleor locale mapping) - Helper functions (isValidLocale, getSaleorLocale, getLocaleFromPath) Updated all files to use centralized constants: - middleware.ts - Header.tsx - ProductCard.tsx - sitemap.ts - root layout and locale layout - routing.ts Benefits: - Adding new locale only requires updating ONE file (locales.ts) - No more hardcoded locale lists scattered across codebase - Cookie name defined in one place - Type-safe locale validation
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { SUPPORTED_LOCALES, DEFAULT_LOCALE, LOCALE_COOKIE, getLocaleFromPath } from "@/lib/i18n/locales";
|
||||
|
||||
const LOCALE_COOKIE = "NEXT_LOCALE";
|
||||
const OLD_SERBIAN_PATHS = ["products", "about", "contact", "checkout"];
|
||||
|
||||
export default function middleware(request: NextRequest) {
|
||||
const pathname = request.nextUrl.pathname;
|
||||
@@ -9,9 +10,9 @@ export default function middleware(request: NextRequest) {
|
||||
const acceptLanguage = request.headers.get("accept-language") || "";
|
||||
|
||||
if (pathname === "/" || pathname === "") {
|
||||
let locale = "sr";
|
||||
let locale = DEFAULT_LOCALE;
|
||||
|
||||
if (cookieLocale && ["sr", "en", "de", "fr"].includes(cookieLocale)) {
|
||||
if (cookieLocale && SUPPORTED_LOCALES.includes(cookieLocale as typeof SUPPORTED_LOCALES[number])) {
|
||||
locale = cookieLocale;
|
||||
} else if (acceptLanguage.includes("en")) {
|
||||
locale = "en";
|
||||
@@ -22,15 +23,14 @@ export default function middleware(request: NextRequest) {
|
||||
return NextResponse.redirect(url, 301);
|
||||
}
|
||||
|
||||
const oldSerbianPaths = ["products", "about", "contact", "checkout"];
|
||||
const isOldSerbianPath = oldSerbianPaths.some(
|
||||
const isOldSerbianPath = OLD_SERBIAN_PATHS.some(
|
||||
(path) => pathname === `/${path}` || pathname.startsWith(`/${path}/`)
|
||||
);
|
||||
|
||||
if (isOldSerbianPath) {
|
||||
let locale = "sr";
|
||||
let locale = DEFAULT_LOCALE;
|
||||
|
||||
if (cookieLocale && ["sr", "en", "de", "fr"].includes(cookieLocale)) {
|
||||
if (cookieLocale && SUPPORTED_LOCALES.includes(cookieLocale as typeof SUPPORTED_LOCALES[number])) {
|
||||
locale = cookieLocale;
|
||||
} else if (acceptLanguage.includes("en")) {
|
||||
locale = "en";
|
||||
|
||||
Reference in New Issue
Block a user