mirror of
https://github.com/10h30/astroplate.git
synced 2026-07-15 20:53:29 +09:00
config ln array changed
This commit is contained in:
+12
-22
@@ -1,19 +1,4 @@
|
||||
---
|
||||
// import {
|
||||
// getCollection,
|
||||
// type CollectionEntry,
|
||||
// type CollectionKey,
|
||||
// } from "astro:content";
|
||||
|
||||
// export const getSinglePage = async <C extends CollectionKey>(
|
||||
// collectionName: C,
|
||||
// ): Promise<CollectionEntry<C>[]> => {
|
||||
// const allPages = await getCollection(collectionName);
|
||||
// const removeIndex = allPages.filter((data) => data.id.match(/^(?!-)/));
|
||||
// const removeDrafts = removeIndex.filter((data) => !data.data.draft);
|
||||
// return removeDrafts;
|
||||
// };
|
||||
|
||||
import {
|
||||
getCollection,
|
||||
type CollectionEntry,
|
||||
@@ -31,15 +16,17 @@ export const getSinglePage = async <C extends CollectionKey>(
|
||||
const langCollection: keyof ContentEntryMap = lang as keyof ContentEntryMap;
|
||||
|
||||
// Explicitly define the type of pages to CollectionEntry<C>[]
|
||||
const pages: CollectionEntry<C>[] = await getCollection(langCollection || defaultLang, ({ id }) => {
|
||||
return id.startsWith(collectionName) && !id.endsWith("-index.md");
|
||||
}) as CollectionEntry<C>[];
|
||||
const pages: CollectionEntry<C>[] = (await getCollection(
|
||||
langCollection || defaultLang,
|
||||
({ id }: any) => {
|
||||
return id.startsWith(collectionName) && !id.endsWith("-index.md");
|
||||
}
|
||||
)) as CollectionEntry<C>[];
|
||||
|
||||
const removeDrafts = pages.filter((data) => !data.data.draft);
|
||||
return removeDrafts;
|
||||
};
|
||||
|
||||
|
||||
export const getListPage = async <C extends CollectionKey>(
|
||||
collectionName: C,
|
||||
lang: keyof ContentEntryMap
|
||||
@@ -47,9 +34,12 @@ export const getListPage = async <C extends CollectionKey>(
|
||||
const { defaultLang } = config.language;
|
||||
const langCollection: keyof ContentEntryMap = lang as keyof ContentEntryMap;
|
||||
// Fetch the collection based on the language
|
||||
const pages: CollectionEntry<C>[] = await getCollection(langCollection || defaultLang, ({ id }) => {
|
||||
return id.startsWith(collectionName);
|
||||
}) as CollectionEntry<C>[];
|
||||
const pages: CollectionEntry<C>[] = (await getCollection(
|
||||
langCollection || defaultLang,
|
||||
({ id }: any) => {
|
||||
return id.startsWith(collectionName);
|
||||
}
|
||||
)) as CollectionEntry<C>[];
|
||||
|
||||
return pages;
|
||||
};
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
---
|
||||
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: any, name: string) => {
|
||||
// const singlePages = await getSinglePage(collection);
|
||||
const singlePages = await getCollection(
|
||||
collection as keyof ContentEntryMap,
|
||||
({ id }) => {
|
||||
({ id }: any) => {
|
||||
return id.startsWith("blog") && !id.endsWith("-index.md");
|
||||
}
|
||||
);
|
||||
@@ -31,7 +29,7 @@ export const getAllTaxonomy = async (collection: any, name: string) => {
|
||||
// const singlePages = await getSinglePage(collection);
|
||||
const singlePages = await getCollection(
|
||||
collection as keyof ContentEntryMap,
|
||||
({ id }) => {
|
||||
({ id }: any) => {
|
||||
return id.startsWith("blog") && !id.endsWith("-index.md");
|
||||
}
|
||||
);
|
||||
|
||||
+23
-41
@@ -1,14 +1,18 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import config from "@/config/config.json";
|
||||
import languagesJSON from "@/config/language.json";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
const { defaultLang } = config.language;
|
||||
|
||||
const menusFolderPath = './src/config';
|
||||
const menusFolderPath = "./src/config";
|
||||
|
||||
const locales = fs.readdirSync(menusFolderPath)
|
||||
.filter(file => /^menu\.[a-z]{2}\.json$/.test(file))
|
||||
.map(file => {
|
||||
const locales = fs
|
||||
.readdirSync(menusFolderPath)
|
||||
.filter((file) => /^menu\.[a-z]{2}\.json$/.test(file))
|
||||
.map((file) => {
|
||||
const filePath = path.join(menusFolderPath, file);
|
||||
const localeName = file.split('.')[1]; // Extract language code from file name
|
||||
const localeData = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
||||
const localeName = file.split(".")[1]; // Extract language code from file name
|
||||
const localeData = JSON.parse(fs.readFileSync(filePath, "utf8"));
|
||||
return { [localeName]: localeData };
|
||||
})
|
||||
.reduce((accumulator, locale) => {
|
||||
@@ -19,44 +23,22 @@ const locales = fs.readdirSync(menusFolderPath)
|
||||
const languages = Object.keys(locales);
|
||||
|
||||
// Export the locales and languages
|
||||
export { locales, languages };
|
||||
export { languages, locales };
|
||||
|
||||
export function getLangFromUrl(url: URL): string {
|
||||
const [, lang] = url.pathname.split('/');
|
||||
const [, lang] = url.pathname.split("/");
|
||||
if (locales.hasOwnProperty(lang)) {
|
||||
return lang;
|
||||
}
|
||||
return 'en';
|
||||
return defaultLang;
|
||||
}
|
||||
|
||||
export const getTranslations = async (lang: string) => {
|
||||
const menu = await import(`../../config/menu.${lang || defaultLang}.json`);
|
||||
const dictionary = await import(`../../i18n/${lang || defaultLang}.json`);
|
||||
return { ...menu, ...dictionary };
|
||||
};
|
||||
|
||||
|
||||
// import { ui } from '@/i18n/ui';
|
||||
// import config from "@/config/config.json";
|
||||
// const {defaultLang}: {defaultLang : keyof typeof ui} = config.language as any;
|
||||
|
||||
// export function useTranslations(lang: keyof typeof ui) {
|
||||
// return function t(key: keyof typeof ui[typeof defaultLang]) {
|
||||
// return ui[lang][key] || ui[defaultLang][key];
|
||||
// }
|
||||
// }
|
||||
|
||||
import { ui } from '@/i18n/ui';
|
||||
import config from "@/config/config.json";
|
||||
|
||||
const { defaultLang }: { defaultLang: keyof typeof ui } = config.language as any;
|
||||
|
||||
export function useTranslations(lang?: keyof typeof ui) {
|
||||
const activeLang = lang || defaultLang;
|
||||
|
||||
return function t(key: keyof typeof ui[typeof defaultLang]): string {
|
||||
const translation = ui[activeLang][key];
|
||||
|
||||
// If not found, fall back to the default language
|
||||
if (translation) {
|
||||
return translation;
|
||||
} else {
|
||||
return ui[defaultLang][key];
|
||||
}
|
||||
};
|
||||
}
|
||||
export const supportedLang = [""].concat(
|
||||
languagesJSON.map((lang) => lang.languageCode),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user