UNPKG

next-i18next

Version:

The easiest way to translate your NextJs apps.

78 lines (77 loc) 4.46 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const require_runtime = require("../_virtual/_rolldown/runtime.cjs"); const require_utils = require("../utils.cjs"); let fs = require("fs"); fs = require_runtime.__toESM(fs); let path = require("path"); path = require_runtime.__toESM(path); //#region src/pagesRouter/config/serverSideConfig.ts const applyServerSideConfig = (combinedConfig, userConfig) => { const { defaultNS, lng, localeExtension, localePath, localeStructure, fallbackLng } = combinedConfig; const userPrefix = userConfig?.interpolation?.prefix; const userSuffix = userConfig?.interpolation?.suffix; const prefix = userPrefix ?? "{{"; const suffix = userSuffix ?? "}}"; combinedConfig.preload = combinedConfig.locales.filter((l) => l !== "default"); if (userConfig?.use?.filter(Boolean).some((b) => b.type === "backend")) return; if (typeof defaultNS === "string" && typeof lng !== "undefined") { if (typeof localePath === "string") { const defaultFile = `/${localeStructure.replace(`${prefix}lng${suffix}`, lng).replace(`${prefix}ns${suffix}`, defaultNS)}.${localeExtension}`; const defaultNSPath = path.default.join(localePath, defaultFile); const defaultNSExists = fs.default.existsSync(defaultNSPath); const defaultFallbackNSExists = require_utils.getFallbackForLng(lng, fallbackLng).some((f) => { const fallbackFile = defaultFile.replace(lng, f); return fs.default.existsSync(path.default.join(localePath, fallbackFile)); }); if (!defaultNSExists && !defaultFallbackNSExists && process.env.NODE_ENV !== "production") throw new Error(`Default namespace not found at ${defaultNSPath}`); } else if (typeof localePath === "function") { const defaultNSPath = localePath(lng, defaultNS, false); const defaultNSExists = fs.default.existsSync(defaultNSPath); const defaultFallbackNSExists = require_utils.getFallbackForLng(lng, fallbackLng).some((f) => { const fallbackNSPath = localePath(f, defaultNS, false); return fs.default.existsSync(fallbackNSPath); }); if (!defaultNSExists && !defaultFallbackNSExists && process.env.NODE_ENV !== "production") throw new Error(`Default namespace not found at ${defaultNSPath}`); } } if (typeof localePath === "string") combinedConfig.backend = { addPath: path.default.resolve(process.cwd(), `${localePath}/${localeStructure}.missing.${localeExtension}`), loadPath: path.default.resolve(process.cwd(), `${localePath}/${localeStructure}.${localeExtension}`) }; else if (typeof localePath === "function") combinedConfig.backend = { addPath: (locale, namespace) => localePath(locale, namespace, true), loadPath: (locale, namespace) => localePath(locale, namespace, false) }; else if (localePath) throw new Error(`Unsupported localePath type: ${typeof localePath}`); if (!combinedConfig.ns && typeof lng !== "undefined") { if (typeof localePath === "function") throw new Error("Must provide all namespaces in ns option if using a function as localePath"); const getNamespaces = (loc) => { const getLocaleNamespaces = (p) => { let ret = []; if (!fs.default.existsSync(p)) return ret; fs.default.readdirSync(p).forEach((file) => { const joinedP = path.default.join(p, file); if (fs.default.statSync(joinedP).isDirectory()) { const subRet = getLocaleNamespaces(joinedP).map((n) => `${file}/${n}`); ret = ret.concat(subRet); return; } ret.push(file.replace(`.${localeExtension}`, "")); }); return ret; }; let namespacesByLocale; const r = combinedConfig.resources; if (!localePath && r) namespacesByLocale = loc.map((locale) => Object.keys(r[locale])); else namespacesByLocale = loc.map((locale) => getLocaleNamespaces(path.default.resolve(process.cwd(), `${localePath}/${locale}`))); const allNamespaces = []; for (const localNamespaces of namespacesByLocale) allNamespaces.push(...localNamespaces); return require_utils.unique(allNamespaces); }; if (localeStructure.indexOf(`${prefix}lng${suffix}`) > localeStructure.indexOf(`${prefix}ns${suffix}`)) throw new Error("Must provide all namespaces in ns option if using a localeStructure that is not namespace-listable like lng/ns"); combinedConfig.ns = getNamespaces(require_utils.unique([lng, ...require_utils.getFallbackForLng(lng, fallbackLng)])); } }; //#endregion exports.applyServerSideConfig = applyServerSideConfig; //# sourceMappingURL=serverSideConfig.cjs.map