slugSelector fn added

This commit is contained in:
Al Murad Uzzaman
2024-05-20 12:15:34 +06:00
parent ae35d98eee
commit e7da8ab6df
13 changed files with 181 additions and 252 deletions
+6 -10
View File
@@ -11,15 +11,14 @@ import languages from "@/config/language.json";
export const getSinglePage = async <C extends CollectionKey>(
collectionName: C,
lang: keyof ContentEntryMap | undefined,
lang: keyof ContentEntryMap | undefined
): Promise<CollectionEntry<C>[]> => {
const { default_language } = config.settings;
// If lang is undefined, use the default language
const selectedLanguageCode = lang || default_language;
const language = languages.find(
(l: any) => l.languageCode === selectedLanguageCode,
(l: any) => l.languageCode === selectedLanguageCode
);
if (!language) {
@@ -28,12 +27,11 @@ export const getSinglePage = async <C extends CollectionKey>(
const { contentDir } = language;
// Explicitly define the type of pages to CollectionEntry<C>[]
const pages: CollectionEntry<C>[] = (await getCollection(
contentDir as any,
({ id }: any) => {
return id.startsWith(collectionName) && !id.endsWith("-index.md");
},
}
)) as CollectionEntry<C>[];
// @ts-ignore
@@ -44,15 +42,14 @@ export const getSinglePage = async <C extends CollectionKey>(
export const getListPage = async <C extends CollectionKey>(
collectionName: C,
lang: keyof ContentEntryMap | undefined,
lang: keyof ContentEntryMap | undefined
): Promise<CollectionEntry<C>[]> => {
const { default_language } = config.settings;
// If lang is undefined, use the default language
const selectedLanguageCode = lang || default_language;
const language = languages.find(
(l: any) => l.languageCode == selectedLanguageCode,
(l: any) => l.languageCode == selectedLanguageCode
);
if (!language) {
@@ -61,12 +58,11 @@ export const getListPage = async <C extends CollectionKey>(
const { contentDir } = language;
// Fetch the collection based on the language
const pages: CollectionEntry<C>[] = (await getCollection(
contentDir as any,
({ id }: any) => {
return id.startsWith(collectionName);
},
}
)) as CollectionEntry<C>[];
return pages;
+118
View File
@@ -0,0 +1,118 @@
---
import config from "@/config/config.json";
import languagesJSON from "@/config/language.json";
import { getRelativeLocaleUrl } from "astro:i18n";
const { default_language } = config.settings;
const locales: { [key: string]: any } = {};
// Load menu and dictionary dynamically
languagesJSON.forEach((language) => {
const { languageCode } = language;
import(`../../config/menu.${languageCode}.json`).then((menu) => {
import(`../../i18n/${languageCode}.json`).then((dictionary) => {
locales[languageCode] = { ...menu, ...dictionary };
});
});
});
// Extract all languages from the locales object
const languages = Object.keys(locales);
// Export the locales and languages
export { languages, locales };
export function getLangFromUrl(url: URL): string {
const [, lang] = url.pathname.split("/");
if (locales.hasOwnProperty(lang)) {
return lang;
}
return default_language;
}
export const getTranslations = async (lang: string) => {
const {
default_language,
disable_languages,
}: { default_language: string; disable_languages: string[] } =
config.settings;
if (disable_languages.includes(lang)) {
lang = default_language;
}
let language = languagesJSON.find((l) => l.languageCode === lang);
if (!language) {
lang = default_language;
language = languagesJSON.find((l) => l.languageCode === default_language);
}
if (!language) {
throw new Error("Default language not found");
}
const contentDir = language.contentDir;
let menu, dictionary;
try {
menu = await import(`../../config/menu.${lang}.json`);
dictionary = await import(`../../i18n/${lang}.json`);
} catch (error) {
menu = await import(`../../config/menu.${default_language}.json`);
dictionary = await import(`../../i18n/${default_language}.json`);
}
return { ...menu.default, ...dictionary.default, contentDir };
};
const supportedLang = ["", ...languagesJSON.map((lang) => lang.languageCode)];
const disabledLanguages = config.settings.disable_languages as string[];
const filteredSupportedLang = supportedLang.filter(
(lang) => !disabledLanguages.includes(lang)
);
export { filteredSupportedLang as supportedLang };
export const slugSelector = (url: string, lang: string) => {
const { default_language, default_language_in_path } = config.settings;
const { trailing_slash } = config.site;
let constructedUrl;
// Determine the initial URL structure based on language
if (url === "/") {
constructedUrl = lang === default_language ? "/" : `/${lang}`;
} else {
constructedUrl = getRelativeLocaleUrl(lang, url, {
normalizeLocale: false,
});
}
// Adjust for trailing slash
if (trailing_slash) {
if (!constructedUrl.endsWith("/")) {
constructedUrl += "/";
}
} else {
if (constructedUrl.endsWith("/")) {
constructedUrl = constructedUrl.slice(0, -1);
}
}
// Ensure home URL is absolute
if (constructedUrl === "") {
constructedUrl = "/";
}
// Add language path if necessary
if (lang === default_language) {
constructedUrl = default_language_in_path
? `/${lang}${constructedUrl}`
: constructedUrl;
}
return constructedUrl;
};
---