diff --git a/src/lib/i18n/loadMessages.ts b/src/lib/i18n/loadMessages.ts index 8b4b0033eda..c93d9cfc569 100644 --- a/src/lib/i18n/loadMessages.ts +++ b/src/lib/i18n/loadMessages.ts @@ -1,14 +1,19 @@ import fs from "fs" +import { readFile } from "fs/promises" import path from "path" +interface IntlMessages { + [key: string]: string | IntlMessages +} + function getNamespaces(localePath: string): string[] { return fs - .readdirSync(localePath) - .filter((file) => file.endsWith(".json")) - .map((file) => file.replace(".json", "")) + .readdirSync(localePath, { withFileTypes: true }) + .filter((entry) => entry.isFile() && entry.name.endsWith(".json")) + .map((entry) => entry.name.replace(".json", "")) } -const messagesCache: Record> = {} +const messagesCache: Record> = {} export async function loadMessages(locale: string) { if (messagesCache[locale]) { @@ -16,15 +21,19 @@ export async function loadMessages(locale: string) { } const intlPath = path.join(process.cwd(), "src/intl") - const messages: Record = {} - const localePath = path.join(intlPath, locale) + const messages: Record = {} + if (fs.statSync(localePath).isDirectory()) { const namespaces = getNamespaces(localePath) - for (const ns of namespaces) { - messages[ns] = (await import(`../../intl/${locale}/${ns}.json`)).default - } + await Promise.all( + namespaces.map(async (ns) => { + const filePath = path.join(localePath, `${ns}.json`) + const content = await readFile(filePath, "utf-8") + messages[ns] = JSON.parse(content) as IntlMessages + }) + ) } messagesCache[locale] = messages diff --git a/src/lib/md/import.ts b/src/lib/md/import.ts index e545953d4f6..143f781a6fd 100644 --- a/src/lib/md/import.ts +++ b/src/lib/md/import.ts @@ -1,32 +1,32 @@ +import { readFile } from "fs/promises" +import path from "path" + import { DEFAULT_LOCALE } from "../constants" export const importMd = async (locale: string, slug: string) => { - let markdown = "" + const contentPath = path.join(process.cwd(), "public/content") if (locale === DEFAULT_LOCALE) { - markdown = (await import(`../../../public/content/${slug}/index.md`)) - .default - } else { - try { - markdown = ( - await import( - `../../../public/content/translations/${locale}/${slug}/index.md` - ) - ).default - } catch (error) { - const markdown = ( - await import(`../../../public/content/${slug}/index.md`) - ).default - - return { - markdown, - isTranslated: false, - } - } + const filePath = path.join(contentPath, slug, "index.md") + const markdown = await readFile(filePath, "utf-8") + return { markdown, isTranslated: true } } - return { - markdown, - isTranslated: true, + // Try translated version first + const translatedPath = path.join( + contentPath, + "translations", + locale, + slug, + "index.md" + ) + try { + const markdown = await readFile(translatedPath, "utf-8") + return { markdown, isTranslated: true } + } catch { + // Fall back to English + const defaultPath = path.join(contentPath, slug, "index.md") + const markdown = await readFile(defaultPath, "utf-8") + return { markdown, isTranslated: false } } }