next-i18next
Version:
The easiest way to translate your NextJs apps.
74 lines (73 loc) • 3.72 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_config_defaultConfig = require("./defaultConfig.cjs");
const require_utils = require("../utils.cjs");
//#region src/pagesRouter/config/createConfig.ts
const deepMergeObjects = ["backend", "detection"];
const createConfig = (userConfig, options = {}) => {
if (typeof userConfig?.lng !== "string") throw new Error("config.lng was not passed into createConfig");
const { i18n: userI18n, ...userConfigStripped } = userConfig;
const { i18n: defaultI18n, ...defaultConfigStripped } = require_config_defaultConfig.defaultConfig;
const combinedConfig = {
...defaultConfigStripped,
...userConfigStripped,
...defaultI18n,
...userI18n
};
const { defaultNS, lng, localeExtension, localePath, nonExplicitSupportedLngs } = combinedConfig;
const locales = combinedConfig.locales.filter((l) => l !== "default");
/**
* Skips translation file resolution while in cimode
* https://github.com/i18next/next-i18next/pull/851#discussion_r503113620
*/
if (lng === "cimode") return combinedConfig;
if (typeof combinedConfig.fallbackLng === "undefined") {
combinedConfig.fallbackLng = combinedConfig.defaultLocale;
if (combinedConfig.fallbackLng === "default") [combinedConfig.fallbackLng] = locales;
}
const userPrefix = userConfig?.interpolation?.prefix;
const userSuffix = userConfig?.interpolation?.suffix;
const prefix = userPrefix ?? "{{";
const suffix = userSuffix ?? "}}";
if (typeof userConfig?.localeStructure !== "string" && (userPrefix || userSuffix)) combinedConfig.localeStructure = `${prefix}lng${suffix}/${prefix}ns${suffix}`;
const { fallbackLng, localeStructure } = combinedConfig;
if (nonExplicitSupportedLngs) {
const createFallbackObject = (acc, l) => {
const [locale] = l.split("-");
acc[l] = [locale];
return acc;
};
if (typeof fallbackLng === "string") combinedConfig.fallbackLng = combinedConfig.locales.filter((l) => l.includes("-")).reduce(createFallbackObject, { default: [fallbackLng] });
else if (Array.isArray(fallbackLng)) combinedConfig.fallbackLng = combinedConfig.locales.filter((l) => l.includes("-")).reduce(createFallbackObject, { default: fallbackLng });
else if (typeof fallbackLng === "object") combinedConfig.fallbackLng = Object.entries(combinedConfig.fallbackLng).reduce((acc, [l, f]) => {
acc[l] = l.includes("-") ? require_utils.unique([l.split("-")[0], ...f]) : f;
return acc;
}, fallbackLng);
else if (typeof fallbackLng === "function") throw new Error("If nonExplicitSupportedLngs is true, no functions are allowed for fallbackLng");
}
const hasCustomBackend = userConfig?.use?.filter(Boolean).some((b) => b.type === "backend");
if (!process.browser && typeof window === "undefined") {
if (options.applyServerSideConfig) options.applyServerSideConfig(combinedConfig, userConfig);
} else {
if (!hasCustomBackend) {
if (typeof localePath === "string") combinedConfig.backend = {
addPath: `${localePath}/${localeStructure}.missing.${localeExtension}`,
loadPath: `${localePath}/${localeStructure}.${localeExtension}`
};
else if (typeof localePath === "function") combinedConfig.backend = {
addPath: (locale, namespace) => localePath(locale, namespace, true),
loadPath: (locale, namespace) => localePath(locale, namespace, false)
};
}
if (typeof combinedConfig.ns !== "string" && !Array.isArray(combinedConfig.ns)) combinedConfig.ns = [defaultNS];
}
deepMergeObjects.forEach((obj) => {
if (userConfig[obj]) combinedConfig[obj] = {
...combinedConfig[obj],
...userConfig[obj]
};
});
return combinedConfig;
};
//#endregion
exports.createConfig = createConfig;
//# sourceMappingURL=createConfig.cjs.map