.
This commit is contained in:
+2
@@ -0,0 +1,2 @@
|
||||
import type { DomainLocale } from '../../../server/config-shared';
|
||||
export declare function detectDomainLocale(domainItems?: readonly DomainLocale[], hostname?: string, detectedLocale?: string): DomainLocale | undefined;
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
Object.defineProperty(exports, "detectDomainLocale", {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return detectDomainLocale;
|
||||
}
|
||||
});
|
||||
function detectDomainLocale(domainItems, hostname, detectedLocale) {
|
||||
if (!domainItems) return;
|
||||
if (detectedLocale) {
|
||||
detectedLocale = detectedLocale.toLowerCase();
|
||||
}
|
||||
for (const item of domainItems){
|
||||
// remove port if present
|
||||
const domainHostname = item.domain?.split(':', 1)[0].toLowerCase();
|
||||
if (hostname === domainHostname || detectedLocale === item.defaultLocale.toLowerCase() || item.locales?.some((locale)=>locale.toLowerCase() === detectedLocale)) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//# sourceMappingURL=detect-domain-locale.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../src/shared/lib/i18n/detect-domain-locale.ts"],"sourcesContent":["import type { DomainLocale } from '../../../server/config-shared'\n\nexport function detectDomainLocale(\n domainItems?: readonly DomainLocale[],\n hostname?: string,\n detectedLocale?: string\n) {\n if (!domainItems) return\n\n if (detectedLocale) {\n detectedLocale = detectedLocale.toLowerCase()\n }\n\n for (const item of domainItems) {\n // remove port if present\n const domainHostname = item.domain?.split(':', 1)[0].toLowerCase()\n if (\n hostname === domainHostname ||\n detectedLocale === item.defaultLocale.toLowerCase() ||\n item.locales?.some((locale) => locale.toLowerCase() === detectedLocale)\n ) {\n return item\n }\n }\n}\n"],"names":["detectDomainLocale","domainItems","hostname","detectedLocale","toLowerCase","item","domainHostname","domain","split","defaultLocale","locales","some","locale"],"mappings":";;;;+BAEgBA;;;eAAAA;;;AAAT,SAASA,mBACdC,WAAqC,EACrCC,QAAiB,EACjBC,cAAuB;IAEvB,IAAI,CAACF,aAAa;IAElB,IAAIE,gBAAgB;QAClBA,iBAAiBA,eAAeC,WAAW;IAC7C;IAEA,KAAK,MAAMC,QAAQJ,YAAa;QAC9B,yBAAyB;QACzB,MAAMK,iBAAiBD,KAAKE,MAAM,EAAEC,MAAM,KAAK,EAAE,CAAC,EAAE,CAACJ;QACrD,IACEF,aAAaI,kBACbH,mBAAmBE,KAAKI,aAAa,CAACL,WAAW,MACjDC,KAAKK,OAAO,EAAEC,KAAK,CAACC,SAAWA,OAAOR,WAAW,OAAOD,iBACxD;YACA,OAAOE;QACT;IACF;AACF","ignoreList":[0]}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
import type { DomainLocale } from '../../../server/config';
|
||||
import type { I18NConfig } from '../../../server/config-shared';
|
||||
interface Options {
|
||||
defaultLocale: string;
|
||||
domainLocale?: DomainLocale;
|
||||
headers?: {
|
||||
[key: string]: string | string[] | undefined;
|
||||
};
|
||||
nextConfig: {
|
||||
basePath?: string;
|
||||
i18n?: I18NConfig | null;
|
||||
trailingSlash?: boolean;
|
||||
};
|
||||
pathLocale?: string;
|
||||
urlParsed: {
|
||||
hostname?: string | null;
|
||||
pathname: string;
|
||||
};
|
||||
}
|
||||
export declare function getLocaleRedirect({ defaultLocale, domainLocale, pathLocale, headers, nextConfig, urlParsed, }: Options): string | undefined;
|
||||
export {};
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
Object.defineProperty(exports, "getLocaleRedirect", {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return getLocaleRedirect;
|
||||
}
|
||||
});
|
||||
const _acceptheader = require("../../../server/accept-header");
|
||||
const _denormalizepagepath = require("../page-path/denormalize-page-path");
|
||||
const _detectdomainlocale = require("./detect-domain-locale");
|
||||
const _formaturl = require("../router/utils/format-url");
|
||||
const _getcookieparser = require("../../../server/api-utils/get-cookie-parser");
|
||||
function getLocaleFromCookie(i18n, headers = {}) {
|
||||
const nextLocale = (0, _getcookieparser.getCookieParser)(headers || {})()?.NEXT_LOCALE?.toLowerCase();
|
||||
return nextLocale ? i18n.locales.find((locale)=>nextLocale === locale.toLowerCase()) : undefined;
|
||||
}
|
||||
function detectLocale({ i18n, headers, domainLocale, preferredLocale, pathLocale }) {
|
||||
return pathLocale || domainLocale?.defaultLocale || getLocaleFromCookie(i18n, headers) || preferredLocale || i18n.defaultLocale;
|
||||
}
|
||||
function getAcceptPreferredLocale(i18n, headers) {
|
||||
if (headers?.['accept-language'] && !Array.isArray(headers['accept-language'])) {
|
||||
try {
|
||||
return (0, _acceptheader.acceptLanguage)(headers['accept-language'], i18n.locales);
|
||||
} catch (err) {}
|
||||
}
|
||||
}
|
||||
function getLocaleRedirect({ defaultLocale, domainLocale, pathLocale, headers, nextConfig, urlParsed }) {
|
||||
if (nextConfig.i18n && nextConfig.i18n.localeDetection !== false && (0, _denormalizepagepath.denormalizePagePath)(urlParsed.pathname) === '/') {
|
||||
const preferredLocale = getAcceptPreferredLocale(nextConfig.i18n, headers);
|
||||
const detectedLocale = detectLocale({
|
||||
i18n: nextConfig.i18n,
|
||||
preferredLocale,
|
||||
headers,
|
||||
pathLocale,
|
||||
domainLocale
|
||||
});
|
||||
const preferredDomain = (0, _detectdomainlocale.detectDomainLocale)(nextConfig.i18n.domains, undefined, preferredLocale);
|
||||
if (domainLocale && preferredDomain) {
|
||||
const isPDomain = preferredDomain.domain === domainLocale.domain;
|
||||
const isPLocale = preferredDomain.defaultLocale === preferredLocale;
|
||||
if (!isPDomain || !isPLocale) {
|
||||
const scheme = `http${preferredDomain.http ? '' : 's'}`;
|
||||
const rlocale = isPLocale ? '' : preferredLocale;
|
||||
return `${scheme}://${preferredDomain.domain}/${rlocale}`;
|
||||
}
|
||||
}
|
||||
if (detectedLocale.toLowerCase() !== defaultLocale.toLowerCase()) {
|
||||
return (0, _formaturl.formatUrl)({
|
||||
...urlParsed,
|
||||
pathname: `${nextConfig.basePath || ''}/${detectedLocale}${nextConfig.trailingSlash ? '/' : ''}`
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//# sourceMappingURL=get-locale-redirect.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+14
@@ -0,0 +1,14 @@
|
||||
export interface PathLocale {
|
||||
detectedLocale?: string;
|
||||
pathname: string;
|
||||
}
|
||||
/**
|
||||
* For a pathname that may include a locale from a list of locales, it
|
||||
* removes the locale from the pathname returning it alongside with the
|
||||
* detected locale.
|
||||
*
|
||||
* @param pathname A pathname that may include a locale.
|
||||
* @param locales A list of locales.
|
||||
* @returns The detected locale and pathname without locale
|
||||
*/
|
||||
export declare function normalizeLocalePath(pathname: string, locales?: readonly string[]): PathLocale;
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
Object.defineProperty(exports, "normalizeLocalePath", {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return normalizeLocalePath;
|
||||
}
|
||||
});
|
||||
/**
|
||||
* A cache of lowercased locales for each list of locales. This is stored as a
|
||||
* WeakMap so if the locales are garbage collected, the cache entry will be
|
||||
* removed as well.
|
||||
*/ const cache = new WeakMap();
|
||||
function normalizeLocalePath(pathname, locales) {
|
||||
// If locales is undefined, return the pathname as is.
|
||||
if (!locales) return {
|
||||
pathname
|
||||
};
|
||||
// Get the cached lowercased locales or create a new cache entry.
|
||||
let lowercasedLocales = cache.get(locales);
|
||||
if (!lowercasedLocales) {
|
||||
lowercasedLocales = locales.map((locale)=>locale.toLowerCase());
|
||||
cache.set(locales, lowercasedLocales);
|
||||
}
|
||||
let detectedLocale;
|
||||
// The first segment will be empty, because it has a leading `/`. If
|
||||
// there is no further segment, there is no locale (or it's the default).
|
||||
const segments = pathname.split('/', 2);
|
||||
// If there's no second segment (ie, the pathname is just `/`), there's no
|
||||
// locale.
|
||||
if (!segments[1]) return {
|
||||
pathname
|
||||
};
|
||||
// The second segment will contain the locale part if any.
|
||||
const segment = segments[1].toLowerCase();
|
||||
// See if the segment matches one of the locales. If it doesn't, there is
|
||||
// no locale (or it's the default).
|
||||
const index = lowercasedLocales.indexOf(segment);
|
||||
if (index < 0) return {
|
||||
pathname
|
||||
};
|
||||
// Return the case-sensitive locale.
|
||||
detectedLocale = locales[index];
|
||||
// Remove the `/${locale}` part of the pathname.
|
||||
pathname = pathname.slice(detectedLocale.length + 1) || '/';
|
||||
return {
|
||||
pathname,
|
||||
detectedLocale
|
||||
};
|
||||
}
|
||||
|
||||
//# sourceMappingURL=normalize-locale-path.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../src/shared/lib/i18n/normalize-locale-path.ts"],"sourcesContent":["export interface PathLocale {\n detectedLocale?: string\n pathname: string\n}\n\n/**\n * A cache of lowercased locales for each list of locales. This is stored as a\n * WeakMap so if the locales are garbage collected, the cache entry will be\n * removed as well.\n */\nconst cache = new WeakMap<readonly string[], readonly string[]>()\n\n/**\n * For a pathname that may include a locale from a list of locales, it\n * removes the locale from the pathname returning it alongside with the\n * detected locale.\n *\n * @param pathname A pathname that may include a locale.\n * @param locales A list of locales.\n * @returns The detected locale and pathname without locale\n */\nexport function normalizeLocalePath(\n pathname: string,\n locales?: readonly string[]\n): PathLocale {\n // If locales is undefined, return the pathname as is.\n if (!locales) return { pathname }\n\n // Get the cached lowercased locales or create a new cache entry.\n let lowercasedLocales = cache.get(locales)\n if (!lowercasedLocales) {\n lowercasedLocales = locales.map((locale) => locale.toLowerCase())\n cache.set(locales, lowercasedLocales)\n }\n\n let detectedLocale: string | undefined\n\n // The first segment will be empty, because it has a leading `/`. If\n // there is no further segment, there is no locale (or it's the default).\n const segments = pathname.split('/', 2)\n\n // If there's no second segment (ie, the pathname is just `/`), there's no\n // locale.\n if (!segments[1]) return { pathname }\n\n // The second segment will contain the locale part if any.\n const segment = segments[1].toLowerCase()\n\n // See if the segment matches one of the locales. If it doesn't, there is\n // no locale (or it's the default).\n const index = lowercasedLocales.indexOf(segment)\n if (index < 0) return { pathname }\n\n // Return the case-sensitive locale.\n detectedLocale = locales[index]\n\n // Remove the `/${locale}` part of the pathname.\n pathname = pathname.slice(detectedLocale.length + 1) || '/'\n\n return { pathname, detectedLocale }\n}\n"],"names":["normalizeLocalePath","cache","WeakMap","pathname","locales","lowercasedLocales","get","map","locale","toLowerCase","set","detectedLocale","segments","split","segment","index","indexOf","slice","length"],"mappings":";;;;+BAqBgBA;;;eAAAA;;;AAhBhB;;;;CAIC,GACD,MAAMC,QAAQ,IAAIC;AAWX,SAASF,oBACdG,QAAgB,EAChBC,OAA2B;IAE3B,sDAAsD;IACtD,IAAI,CAACA,SAAS,OAAO;QAAED;IAAS;IAEhC,iEAAiE;IACjE,IAAIE,oBAAoBJ,MAAMK,GAAG,CAACF;IAClC,IAAI,CAACC,mBAAmB;QACtBA,oBAAoBD,QAAQG,GAAG,CAAC,CAACC,SAAWA,OAAOC,WAAW;QAC9DR,MAAMS,GAAG,CAACN,SAASC;IACrB;IAEA,IAAIM;IAEJ,oEAAoE;IACpE,yEAAyE;IACzE,MAAMC,WAAWT,SAASU,KAAK,CAAC,KAAK;IAErC,0EAA0E;IAC1E,UAAU;IACV,IAAI,CAACD,QAAQ,CAAC,EAAE,EAAE,OAAO;QAAET;IAAS;IAEpC,0DAA0D;IAC1D,MAAMW,UAAUF,QAAQ,CAAC,EAAE,CAACH,WAAW;IAEvC,yEAAyE;IACzE,mCAAmC;IACnC,MAAMM,QAAQV,kBAAkBW,OAAO,CAACF;IACxC,IAAIC,QAAQ,GAAG,OAAO;QAAEZ;IAAS;IAEjC,oCAAoC;IACpCQ,iBAAiBP,OAAO,CAACW,MAAM;IAE/B,gDAAgD;IAChDZ,WAAWA,SAASc,KAAK,CAACN,eAAeO,MAAM,GAAG,MAAM;IAExD,OAAO;QAAEf;QAAUQ;IAAe;AACpC","ignoreList":[0]}
|
||||
Reference in New Issue
Block a user