Initial boiler plate project

This commit is contained in:
2024-09-24 03:52:46 +00:00
parent 6120b2d6c3
commit 154b93e267
10034 changed files with 2079352 additions and 2 deletions

View File

@ -0,0 +1,16 @@
export function detectDomainLocale(domainItems, hostname, detectedLocale) {
if (!domainItems) return;
if (detectedLocale) {
detectedLocale = detectedLocale.toLowerCase();
}
for (const item of domainItems){
var _item_domain, _item_locales;
// remove port if present
const domainHostname = (_item_domain = item.domain) == null ? void 0 : _item_domain.split(":", 1)[0].toLowerCase();
if (hostname === domainHostname || detectedLocale === item.defaultLocale.toLowerCase() || ((_item_locales = item.locales) == null ? void 0 : _item_locales.some((locale)=>locale.toLowerCase() === detectedLocale))) {
return item;
}
}
}
//# sourceMappingURL=detect-domain-locale.js.map

View File

@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/shared/lib/i18n/detect-domain-locale.ts"],"names":["detectDomainLocale","domainItems","hostname","detectedLocale","toLowerCase","item","domainHostname","domain","split","defaultLocale","locales","some","locale"],"mappings":"AAEA,OAAO,SAASA,mBACdC,WAA4B,EAC5BC,QAAiB,EACjBC,cAAuB;IAEvB,IAAI,CAACF,aAAa;IAElB,IAAIE,gBAAgB;QAClBA,iBAAiBA,eAAeC,WAAW;IAC7C;IAEA,KAAK,MAAMC,QAAQJ,YAAa;YAEPI,cAIrBA;QALF,yBAAyB;QACzB,MAAMC,kBAAiBD,eAAAA,KAAKE,MAAM,qBAAXF,aAAaG,KAAK,CAAC,KAAK,EAAE,CAAC,EAAE,CAACJ,WAAW;QAChE,IACEF,aAAaI,kBACbH,mBAAmBE,KAAKI,aAAa,CAACL,WAAW,QACjDC,gBAAAA,KAAKK,OAAO,qBAAZL,cAAcM,IAAI,CAAC,CAACC,SAAWA,OAAOR,WAAW,OAAOD,kBACxD;YACA,OAAOE;QACT;IACF;AACF"}

View File

@ -0,0 +1,53 @@
import { acceptLanguage } from "../../../server/accept-header";
import { denormalizePagePath } from "../page-path/denormalize-page-path";
import { detectDomainLocale } from "./detect-domain-locale";
import { formatUrl } from "../router/utils/format-url";
import { getCookieParser } from "../../../server/api-utils/get-cookie-parser";
function getLocaleFromCookie(i18n, headers) {
if (headers === void 0) headers = {};
var _getCookieParser_NEXT_LOCALE, _getCookieParser;
const nextLocale = (_getCookieParser = getCookieParser(headers || {})()) == null ? void 0 : (_getCookieParser_NEXT_LOCALE = _getCookieParser.NEXT_LOCALE) == null ? void 0 : _getCookieParser_NEXT_LOCALE.toLowerCase();
return nextLocale ? i18n.locales.find((locale)=>nextLocale === locale.toLowerCase()) : undefined;
}
function detectLocale(param) {
let { i18n, headers, domainLocale, preferredLocale, pathLocale } = param;
return pathLocale || (domainLocale == null ? void 0 : domainLocale.defaultLocale) || getLocaleFromCookie(i18n, headers) || preferredLocale || i18n.defaultLocale;
}
function getAcceptPreferredLocale(i18n, headers) {
if ((headers == null ? void 0 : headers["accept-language"]) && !Array.isArray(headers["accept-language"])) {
try {
return acceptLanguage(headers["accept-language"], i18n.locales);
} catch (err) {}
}
}
export function getLocaleRedirect(param) {
let { defaultLocale, domainLocale, pathLocale, headers, nextConfig, urlParsed } = param;
if (nextConfig.i18n && nextConfig.i18n.localeDetection !== false && denormalizePagePath(urlParsed.pathname) === "/") {
const preferredLocale = getAcceptPreferredLocale(nextConfig.i18n, headers);
const detectedLocale = detectLocale({
i18n: nextConfig.i18n,
preferredLocale,
headers,
pathLocale,
domainLocale
});
const preferredDomain = 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 formatUrl({
...urlParsed,
pathname: (nextConfig.basePath || "") + "/" + detectedLocale + (nextConfig.trailingSlash ? "/" : "")
});
}
}
}
//# sourceMappingURL=get-locale-redirect.js.map

View File

