UNPKG

@tanosugi/better-auth-utils

Version:

Better Auth Utils

28 lines (27 loc) 1.1 kB
import { uiLocalesDict } from "./locales/ui-shared"; const isValidLocale = (locale) => Object.prototype.hasOwnProperty.call(uiLocalesDict, locale); export function getUiLocaleDict(locale) { return isValidLocale(locale) ? uiLocalesDict[locale] : uiLocalesDict.en; } // 使い方例: // Next.jsのAPI Routeでロケール辞書を返すエンドポイントを作成する場合: // // 例: app/api/ui-locale/route.ts // // import { createUiLocaleHandler } from "./api"; // // export const GET = createUiLocaleHandler({ cacheMaxAge: 86400 }); // // これにより、/api/ui-locale?locale=ja のようなリクエストで対応するロケール辞書がJSONで返ります。 export function createUiLocaleHandler({ cacheMaxAge }) { return async function GET(req) { const { searchParams } = new URL(req.url); const locale = searchParams.get("locale") || "en"; const dict = getUiLocaleDict(locale); return Response.json(dict, { headers: { "Cache-Control": `public, max-age=${cacheMaxAge}, immutable`, }, }); }; }