Skip to content

Commit 678ee61

Browse files
committed
added comments
1 parent 4a0c2c2 commit 678ee61

File tree

1 file changed

+19
-2
lines changed
  • packages/open-next/src/core/routing/i18n

1 file changed

+19
-2
lines changed

packages/open-next/src/core/routing/i18n/index.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ function getLocaleFromCookie(cookies: Record<string, string>) {
2323
}
2424

2525
// Inspired by https://github.com/vercel/next.js/blob/6d93d652e0e7ba72d9a3b66e78746dce2069db03/packages/next/src/shared/lib/i18n/detect-domain-locale.ts#L3-L25
26+
/**
27+
* @param arg an object containing the hostname and detectedLocale
28+
* @returns The `DomainLocale` object if a domain is detected, `undefined` otherwise
29+
*/
2630
export function detectDomainLocale({
2731
hostname,
2832
detectedLocale,
@@ -31,11 +35,12 @@ export function detectDomainLocale({
3135
detectedLocale?: string;
3236
}): DomainLocale | undefined {
3337
const i18n = NextConfig.i18n;
34-
if (!i18n || !i18n.domains) {
38+
const domains = i18n?.domains;
39+
if (!domains) {
3540
return;
3641
}
3742
const lowercasedLocale = detectedLocale?.toLowerCase();
38-
for (const domain of i18n.domains) {
43+
for (const domain of domains) {
3944
// We remove the port if present
4045
const domainHostname = domain.domain.split(":", 1)[0].toLowerCase();
4146
if (
@@ -50,6 +55,12 @@ export function detectDomainLocale({
5055
}
5156
}
5257

58+
/**
59+
*
60+
* @param internalEvent
61+
* @param i18n
62+
* @returns The detected locale, if `localeDetection` is set to `false` it will return the default locale **or** the domain default locale if a domain is detected.
63+
*/
5364
export function detectLocale(
5465
internalEvent: InternalEvent,
5566
i18n: i18nConfig,
@@ -81,11 +92,17 @@ export function detectLocale(
8192
);
8293
}
8394

95+
/**
96+
* This function is used for OpenNext internal routing to localize the path for next config rewrite/redirects/headers and the middleware
97+
* @param internalEvent
98+
* @returns The localized path
99+
*/
84100
export function localizePath(internalEvent: InternalEvent): string {
85101
const i18n = NextConfig.i18n;
86102
if (!i18n) {
87103
return internalEvent.rawPath;
88104
}
105+
// When the path is already localized we don't need to do anything
89106
if (isLocalizedPath(internalEvent.rawPath)) {
90107
return internalEvent.rawPath;
91108
}

0 commit comments

Comments
 (0)