diff --git a/src/config/config.json b/src/config/config.json index ea6b207..d3b4957 100755 --- a/src/config/config.json +++ b/src/config/config.json @@ -3,7 +3,7 @@ "title": "Astroplate", "base_url": "https://astroplate.netlify.app", "base_path": "/", - "trailing_slash": true, + "trailing_slash": false, "favicon": "/images/favicon.png", "logo": "/images/logo.png", "logo_darkmode": "/images/logo-darkmode.png", diff --git a/src/config/language.json b/src/config/language.json index ace7679..706b137 100644 --- a/src/config/language.json +++ b/src/config/language.json @@ -2,13 +2,13 @@ { "languageName": "En", "languageCode": "en", - "contentDir": "en", + "contentDir": "english", "weight": 1 }, { "languageName": "Fr", "languageCode": "fr", - "contentDir": "fr", + "contentDir": "french", "weight": 2 } ] diff --git a/src/content/en/about/-index.md b/src/content/english/about/-index.md similarity index 100% rename from src/content/en/about/-index.md rename to src/content/english/about/-index.md diff --git a/src/content/en/authors/-index.md b/src/content/english/authors/-index.md similarity index 100% rename from src/content/en/authors/-index.md rename to src/content/english/authors/-index.md diff --git a/src/content/en/authors/john-doe.md b/src/content/english/authors/john-doe.md similarity index 100% rename from src/content/en/authors/john-doe.md rename to src/content/english/authors/john-doe.md diff --git a/src/content/en/authors/sam-wilson.md b/src/content/english/authors/sam-wilson.md similarity index 100% rename from src/content/en/authors/sam-wilson.md rename to src/content/english/authors/sam-wilson.md diff --git a/src/content/en/authors/william-jacob.md b/src/content/english/authors/william-jacob.md similarity index 100% rename from src/content/en/authors/william-jacob.md rename to src/content/english/authors/william-jacob.md diff --git a/src/content/en/blog/-index.md b/src/content/english/blog/-index.md similarity index 100% rename from src/content/en/blog/-index.md rename to src/content/english/blog/-index.md diff --git a/src/content/en/blog/post-1.md b/src/content/english/blog/post-1.md similarity index 100% rename from src/content/en/blog/post-1.md rename to src/content/english/blog/post-1.md diff --git a/src/content/en/blog/post-2.md b/src/content/english/blog/post-2.md similarity index 100% rename from src/content/en/blog/post-2.md rename to src/content/english/blog/post-2.md diff --git a/src/content/en/blog/post-3.md b/src/content/english/blog/post-3.md similarity index 100% rename from src/content/en/blog/post-3.md rename to src/content/english/blog/post-3.md diff --git a/src/content/en/blog/post-4.md b/src/content/english/blog/post-4.md similarity index 100% rename from src/content/en/blog/post-4.md rename to src/content/english/blog/post-4.md diff --git a/src/content/en/contact/-index.md b/src/content/english/contact/-index.md similarity index 100% rename from src/content/en/contact/-index.md rename to src/content/english/contact/-index.md diff --git a/src/content/en/homepage/-index.md b/src/content/english/homepage/-index.md similarity index 100% rename from src/content/en/homepage/-index.md rename to src/content/english/homepage/-index.md diff --git a/src/content/en/pages/elements.mdx b/src/content/english/pages/elements.mdx similarity index 100% rename from src/content/en/pages/elements.mdx rename to src/content/english/pages/elements.mdx diff --git a/src/content/en/pages/privacy-policy.md b/src/content/english/pages/privacy-policy.md similarity index 100% rename from src/content/en/pages/privacy-policy.md rename to src/content/english/pages/privacy-policy.md diff --git a/src/content/en/sections/call-to-action.md b/src/content/english/sections/call-to-action.md similarity index 100% rename from src/content/en/sections/call-to-action.md rename to src/content/english/sections/call-to-action.md diff --git a/src/content/en/sections/testimonial.md b/src/content/english/sections/testimonial.md similarity index 100% rename from src/content/en/sections/testimonial.md rename to src/content/english/sections/testimonial.md diff --git a/src/content/fr/about/-index.md b/src/content/french/about/-index.md similarity index 100% rename from src/content/fr/about/-index.md rename to src/content/french/about/-index.md diff --git a/src/content/fr/authors/-index.md b/src/content/french/authors/-index.md similarity index 100% rename from src/content/fr/authors/-index.md rename to src/content/french/authors/-index.md diff --git a/src/content/fr/authors/john-doe.md b/src/content/french/authors/john-doe.md similarity index 100% rename from src/content/fr/authors/john-doe.md rename to src/content/french/authors/john-doe.md diff --git a/src/content/fr/authors/sam-wilson.md b/src/content/french/authors/sam-wilson.md similarity index 100% rename from src/content/fr/authors/sam-wilson.md rename to src/content/french/authors/sam-wilson.md diff --git a/src/content/fr/authors/william-jacob.md b/src/content/french/authors/william-jacob.md similarity index 100% rename from src/content/fr/authors/william-jacob.md rename to src/content/french/authors/william-jacob.md diff --git a/src/content/fr/blog/-index.md b/src/content/french/blog/-index.md similarity index 100% rename from src/content/fr/blog/-index.md rename to src/content/french/blog/-index.md diff --git a/src/content/fr/blog/post-1.md b/src/content/french/blog/post-1.md similarity index 100% rename from src/content/fr/blog/post-1.md rename to src/content/french/blog/post-1.md diff --git a/src/content/fr/blog/post-2.md b/src/content/french/blog/post-2.md similarity index 100% rename from src/content/fr/blog/post-2.md rename to src/content/french/blog/post-2.md diff --git a/src/content/fr/blog/post-3.md b/src/content/french/blog/post-3.md similarity index 100% rename from src/content/fr/blog/post-3.md rename to src/content/french/blog/post-3.md diff --git a/src/content/fr/blog/post-4.md b/src/content/french/blog/post-4.md similarity index 100% rename from src/content/fr/blog/post-4.md rename to src/content/french/blog/post-4.md diff --git a/src/content/fr/contact/-index.md b/src/content/french/contact/-index.md similarity index 100% rename from src/content/fr/contact/-index.md rename to src/content/french/contact/-index.md diff --git a/src/content/fr/homepage/-index.md b/src/content/french/homepage/-index.md similarity index 100% rename from src/content/fr/homepage/-index.md rename to src/content/french/homepage/-index.md diff --git a/src/content/fr/pages/elements.mdx b/src/content/french/pages/elements.mdx similarity index 100% rename from src/content/fr/pages/elements.mdx rename to src/content/french/pages/elements.mdx diff --git a/src/content/fr/pages/privacy-policy.md b/src/content/french/pages/privacy-policy.md similarity index 100% rename from src/content/fr/pages/privacy-policy.md rename to src/content/french/pages/privacy-policy.md diff --git a/src/content/fr/sections/call-to-action.md b/src/content/french/sections/call-to-action.md similarity index 100% rename from src/content/fr/sections/call-to-action.md rename to src/content/french/sections/call-to-action.md diff --git a/src/content/fr/sections/testimonial.md b/src/content/french/sections/testimonial.md similarity index 100% rename from src/content/fr/sections/testimonial.md rename to src/content/french/sections/testimonial.md diff --git a/src/lib/contentParser.astro b/src/lib/contentParser.astro index ce22d49..47d2494 100644 --- a/src/lib/contentParser.astro +++ b/src/lib/contentParser.astro @@ -15,9 +15,35 @@ export const getSinglePage = async ( const { default_language } = config.settings; const langCollection: keyof ContentEntryMap = lang as keyof ContentEntryMap; + const languages = [ + { + languageName: "En", + languageCode: "en", + contentDir: "english", + weight: 1, + }, + { + languageName: "Fr", + languageCode: "fr", + contentDir: "french", + weight: 2, + }, + ]; + + const language = languages.find( + (l) => + l.languageCode === langCollection || l.languageCode === default_language + ); + + if (!language) { + throw new Error("Language not found"); + } + + const { contentDir } = language; + // Explicitly define the type of pages to CollectionEntry[] const pages: CollectionEntry[] = (await getCollection( - langCollection || default_language, + contentDir as any, ({ id }: any) => { return id.startsWith(collectionName) && !id.endsWith("-index.md"); } @@ -33,10 +59,35 @@ export const getListPage = async ( lang: keyof ContentEntryMap ): Promise[]> => { const { default_language } = config.settings; - const langCollection: keyof ContentEntryMap = lang as keyof ContentEntryMap; + + const languages = [ + { + languageName: "En", + languageCode: "en", + contentDir: "english", + weight: 1, + }, + { + languageName: "Fr", + languageCode: "fr", + contentDir: "french", + weight: 2, + }, + ]; + + const language = languages.find( + (l) => l.languageCode == lang || l.languageCode == default_language + ); + + if (!language) { + throw new Error("Language not found"); + } + + const { contentDir } = language; + // Fetch the collection based on the language const pages: CollectionEntry[] = (await getCollection( - langCollection || default_language, + contentDir as any, ({ id }: any) => { return id.startsWith(collectionName); } diff --git a/src/lib/utils/i18nUtils.ts b/src/lib/utils/i18nUtils.ts index 16a6ff4..2d30063 100644 --- a/src/lib/utils/i18nUtils.ts +++ b/src/lib/utils/i18nUtils.ts @@ -5,20 +5,17 @@ import fs from "fs"; import path from "path"; const { default_language } = config.settings; -const menusFolderPath = "./src/config"; +const locales: { [key: string]: any } = {}; -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")); - return { [localeName]: localeData }; - }) - .reduce((accumulator, locale) => { - return { ...accumulator, ...locale }; - }, {}); +// 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); @@ -47,7 +44,6 @@ export const getTranslations = async (lang: string) => { export const supportedLang = [""].concat( languagesJSON.map((lang) => lang.languageCode), ); - // Function to construct the URL based on trailing_slash value export const constructUrl = ( url: string, diff --git a/src/lib/utils/loadMenu.ts b/src/lib/utils/loadMenu.ts index 302750a..6ed450a 100644 --- a/src/lib/utils/loadMenu.ts +++ b/src/lib/utils/loadMenu.ts @@ -1,5 +1,6 @@ -import fs from 'fs'; -import path from 'path'; +import fs from "fs"; +import path from "path"; +import languages from "@/config/language.json"; export interface ChildNavigationLink { name: string; @@ -19,7 +20,22 @@ interface Menu { } export function loadMenu(lang: string): Menu { - const menuPath: string = path.join(process.cwd(), 'src', 'config', `menu.${lang}.json`); - const menuData: string = fs.readFileSync(menuPath, 'utf-8'); + // Find the language object based on the language code + const language = languages.find((l) => l.languageCode === lang); + + if (!language) { + throw new Error( + `Language '${lang}' not found in the list of supported languages.`, + ); + } + + // Load menu data based on the language code + const menuPath: string = path.join( + process.cwd(), + "src", + "config", + `menu.${language.languageCode}.json`, + ); + const menuData: string = fs.readFileSync(menuPath, "utf-8"); return JSON.parse(menuData) as Menu; } diff --git a/src/pages/[...lang]/about.astro b/src/pages/[...lang]/about.astro index 72df691..88c810d 100644 --- a/src/pages/[...lang]/about.astro +++ b/src/pages/[...lang]/about.astro @@ -17,9 +17,11 @@ export function getStaticPaths() { const { lang } = Astro.params; const about: any = await getListPage( "about" as ContentCollectionKey, - lang as keyof ContentEntryMap, + lang as keyof ContentEntryMap ); +console.log(about); + const { Content } = await about[0].render(); const { title, description, meta_title, image } = about[0].data; ---