Files
petvity/i18n/request.ts
T
GandClaude Opus 5 c65ac4b7d7 chore: add a formatter (#71)
* chore: add a formatter, matching the style this repo already writes

singleQuote=false was chosen by counting this repo's own
imports, not by fleet decree. The fleet is genuinely split and the two repos
that already had a .prettierrc disagreed with each other, so there was no
standard to restore. Quote style does not cross repo boundaries; having a
gate does.

Markdown is ignored for now — prettier rewraps prose, which would bury the
real diff.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* style: format with prettier (252 files)

Mechanical. No behaviour change. This SHA is listed in
.git-blame-ignore-revs so `git blame` skips it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* chore: teach git blame to skip the reformat

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-29 10:05:01 +00:00

44 lines
1.5 KiB
TypeScript

import { getRequestConfig } from "next-intl/server";
import { routing } from "./routing";
import { APP } from "@/lib/config/app";
/**
* Substitute the {app} placeholder once, at message-load time. 47 message keys
* reference {app}; requiring every t() call site to pass `{ app: APP.name }`
* shipped literal "{app}" into rendered copy whenever one forgot (next-intl v4
* removed defaultTranslationValues). Translators keep the placeholder; the
* runtime owns the value — APP.name stays the single source of truth.
*/
function injectAppName(node: unknown): unknown {
if (typeof node === "string") return node.replaceAll("{app}", APP.name);
if (Array.isArray(node)) return node.map(injectAppName);
if (node && typeof node === "object") {
return Object.fromEntries(Object.entries(node).map(([k, v]) => [k, injectAppName(v)]));
}
return node;
}
const messageCache = new Map<string, Record<string, unknown>>();
async function loadMessages(locale: string): Promise<Record<string, unknown>> {
const cached = messageCache.get(locale);
if (cached) return cached;
const raw = (await import(`../messages/${locale}.json`)).default;
const processed = injectAppName(raw) as Record<string, unknown>;
messageCache.set(locale, processed);
return processed;
}
export default getRequestConfig(async ({ requestLocale }) => {
let locale = await requestLocale;
if (!locale || !routing.locales.includes(locale as "en")) {
locale = routing.defaultLocale;
}
return {
locale,
messages: await loadMessages(locale),
};
});