UNPKG

next-i18next

Version:

The easiest way to translate your NextJs apps.

1 lines 8.12 kB
{"version":3,"file":"serverSideConfig.mjs","names":[],"sources":["../../../src/pagesRouter/config/serverSideConfig.ts"],"sourcesContent":["import fs from 'fs'\nimport path from 'path'\n\nimport { InternalConfig, UserConfig } from '../types'\nimport { getFallbackForLng, unique } from '../utils'\nimport { Module } from 'i18next'\n\n// Server-only logic extracted from createConfig so that the bundler never\n// traces `fs`/`path` from the client side. createConfig invokes this via the\n// `applyServerSideConfig` hook, which is wired up only by callers that are\n// guaranteed to run on the server (e.g. serverSideTranslations).\nexport const applyServerSideConfig = (\n combinedConfig: any,\n userConfig: UserConfig\n): void => {\n const {\n defaultNS,\n lng,\n localeExtension,\n localePath,\n localeStructure,\n fallbackLng,\n } = combinedConfig\n\n const userPrefix = userConfig?.interpolation?.prefix\n const userSuffix = userConfig?.interpolation?.suffix\n const prefix = userPrefix ?? '{{'\n const suffix = userSuffix ?? '}}'\n\n const locales: string[] = combinedConfig.locales.filter(\n (l: string) => l !== 'default'\n )\n\n combinedConfig.preload = locales\n\n const hasCustomBackend = userConfig?.use?.filter(Boolean).some(\n (b: Module) => b.type === 'backend'\n )\n if (hasCustomBackend) return\n\n //\n // Validate defaultNS\n // https://github.com/i18next/next-i18next/issues/358\n //\n if (typeof defaultNS === 'string' && typeof lng !== 'undefined') {\n if (typeof localePath === 'string') {\n const defaultLocaleStructure = localeStructure\n .replace(`${prefix}lng${suffix}`, lng)\n .replace(`${prefix}ns${suffix}`, defaultNS)\n const defaultFile = `/${defaultLocaleStructure}.${localeExtension}`\n const defaultNSPath = path.join(localePath, defaultFile)\n const defaultNSExists = fs.existsSync(defaultNSPath)\n const fallback = getFallbackForLng(lng, fallbackLng)\n const defaultFallbackNSExists = fallback.some(f => {\n const fallbackFile = defaultFile.replace(lng, f)\n return fs.existsSync(path.join(localePath, fallbackFile))\n })\n if (\n !defaultNSExists &&\n !defaultFallbackNSExists &&\n process.env.NODE_ENV !== 'production'\n ) {\n throw new Error(`Default namespace not found at ${defaultNSPath}`)\n }\n } else if (typeof localePath === 'function') {\n const defaultNSPath = localePath(lng, defaultNS, false)\n const defaultNSExists = fs.existsSync(defaultNSPath)\n const fallback = getFallbackForLng(lng, fallbackLng)\n const defaultFallbackNSExists = fallback.some(f => {\n const fallbackNSPath = localePath(f, defaultNS, false)\n return fs.existsSync(fallbackNSPath)\n })\n if (\n !defaultNSExists &&\n !defaultFallbackNSExists &&\n process.env.NODE_ENV !== 'production'\n ) {\n throw new Error(`Default namespace not found at ${defaultNSPath}`)\n }\n }\n }\n\n //\n // Set server side backend\n //\n if (typeof localePath === 'string') {\n combinedConfig.backend = {\n addPath: path.resolve(\n process.cwd(),\n `${localePath}/${localeStructure}.missing.${localeExtension}`\n ),\n loadPath: path.resolve(\n process.cwd(),\n `${localePath}/${localeStructure}.${localeExtension}`\n ),\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 } else if (localePath) {\n throw new Error(`Unsupported localePath type: ${typeof localePath}`)\n }\n\n //\n // Set server side preload (namespaces)\n //\n if (!combinedConfig.ns && typeof lng !== 'undefined') {\n if (typeof localePath === 'function') {\n throw new Error(\n 'Must provide all namespaces in ns option if using a function as localePath'\n )\n }\n\n const getNamespaces = (loc: string[]): string[] => {\n const getLocaleNamespaces = (p: string): string[] => {\n let ret: string[] = []\n if (!fs.existsSync(p)) return ret\n fs.readdirSync(p).forEach((file: string) => {\n const joinedP = path.join(p, file)\n if (fs.statSync(joinedP).isDirectory()) {\n const subRet = getLocaleNamespaces(joinedP).map(\n n => `${file}/${n}`\n )\n ret = ret.concat(subRet)\n return\n }\n ret.push(file.replace(`.${localeExtension}`, ''))\n })\n return ret\n }\n\n let namespacesByLocale: string[][]\n const r = combinedConfig.resources\n if (!localePath && r) {\n namespacesByLocale = loc.map(locale => Object.keys(r[locale]))\n } else {\n namespacesByLocale = loc.map(locale =>\n getLocaleNamespaces(\n path.resolve(process.cwd(), `${localePath}/${locale}`)\n )\n )\n }\n\n const allNamespaces: string[] = []\n for (const localNamespaces of namespacesByLocale) {\n allNamespaces.push(...localNamespaces)\n }\n\n return unique(allNamespaces)\n }\n\n if (\n localeStructure.indexOf(`${prefix}lng${suffix}`) >\n localeStructure.indexOf(`${prefix}ns${suffix}`)\n ) {\n throw new Error(\n 'Must provide all namespaces in ns option if using a localeStructure that is not namespace-listable like lng/ns'\n )\n }\n\n combinedConfig.ns = getNamespaces(\n unique([lng, ...getFallbackForLng(lng, fallbackLng)])\n ) as InternalConfig['ns']\n }\n}\n"],"mappings":";;;;AAWA,MAAa,yBACX,gBACA,eACS;CACT,MAAM,EACJ,WACA,KACA,iBACA,YACA,iBACA,gBACE;CAEJ,MAAM,aAAa,YAAY,eAAe;CAC9C,MAAM,aAAa,YAAY,eAAe;CAC9C,MAAM,SAAS,cAAc;CAC7B,MAAM,SAAS,cAAc;AAM7B,gBAAe,UAJW,eAAe,QAAQ,QAC9C,MAAc,MAAM,UACtB;AAOD,KAHyB,YAAY,KAAK,OAAO,QAAQ,CAAC,MACvD,MAAc,EAAE,SAAS,UAC3B,CACqB;AAMtB,KAAI,OAAO,cAAc,YAAY,OAAO,QAAQ;MAC9C,OAAO,eAAe,UAAU;GAIlC,MAAM,cAAc,IAHW,gBAC5B,QAAQ,GAAG,OAAO,KAAK,UAAU,IAAI,CACrC,QAAQ,GAAG,OAAO,IAAI,UAAU,UAAU,CACE,GAAG;GAClD,MAAM,gBAAgB,KAAK,KAAK,YAAY,YAAY;GACxD,MAAM,kBAAkB,GAAG,WAAW,cAAc;GAEpD,MAAM,0BADW,kBAAkB,KAAK,YAAY,CACX,MAAK,MAAK;IACjD,MAAM,eAAe,YAAY,QAAQ,KAAK,EAAE;AAChD,WAAO,GAAG,WAAW,KAAK,KAAK,YAAY,aAAa,CAAC;KACzD;AACF,OACE,CAAC,mBACD,CAAC,2B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