Files
pasargad/src/locales/translates.ts
T
ahasani a3915377bd feat: implement internationalization and routing for locale-based pages
- Added RootLayout component to handle locale and message loading.
- Created Home page with header, about info, products, and story sections.
- Developed ProductPage for displaying product categories and products.
- Implemented ProductsPage to list product categories with banners.
- Added Projects page with a grid layout for project display.
- Configured server settings for port and host.
- Established navigation and routing for internationalization.
- Set up request handling for locale and message retrieval.
- Defined routing structure for supported locales.
- Created middleware for locale-based routing.
- Defined layout parameters for locale handling.
2025-06-07 16:31:52 +03:30

70 lines
1.9 KiB
TypeScript

import en from './en';
import fa from './fa';
// Universal getCookie function
export function getCookie(name: string): string | undefined {
if (typeof window !== 'undefined') {
// Client-side
const match = document.cookie.match(
new RegExp('(^| )' + name + '=([^;]+)'),
);
return match ? decodeURIComponent(match[2]) : undefined;
} else {
// Server-side (Next.js App Router)
try {
// Dynamically import to avoid errors in client bundle
const { cookies } = require('next/headers');
return cookies().get(name)?.value;
} catch {
return undefined;
}
}
}
// Universal setCookie function to change language
export function changeLanguage(lang: TLocales, days = 365) {
if (typeof window !== 'undefined') {
// Client-side
const expires = new Date(Date.now() + days * 864e5).toUTCString();
document.cookie = `lang=${encodeURIComponent(lang)}; expires=${expires}; path=/`;
} else {
// Server-side (Next.js App Router)
try {
const { cookies } = require('next/headers');
cookies().set('lang', lang, { path: '/', maxAge: days * 24 * 60 * 60 });
} catch {
// No-op on server if not available
}
}
}
export const locales = {
en: en as unknown as Translates,
fa,
} as Record<TLocales, Translates>;
export const translates = (
key: keyof Translates,
dynamicValue?: Record<string, string | number>,
): string | ((dynamicValue: Record<string, string | number>) => string) => {
const lang = (getCookie('lang') || 'en') as TLocales;
const value = locales[lang][key] || locales.en[key] || key;
if (typeof value === 'function') {
return value(dynamicValue || {});
}
return value;
};
export const t = (
key: keyof Translates,
dynamicValue?: Record<string, string | number>,
) => '';
export type TLocales = 'en' | 'fa';
export type Translates = {
[K in keyof typeof en]:
| string
| ((dynamicValue: Record<string, string | number>) => string);
};