remove multilingual and create a new multilingual branch

This commit is contained in:
Somrat
2024-11-18 09:55:18 +06:00
parent b092921209
commit f7bc32a629
82 changed files with 453 additions and 1928 deletions
+3 -121
View File
@@ -3,132 +3,14 @@ import {
getCollection,
type CollectionEntry,
type CollectionKey,
type ContentEntryMap,
} from "astro:content";
import config from "@/config/config.json";
import languages from "@/config/language.json";
export const getSP = async <C extends CollectionKey>(
collectionName: C,
lang: keyof ContentEntryMap | undefined
): Promise<CollectionEntry<C>[]> => {
const { default_language } = config.settings;
const selectedLanguageCode = lang || default_language;
const language = languages.find(
(l: any) => l.languageCode === selectedLanguageCode
);
if (!language) {
throw new Error("Language not found");
}
const { contentDir } = language;
const pages: CollectionEntry<C>[] = (await getCollection(
contentDir as any,
({ id }: any) => {
return id.startsWith(collectionName) && !id.endsWith("-index.md");
}
)) as CollectionEntry<C>[];
// @ts-ignore
const removeDrafts = pages.filter((data) => !data.data.draft);
return removeDrafts;
};
export const getLP = async <C extends CollectionKey>(
collectionName: C,
lang: keyof ContentEntryMap | undefined
): Promise<CollectionEntry<C>[]> => {
const { default_language } = config.settings;
const selectedLanguageCode = lang || default_language;
const language = languages.find(
(l: any) => l.languageCode == selectedLanguageCode
);
if (!language) {
throw new Error("Language not found");
}
const { contentDir } = language;
const pages: CollectionEntry<C>[] = (await getCollection(
contentDir as any,
({ id }: any) => {
return id.startsWith(collectionName);
}
)) as CollectionEntry<C>[];
return pages;
};
export const getSinglePage = async <C extends CollectionKey>(
collectionName: C,
lang: keyof ContentEntryMap | undefined,
subCollectionName?: string
): Promise<CollectionEntry<C>[]> => {
const { default_language } = config.settings;
const selectedLanguageCode = lang || default_language;
const language = languages.find(
(l: any) => l.languageCode === selectedLanguageCode
);
if (!language) {
throw new Error("Language not found");
}
const { contentDir } = language;
const path = subCollectionName
? `${contentDir}/${subCollectionName}`
: contentDir;
const pages: CollectionEntry<C>[] = (await getCollection(
collectionName as any,
({ id }: any) => {
return id.startsWith(path) && !id.endsWith("-index.md");
}
)) as CollectionEntry<C>[];
// @ts-ignore
const removeDrafts = pages.filter((data) => !data.data.draft);
const allPages = await getCollection(collectionName);
const removeIndex = allPages.filter((data) => data.id.match(/^(?!-)/));
const removeDrafts = removeIndex.filter((data) => !data.data.draft);
return removeDrafts;
};
export const getListPage = async <C extends CollectionKey>(
collectionName: C,
lang: keyof ContentEntryMap | undefined
): Promise<CollectionEntry<C>[]> => {
const { default_language } = config.settings;
const selectedLanguageCode = lang || default_language;
const language = languages.find(
(l: any) => l.languageCode == selectedLanguageCode
);
if (!language) {
throw new Error("Language not found");
}
const { contentDir } = language;
const pages: CollectionEntry<C>[] = (await getCollection(
collectionName as any,
({ id }: any) => {
return id.startsWith(contentDir);
}
)) as CollectionEntry<C>[];
return pages;
};
---
+5 -50
View File
@@ -1,34 +1,10 @@
---
import config from "@/config/config.json";
import languages from "@/config/language.json";
import { getSinglePage } from "@/lib/contentParser.astro";
import { slugify } from "@/lib/utils/textConverter";
import type { ContentEntryMap } from "astro:content";
import { getCollection } from "astro:content";
// get taxonomy from frontmatter
export const getTaxonomy = async (collection: string, name: string) => {
const { default_language } = config.settings;
const language = languages.find((l) => l.languageCode === collection);
let actualCollection = default_language;
if (language) {
actualCollection = language.contentDir;
} else {
const defaultLanguageMatch = languages.find(
(l) => l.languageCode === default_language
);
if (defaultLanguageMatch) {
actualCollection = defaultLanguageMatch.contentDir;
}
}
const singlePages = await getCollection(
"blog" as keyof ContentEntryMap,
({ id }: any) => {
return id.startsWith(actualCollection) && !id.endsWith("-index.md");
}
);
export const getTaxonomy = async (collection: any, name: string) => {
const singlePages = await getSinglePage(collection);
const taxonomyPages = singlePages.map((page: any) => page.data[name]);
let taxonomies: string[] = [];
for (let i = 0; i < taxonomyPages.length; i++) {
@@ -42,29 +18,8 @@ export const getTaxonomy = async (collection: string, name: string) => {
};
// get all taxonomies from frontmatter
export const getAllTaxonomy = async (collection: string, name: string) => {
const { default_language } = config.settings;
const language = languages.find((l) => l.languageCode === collection);
let actualCollection = default_language;
if (language) {
actualCollection = language.contentDir;
} else {
const defaultLanguageMatch = languages.find(
(l) => l.languageCode === default_language
);
if (defaultLanguageMatch) {
actualCollection = defaultLanguageMatch.contentDir;
}
}
const singlePages = await getCollection(
"blog" as keyof ContentEntryMap,
({ id }: any) => {
return id.startsWith(actualCollection) && !id.endsWith("-index.md");
}
);
export const getAllTaxonomy = async (collection: any, name: string) => {
const singlePages = await getSinglePage(collection);
const taxonomyPages = singlePages.map((page: any) => page.data[name]);
let taxonomies: string[] = [];
for (let i = 0; i < taxonomyPages.length; i++) {
-110
View File
@@ -1,110 +0,0 @@
import { getRelativeLocaleUrl } from "astro:i18n";
import config from "../../config/config.json";
import languagesJSON from "../../config/language.json";
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[];
// Filter out disabled languages from supportedLang
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_subdir } = 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,
});
}
// Add language path if necessary
if (lang === default_language && default_language_in_subdir) {
constructedUrl = `/${lang}${constructedUrl}`;
}
// Adjust for trailing slash
if (trailing_slash) {
if (!constructedUrl.endsWith("/")) {
constructedUrl += "/";
}
} else {
if (constructedUrl.endsWith("/") && constructedUrl !== "/") {
constructedUrl = constructedUrl.slice(0, -1);
}
}
return constructedUrl;
};