refactor: make locale handling truly centralized and robust
- Added getPathWithoutLocale() and buildLocalePath() helpers to locales.ts - Updated Header to use centralized helpers instead of hardcoded regex - Updated middleware to use SUPPORTED_LOCALES in matcher config - Updated LocaleProvider to use isValidLocale() instead of hardcoded array To add a new language now, only update: 1. SUPPORTED_LOCALES in locales.ts 2. LOCALE_CONFIG entry with label, flag, saleorLocale 3. Add translation keys to all message files All routing now uses centralized constants - no more hardcoded locale lists.
This commit is contained in:
@@ -9,7 +9,7 @@ import { useTranslations } from "next-intl";
|
||||
import { useSaleorCheckoutStore } from "@/stores/saleorCheckoutStore";
|
||||
import { User, ShoppingBag, Menu, X, Globe } from "lucide-react";
|
||||
import CartDrawer from "@/components/cart/CartDrawer";
|
||||
import { SUPPORTED_LOCALES, LOCALE_COOKIE, LOCALE_CONFIG, isValidLocale } from "@/lib/i18n/locales";
|
||||
import { SUPPORTED_LOCALES, LOCALE_COOKIE, LOCALE_CONFIG, isValidLocale, getPathWithoutLocale, buildLocalePath } from "@/lib/i18n/locales";
|
||||
import type { Locale } from "@/lib/i18n/locales";
|
||||
|
||||
interface HeaderProps {
|
||||
@@ -48,8 +48,8 @@ export default function Header({ locale = "sr" }: HeaderProps) {
|
||||
return;
|
||||
}
|
||||
document.cookie = `${LOCALE_COOKIE}=${newLocale}; path=/; max-age=31536000`;
|
||||
const pathWithoutLocale = pathname.replace(/^\/(sr|en|de|fr)/, "") || "/";
|
||||
const newPath = `/${newLocale}${pathWithoutLocale}`;
|
||||
const pathWithoutLocale = getPathWithoutLocale(pathname);
|
||||
const newPath = buildLocalePath(newLocale as Locale, pathWithoutLocale);
|
||||
window.location.replace(newPath);
|
||||
setLangDropdownOpen(false);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user