UNPKG

next-i18next

Version:

The easiest way to translate your NextJs apps.

1 lines 7.19 kB
{"version":3,"file":"createConfig.cjs","names":["defaultConfig","unique"],"sources":["../../../src/pagesRouter/config/createConfig.ts"],"sourcesContent":["import { defaultConfig } from './defaultConfig'\nimport { InternalConfig, UserConfig } from '../types'\nimport { unique } from '../utils'\nimport { FallbackLngObjList, Module } from 'i18next'\n\nconst deepMergeObjects = ['backend', 'detection'] as (keyof Pick<\n UserConfig,\n 'backend' | 'detection'\n>)[]\n\ntype CreateConfigOptions = {\n // Hook called only on the server side. Kept as an injected dependency so\n // `createConfig` itself never references Node built-ins (`fs`, `path`,\n // `module`) and stays safe to bundle for the browser. Wired up by\n // `serverSideTranslations`, which is server-only.\n applyServerSideConfig?: (\n combinedConfig: any,\n userConfig: UserConfig\n ) => void\n}\n\nexport const createConfig = (\n userConfig: UserConfig,\n options: CreateConfigOptions = {}\n): InternalConfig => {\n if (typeof userConfig?.lng !== 'string') {\n throw new Error('config.lng was not passed into createConfig')\n }\n\n //\n // Initial merge of default and user-provided config\n //\n const { i18n: userI18n, ...userConfigStripped } = userConfig\n const { i18n: defaultI18n, ...defaultConfigStripped } =\n defaultConfig\n const combinedConfig = {\n ...defaultConfigStripped,\n ...userConfigStripped,\n ...defaultI18n,\n ...userI18n,\n }\n\n const {\n defaultNS,\n lng,\n localeExtension,\n localePath,\n nonExplicitSupportedLngs,\n } = combinedConfig\n\n const locales = combinedConfig.locales.filter((l: string) => l !== 'default')\n\n /**\n * Skips translation file resolution while in cimode\n * https://github.com/i18next/next-i18next/pull/851#discussion_r503113620\n */\n if (lng === 'cimode') {\n return combinedConfig as unknown as InternalConfig\n }\n\n if (typeof combinedConfig.fallbackLng === 'undefined') {\n combinedConfig.fallbackLng = combinedConfig.defaultLocale\n if (combinedConfig.fallbackLng === 'default') { [combinedConfig.fallbackLng] = locales }\n }\n\n const userPrefix = userConfig?.interpolation?.prefix\n const userSuffix = userConfig?.interpolation?.suffix\n const prefix = userPrefix ?? '{{'\n const suffix = userSuffix ?? '}}'\n if (\n typeof userConfig?.localeStructure !== 'string' &&\n (userPrefix || userSuffix)\n ) {\n combinedConfig.localeStructure = `${prefix}lng${suffix}/${prefix}ns${suffix}`\n }\n\n const { fallbackLng, localeStructure } = combinedConfig\n\n if (nonExplicitSupportedLngs) {\n const createFallbackObject = (\n acc: FallbackLngObjList,\n l: string\n ) => {\n const [locale] = l.split('-')\n acc[l] = [locale]\n return acc\n }\n\n if (typeof fallbackLng === 'string') {\n combinedConfig.fallbackLng = combinedConfig.locales\n .filter((l: string) => l.includes('-'))\n .reduce(createFallbackObject, { default: [fallbackLng] })\n } else if (Array.isArray(fallbackLng)) {\n combinedConfig.fallbackLng = combinedConfig.locales\n .filter((l: string) => l.includes('-'))\n .reduce(createFallbackObject, { default: fallbackLng })\n } else if (typeof fallbackLng === 'object') {\n combinedConfig.fallbackLng = Object.entries(\n combinedConfig.fallbackLng\n ).reduce<FallbackLngObjList>((acc, [l, f]: [string, any]) => {\n acc[l] = l.includes('-')\n ? unique([l.split('-')[0], ...f])\n : f\n return acc\n }, fallbackLng as FallbackLngObjList)\n } else if (typeof fallbackLng === 'function') {\n throw new Error(\n 'If nonExplicitSupportedLngs is true, no functions are allowed for fallbackLng'\n )\n }\n }\n\n const hasCustomBackend = userConfig?.use?.filter(Boolean).some(\n (b: Module) => b.type === 'backend'\n )\n if (!process.browser && typeof window === 'undefined') {\n if (options.applyServerSideConfig) {\n options.applyServerSideConfig(combinedConfig, userConfig)\n }\n } else {\n //\n // Set client side backend, if there is no custom backend\n //\n if (!hasCustomBackend) {\n if (typeof localePath === 'string') {\n combinedConfig.backend = {\n addPath: `${localePath}/${localeStructure}.missing.${localeExtension}`,\n loadPath: `${localePath}/${localeStructure}.${localeExtension}`,\n }\n } else if (typeof localePath === 'function') {\n combinedConfig.backend = {\n addPath: (locale: string, namespace: string) =>\n localePath(locale, namespace, true),\n loadPath: (locale: string, namespace: string) =>\n localePath(locale, namespace, false),\n }\n }\n }\n\n if (\n typeof combinedConfig.ns !== 'string' &&\n !Array.isArray(combinedConfig.ns)\n ) {\n combinedConfig.ns = [defaultNS as string]\n }\n }\n\n //\n // Deep merge with overwrite - goes last\n //\n deepMergeObjects.forEach(obj => {\n if (userConfig[obj]) {\n combinedConfig[obj] = {\n ...combinedConfig[obj],\n ...userConfig[obj],\n }\n }\n })\n\n return combinedConfig as unknown as InternalConfig\n}\n"],"mappings":";;;;AAKA,MAAM,mBAAmB,CAAC,WAAW,YAAY;AAgBjD,MAAa,gBACX,YACA,UAA+B,EAAE,KACd;AACnB,KAAI,OAAO,YAAY,QAAQ,SAC7B,OAAM,IAAI,MAAM,8CAA8C;CAMhE,MAAM,EAAE,MAAM,UAAU,GAAG,uBAAuB;CAClD,MAAM,EAAE,MAAM,aAAa,GAAG,0BAC5BA,6BAAAA;CACF,MAAM,iBAAiB;EACrB,GAAG;EACH,GAAG;EACH,GAAG;EACH,GAAG;EACJ;CAED,MAAM,EACJ,WACA,KACA,iBACA,YACA,6BACE;CAEJ,MAAM,UAAU,eAAe,QAAQ,QAAQ,MAAc,MAAM,UAAU;;;;;AAM7E,KAAI,QAAQ,SACV,QAAO;AAGT,KAAI,OAAO,eAAe,gBAAgB,aAAa;AACrD,iBAAe,cAAc,eAAe;AAC5C,MAAI,eAAe,gBAAgB,UAAa,EAAC,eAAe,eAAe;;CAGjF,MAAM,aAAa,YAAY,eAAe;CAC9C,MAAM,aAAa,YAAY,eAAe;CAC9C,MAAM,SAAS,cAAc;CAC7B,MAAM,SAAS,cAAc;AAC7B,KACE,OAAO,YAAY,oBAAoB,aACtC,cAAc,YAEf,gBAAe,kBAAkB,GAAG,OAAO,KAAK,OAAO,GAAG,OAAO,IAAI;CAGvE,MAAM,EAAE,aAAa,oBAAoB;AAEzC,KAAI,0BAA0B;EAC5B,MAAM,wBACJ,KACA,MACG;GACH,MAAM,CAAC,UAAU,EAAE,MAAM,IAAI;AAC7B,OAAI,KAAK,CAAC,OAAO;AACjB,UAAO;;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