Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions src/lib/i18n/loadMessages.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
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<string, Record<string, string>> = {}
const messagesCache: Record<string, Record<string, IntlMessages>> = {}

export async function loadMessages(locale: string) {
if (messagesCache[locale]) {
return messagesCache[locale]
}

const intlPath = path.join(process.cwd(), "src/intl")
const messages: Record<string, string> = {}

const localePath = path.join(intlPath, locale)
const messages: Record<string, IntlMessages> = {}

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
Expand Down
46 changes: 23 additions & 23 deletions src/lib/md/import.ts
Original file line number Diff line number Diff line change
@@ -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 }
}
}