34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
import { defaultLocale, getLocale } from 'astro-i18n-aut';
|
|
import { ui, defaultLang, showDefaultLang } from './ui';
|
|
import type { AstroGlobal } from 'astro';
|
|
|
|
|
|
export function useTranslatedPath(astro: AstroGlobal) {
|
|
const locale = getLocale(astro.url);
|
|
return function translatePath(path: string, l: string = locale) {
|
|
return !showDefaultLang && l === defaultLang ? path : `/${l}${path}`.replace(/\/$/g, '');
|
|
}
|
|
|
|
}
|
|
|
|
export function useTranslations(astro: AstroGlobal) {
|
|
const lang = getLocale(astro.url);
|
|
return function t(key: keyof typeof ui[typeof defaultLang]) {
|
|
return ui[lang as keyof typeof ui][key] || ui[defaultLang][key];
|
|
}
|
|
}
|
|
|
|
export function parseSlug(id: string) {
|
|
const splitPath = id.split('/');
|
|
const split = splitPath.pop()?.split('.');
|
|
const lang = split?.length === 2 ? defaultLocale : split?.[1];
|
|
return [splitPath.join("/"), lang]
|
|
}
|
|
|
|
export function filterCollection<T extends { id: string }>(collection: T[], locale: string): T[] {
|
|
return collection.filter(post => {
|
|
const [_, lang] = parseSlug(post?.id);
|
|
return lang === locale;
|
|
});
|
|
}
|