UNPKG

@webpro/next-translate-plugin

Version:

Tiny and powerful i18n plugin to translate your Next.js pages.

62 lines (61 loc) 5.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var utils_1 = require("./utils"); var validPages = [ '/page', '/layout', '/error', '/loading', '/not-found', '/global-error', ]; var validPagesRegex = new RegExp("(".concat(validPages.join('|'), ")$")); function templateAppDir(pagePkg, _a) { var _b = _a === void 0 ? {} : _a, _c = _b.code, code = _c === void 0 ? '' : _c, _d = _b.pageNoExt, pageNoExt = _d === void 0 ? '/' : _d, _e = _b.normalizedResourcePath, normalizedResourcePath = _e === void 0 ? '' : _e, _f = _b.appFolder, appFolder = _f === void 0 ? '' : _f, _g = _b.isClientComponent, isClientComponent = _g === void 0 ? false : _g, _h = _b.existLocalesFolder, existLocalesFolder = _h === void 0 ? true : _h; var routeType = validPages.find(function (pageName) { return pageNoExt.endsWith(pageName); }) || 'component'; var isPage = routeType !== 'component' && normalizedResourcePath.includes(appFolder); if (!isPage) return code; var hash = Date.now().toString(16); var pathname = pageNoExt.replace(validPagesRegex, '/'); var pageVariableName = (0, utils_1.interceptExport)(pagePkg, 'default', "__Next_Translate__Page__".concat(hash, "__")); if (isPage && !pageVariableName) return code; if (isClientComponent) { return templateRCCPage({ pagePkg: pagePkg, hash: hash, pageVariableName: pageVariableName, pathname: pathname, routeType: routeType, existLocalesFolder: existLocalesFolder, }); } return templateRSCPage({ pagePkg: pagePkg, hash: hash, pageVariableName: pageVariableName, pathname: pathname, routeType: routeType, existLocalesFolder: existLocalesFolder, }); } exports.default = templateAppDir; function templateRSCPage(_a) { var pagePkg = _a.pagePkg, hash = _a.hash, pageVariableName = _a.pageVariableName, pathname = _a.pathname, routeType = _a.routeType, existLocalesFolder = _a.existLocalesFolder; var code = pagePkg.getCode(); return "\n import ".concat(utils_1.INTERNAL_CONFIG_KEY, " from '@next-translate-root/i18n'\n import AppDirI18nProvider from 'next-translate/AppDirI18nProvider'\n import __loadNamespaces from 'next-translate/loadNamespaces'\n\n ").concat(code, "\n\n export default async function __Next_Translate_new__").concat(hash, "__(props) {\n const detectedLang = props.params?.lang ?? props.searchParams?.lang\n\n if (detectedLang === 'favicon.ico') return <").concat(pageVariableName, " {...props} />\n\n ").concat(routeType !== '/page' ? "if (globalThis.__NEXT_TRANSLATE__ && !detectedLang) return <".concat(pageVariableName, " {...props} />") : '', "\n \n const config = { \n ...").concat(utils_1.INTERNAL_CONFIG_KEY, ",\n locale: detectedLang ?? ").concat(utils_1.INTERNAL_CONFIG_KEY, ".defaultLocale,\n loaderName: 'server ").concat(routeType, "',\n pathname: '").concat(pathname, "'\n }\n\n const { __lang, __namespaces } = await __loadNamespaces({ ...config, ").concat((0, utils_1.addLoadLocalesFrom)(existLocalesFolder), " });\n \n const oldNamespaces = globalThis.__NEXT_TRANSLATE__?.namespaces ?? {}\n const namespaces = { ...oldNamespaces, ...__namespaces }\n\n globalThis.__NEXT_TRANSLATE__ = { lang: __lang, namespaces, config }\n\n return <AppDirI18nProvider lang={__lang} namespaces={namespaces} config={JSON.parse(JSON.stringify(config))}><").concat(pageVariableName, " {...props} /></AppDirI18nProvider>\n }\n"); } function templateRCCPage(_a) { var pagePkg = _a.pagePkg, hash = _a.hash, pageVariableName = _a.pageVariableName, pathname = _a.pathname, routeType = _a.routeType, existLocalesFolder = _a.existLocalesFolder; var topLine = utils_1.clientLine[0]; var clientCode = pagePkg.getCode(); utils_1.clientLine.forEach(function (line) { clientCode = clientCode.replace(line, ''); }); return "".concat(topLine, "\n import ").concat(utils_1.INTERNAL_CONFIG_KEY, " from '@next-translate-root/i18n'\n import AppDirI18nProvider from 'next-translate/AppDirI18nProvider'\n import { useSearchParams as __useSearchParams, useParams as __useParams } from 'next/navigation'\n import { use as __use, Suspense as __Suspense } from 'react'\n import __loadNamespaces from 'next-translate/loadNamespaces'\n\n ").concat(clientCode, "\n\n export default function __Next_Translate_new__").concat(hash, "__(props) {\n const searchParams = __useSearchParams()\n const params = __useParams()\n const detectedLang = params.lang ?? searchParams.get('lang')\n\n if (detectedLang === 'favicon.ico') return <").concat(pageVariableName, " {...props} />\n\n ").concat(routeType !== '/page' ? "if (globalThis.__NEXT_TRANSLATE__ && !detectedLang) return <".concat(pageVariableName, " {...props} />") : '', "\n\n const lang = detectedLang ?? ").concat(utils_1.INTERNAL_CONFIG_KEY, ".defaultLocale\n const config = {\n ...").concat(utils_1.INTERNAL_CONFIG_KEY, ",\n locale: lang,\n loaderName: 'client ").concat(routeType, "',\n pathname: '").concat(pathname, "',\n }\n\n return (\n <__Suspense fallback={null}>\n <__Next_Translate__child__").concat(hash, "__ \n {...props} \n config={config} \n promise={__loadNamespaces({ ...config, ").concat((0, utils_1.addLoadLocalesFrom)(existLocalesFolder), " })}\n />\n </__Suspense>\n )\n }\n\n function __Next_Translate__child__").concat(hash, "__({ promise, config, ...props }) {\n const { __lang, __namespaces } = __use(promise);\n const oldNamespaces = globalThis.__NEXT_TRANSLATE__?.namespaces ?? {};\n globalThis.__NEXT_TRANSLATE__ = { lang: __lang, namespaces: { ...oldNamespaces, ...__namespaces }, config };\n return <").concat(pageVariableName, " {...props} />;\n }\n"); }