@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/shared/lib/i18n/get-locale-redirect.ts"],"names":["acceptLanguage","denormalizePagePath","detectDomainLocale","formatUrl","getCookieParser","getLocaleFromCookie","i18n","headers","nextLocale","NEXT_LOCALE","toLowerCase","locales","find","locale","undefined","detectLocale","domainLocale","preferredLocale","pathLocale","defaultLocale","getAcceptPreferredLocale","Array","isArray","err","getLocaleRedirect","nextConfig","urlParsed","localeDetection","pathname","detectedLocale","preferredDomain","domains","isPDomain","domain","isPLocale","scheme","http","rlocale","basePath","trailingSlash"],"mappings":"AAEA,SAASA,cAAc,QAAQ,gCAA+B;AAC9D,SAASC,mBAAmB,QAAQ,qCAAoC;AACxE,SAASC,kBAAkB,QAAQ,yBAAwB;AAC3D,SAASC,SAAS,QAAQ,6BAA4B;AACtD,SAASC,eAAe,QAAQ,8CAA6C;AAe7E,SAASC,oBACPC,IAAgB,EAChBC,OAA8D;IAA9DA,IAAAA,oBAAAA,UAA4D,CAAC;QAE1CH,8BAAAA;IAAnB,MAAMI,cAAaJ,mBAAAA,gBACjBG,WAAW,CAAC,0BADKH,+BAAAA,iBAEdK,WAAW,qBAFGL,6BAEDM,WAAW;IAC7B,OAAOF,aACHF,KAAKK,OAAO,CAACC,IAAI,CAAC,CAACC,SAAWL,eAAeK,OAAOH,WAAW,MAC/DI;AACN;AAEA,SAASC,aAAa,KAYrB;IAZqB,IAAA,EACpBT,IAAI,EACJC,OAAO,EACPS,YAAY,EACZC,eAAe,EACfC,UAAU,EAOX,GAZqB;IAapB,OACEA,eACAF,gCAAAA,aAAcG,aAAa,KAC3Bd,oBAAoBC,MAAMC,YAC1BU,mBACAX,KAAKa,aAAa;AAEtB;AAEA,SAASC,yBACPd,IAAgB,EAChBC,OAA0D;IAE1D,IACEA,CAAAA,2BAAAA,OAAS,CAAC,kBAAkB,KAC5B,CAACc,MAAMC,OAAO,CAACf,OAAO,CAAC,kBAAkB,GACzC;QACA,IAAI;YACF,OAAOP,eAAeO,OAAO,CAAC,kBAAkB,EAAED,KAAKK,OAAO;QAChE,EAAE,OAAOY,KAAK,CAAC;IACjB;AACF;AAEA,OAAO,SAASC,kBAAkB,KAOxB;IAPwB,IAAA,EAChCL,aAAa,EACbH,YAAY,EACZE,UAAU,EACVX,OAAO,EACPkB,UAAU,EACVC,SAAS,EACD,GAPwB;IAQhC,IACED,WAAWnB,IAAI,IACfmB,WAAWnB,IAAI,CAACqB,eAAe,KAAK,SACpC1B,oBAAoByB,UAAUE,QAAQ,MAAM,KAC5C;QACA,MAAMX,kBAAkBG,yBAAyBK,WAAWnB,IAAI,EAAEC;QAClE,MAAMsB,iBAAiBd,aAAa;YAClCT,MAAMmB,WAAWnB,IAAI;YACrBW;YACAV;YACAW;YACAF;QACF;QAEA,MAAMc,kBAAkB5B,mBACtBuB,WAAWnB,IAAI,CAACyB,OAAO,EACvBjB,WACAG;QAGF,IAAID,gBAAgBc,iBAAiB;YACnC,MAAME,YAAYF,gBAAgBG,MAAM,KAAKjB,aAAaiB,MAAM;YAChE,MAAMC,YAAYJ,gBAAgBX,aAAa,KAAKF;YACpD,IAAI,CAACe,aAAa,CAACE,WAAW;gBAC5B,MAAMC,SAAS,AAAC,SAAML,CAAAA,gBAAgBM,IAAI,GAAG,KAAK,GAAE;gBACpD,MAAMC,UAAUH,YAAY,KAAKjB;gBACjC,OAAO,AAAGkB,SAAO,QAAKL,gBAAgBG,MAAM,GAAC,MAAGI;YAClD;QACF;QAEA,IAAIR,eAAenB,WAAW,OAAOS,cAAcT,WAAW,IAAI;YAChE,OAAOP,UAAU;gBACf,GAAGuB,SAAS;gBACZE,UAAU,AAAGH,CAAAA,WAAWa,QAAQ,IAAI,EAAC,IAAE,MAAGT,iBACxCJ,CAAAA,WAAWc,aAAa,GAAG,MAAM,EAAC;YAEtC;QACF;IACF;AACF"}

View File

@ -0,0 +1,28 @@
/**
* 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 function normalizeLocalePath(pathname, locales) {
let detectedLocale;
// first item will be empty string from splitting at first char
const pathnameParts = pathname.split("/");
(locales || []).some((locale)=>{
if (pathnameParts[1] && pathnameParts[1].toLowerCase() === locale.toLowerCase()) {
detectedLocale = locale;
pathnameParts.splice(1, 1);
pathname = pathnameParts.join("/") || "/";
return true;
}
return false;
});
return {
pathname,
detectedLocale
};
}
//# sourceMappingURL=normalize-locale-path.js.map

View File

@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/shared/lib/i18n/normalize-locale-path.ts"],"names":["normalizeLocalePath","pathname","locales","detectedLocale","pathnameParts","split","some","locale","toLowerCase","splice","join"],"mappings":"AAKA;;;;;;;;CAQC,GACD,OAAO,SAASA,oBACdC,QAAgB,EAChBC,OAAkB;IAElB,IAAIC;IACJ,+DAA+D;IAC/D,MAAMC,gBAAgBH,SAASI,KAAK,CAAC;IAEnCH,CAAAA,WAAW,EAAE,AAAD,EAAGI,IAAI,CAAC,CAACC;QACrB,IACEH,aAAa,CAAC,EAAE,IAChBA,aAAa,CAAC,EAAE,CAACI,WAAW,OAAOD,OAAOC,WAAW,IACrD;YACAL,iBAAiBI;YACjBH,cAAcK,MAAM,CAAC,GAAG;YACxBR,WAAWG,cAAcM,IAAI,CAAC,QAAQ;YACtC,OAAO;QACT;QACA,OAAO;IACT;IAEA,OAAO;QACLT;QACAE;IACF;AACF"}