26 lines
902 B
TypeScript
26 lines
902 B
TypeScript
export const SUPPORTED_LOCALES = ['th', 'en', 'ja', 'zh', 'vi', 'ko', 'lo', 'it'] as const;
|
|
export type Locale = typeof SUPPORTED_LOCALES[number];
|
|
|
|
export const DEFAULT_LOCALE: Locale = 'th';
|
|
|
|
export function isSupportedLocale(locale: string): locale is Locale {
|
|
return SUPPORTED_LOCALES.includes(locale as Locale);
|
|
}
|
|
|
|
export function getPathWithoutLocale(path: string): string {
|
|
const segments = path.split('/').filter(Boolean);
|
|
if (segments.length > 0 && isSupportedLocale(segments[0])) {
|
|
segments.shift();
|
|
}
|
|
return '/' + segments.join('/');
|
|
}
|
|
|
|
export function getPathWithLocale(path: string, locale: string): string {
|
|
if (!path) return `/${locale}`;
|
|
if (path.startsWith('http')) return path; // Skip absolute external URLs
|
|
|
|
const pathWithoutLocale = getPathWithoutLocale(path);
|
|
if (pathWithoutLocale === '/') return `/${locale}`;
|
|
return `/${locale}${pathWithoutLocale}`;
|
|
}
|