这是一个巴适伙伴企业官网项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

32 lines
919 B

import { zh } from './zh';
import { en } from './en';
import { hk } from './hk';
export type Locale = 'zh' | 'en' | 'hk';
export type TranslationKey = keyof typeof zh;
const translations: Record<Locale, typeof zh> = { zh, en, hk };
export function getTranslation(locale: Locale) {
return translations[locale] || translations.zh;
}
// Replaces the old Astro `getLocaleFromUrl(url)` — locale is now derived
// from the browser pathname (zh at `/`, en at `/en/*`, hk at `/hk/*`).
export function getLocaleFromPathname(): Locale {
if (typeof window === 'undefined') return 'zh';
const p = window.location.pathname;
if (p.startsWith('/en')) return 'en';
if (p.startsWith('/hk')) return 'hk';
return 'zh';
}
export function getLocalizedPath(path: string, locale: Locale): string {
if (locale === 'en') {
return `/en${path}`;
}
if (locale === 'hk') {
return `/hk${path}`;
}
return path;
}