feat(i18n): implement language switching and localization support
- Remove i18next configuration and dependencies - Add language switcher component - Integrate language change functionality with cookies - Update translations for English and Persian - Modify layout and page components to support dynamic language rendering - Refactor Tailwind CSS configuration for custom colors
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
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 Translates,
|
||||
fa,
|
||||
} as Record<TLocales, Translates>;
|
||||
|
||||
export const translates = (key: keyof Translates): string | (() => string) => {
|
||||
const lang = (getCookie('lang') || 'en') as TLocales;
|
||||
return locales[lang][key] || locales.en[key] || key;
|
||||
};
|
||||
|
||||
export const t = (key: keyof Translates) => translates(key);
|
||||
|
||||
export type TLocales = 'en' | 'fa';
|
||||
|
||||
export type Translates = {
|
||||
[K in keyof typeof en]: string | (() => string);
|
||||
};
|
||||
Reference in New Issue
Block a user