25 lines
725 B
TypeScript
25 lines
725 B
TypeScript
import type { Primitive } from 'astro:schema'
|
|
import { ui, type TranslationKey } from './ui'
|
|
|
|
import { i18n } from 'astro:config/client'
|
|
|
|
export function getLangFromUrl(url: URL) {
|
|
const [, lang] = url.pathname.split('/')
|
|
|
|
if (lang in ui) return lang as keyof typeof ui
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
return i18n!.defaultLocale
|
|
}
|
|
|
|
export function useTranslations(lang?: string) {
|
|
return function t(key: TranslationKey, payload?: Primitive): string {
|
|
const translation =
|
|
ui[lang as keyof typeof ui]?.[key] || ui[i18n!.defaultLocale as keyof typeof ui][key]
|
|
|
|
if (typeof translation === 'function') return translation(String(payload))
|
|
|
|
return translation
|
|
}
|
|
}
|