next-intl
Version:
Internationalization (i18n) for Next.js
2 lines (1 loc) • 1.43 kB
JavaScript
import{normalizeTrailingSlash as e}from"../shared/utils.js";import{getHost as a,getNormalizedPathname as t,getLocalePrefixes as n,isLocaleSupportedOnDomain as o,applyBasePath as r,formatTemplatePathname as l}from"./utils.js";function s({internalTemplateName:s,localizedPathnames:i,request:m,resolvedLocale:c,routing:p}){const f=m.nextUrl.clone(),h=a(m.headers);function u(a,t){return a.pathname=e(a.pathname),m.nextUrl.basePath&&((a=new URL(a)).pathname=r(a.pathname,m.nextUrl.basePath)),`<${a.toString()}>; rel="alternate"; hreflang="${t}"`}function d(e,a){if(i&&"object"==typeof i){const t=i[c];return l(e,t??s,i[a]??s)}return e}h&&(f.port="",f.host=h),f.protocol=m.headers.get("x-forwarded-proto")??f.protocol,f.pathname=t(f.pathname,p.locales,p.localePrefix);const x=n(p.locales,p.localePrefix,!1).flatMap((([e,a])=>{function t(e){return"/"===e?a:a+e}let n;if(p.domains){return p.domains.filter((a=>o(e,a))).map((a=>{n=new URL(f),n.port="",n.host=a.domain,n.pathname=d(f.pathname,e);const o=a.localePrefix||p.localePrefix.mode;return e===a.defaultLocale&&"always"!==o||(n.pathname=t(n.pathname)),u(n,e)}))}{let a;a=i&&"object"==typeof i?d(f.pathname,e):f.pathname,e===p.defaultLocale&&"always"!==p.localePrefix.mode||(a=t(a)),n=new URL(a,f)}return u(n,e)}));if(!p.domains||0===p.domains.length){const e=d(f.pathname,p.defaultLocale);if(e){const a=new URL(e,f);x.push(u(a,"x-default"))}}return x.join(", ")}export{s as default};