UNPKG

next-i18next

Version:

The easiest way to translate your NextJs apps.

73 lines (72 loc) 3.57 kB
import { defaultConfig } from "./defaultConfig.mjs"; import { unique } from "../utils.mjs"; //#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 } = 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("-") ? 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 export { createConfig }; //# sourceMappingURL=createConfig.mjs.map