feat: some shit
This commit is contained in:
26
src/i18n/ui.ts
Normal file
26
src/i18n/ui.ts
Normal file
@ -0,0 +1,26 @@
|
||||
|
||||
export const languages = {
|
||||
en: 'English',
|
||||
de: 'Deutsch',
|
||||
};
|
||||
|
||||
export const defaultLang = 'de';
|
||||
|
||||
export const ui = {
|
||||
en: {
|
||||
"en": "English",
|
||||
"de": "Deutsch",
|
||||
'nav.home': 'Home',
|
||||
'nav.blog': 'Blog',
|
||||
'nav.about': 'About',
|
||||
},
|
||||
de: {
|
||||
"en": "English",
|
||||
"de": "Deutsch",
|
||||
'nav.home': 'Home',
|
||||
'nav.blog': 'Blog',
|
||||
'nav.about': 'Über',
|
||||
},
|
||||
} as const;
|
||||
|
||||
export const showDefaultLang = false;
|
28
src/i18n/utils.ts
Normal file
28
src/i18n/utils.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { defaultLocale } from 'astro-i18n-aut';
|
||||
import { ui, defaultLang, showDefaultLang } from './ui';
|
||||
|
||||
export function useTranslatedPath(lang: string) {
|
||||
return function translatePath(path: string, l: string = lang) {
|
||||
return !showDefaultLang && l === defaultLang ? path : `/${l}${path}`.replace(/\/$/g, '');
|
||||
}
|
||||
}
|
||||
|
||||
export function useTranslations(lang: string) {
|
||||
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) {
|
||||
return collection.filter(post => {
|
||||
const [_, lang] = parseSlug(post?.id);
|
||||
return lang === locale;
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user