next-i18next
Version:
The easiest way to translate your NextJs apps.
74 lines (73 loc) • 4.06 kB
JavaScript
import { getFallbackForLng, unique } from "../utils.mjs";
import fs from "fs";
import path from "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.join(localePath, defaultFile);
const defaultNSExists = fs.existsSync(defaultNSPath);
const defaultFallbackNSExists = getFallbackForLng(lng, fallbackLng).some((f) => {
const fallbackFile = defaultFile.replace(lng, f);
return fs.existsSync(path.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.existsSync(defaultNSPath);
const defaultFallbackNSExists = getFallbackForLng(lng, fallbackLng).some((f) => {
const fallbackNSPath = localePath(f, defaultNS, false);
return fs.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.resolve(process.cwd(), `${localePath}/${localeStructure}.missing.${localeExtension}`),
loadPath: path.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.existsSync(p)) return ret;
fs.readdirSync(p).forEach((file) => {
const joinedP = path.join(p, file);
if (fs.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.resolve(process.cwd(), `${localePath}/${locale}`)));
const allNamespaces = [];
for (const localNamespaces of namespacesByLocale) allNamespaces.push(...localNamespaces);
return 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(unique([lng, ...getFallbackForLng(lng, fallbackLng)]));
}
};
//#endregion
export { applyServerSideConfig };
//# sourceMappingURL=serverSideConfig.mjs.map