Skip to content

Fix i18n for domain #759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 101 additions & 4 deletions packages/open-next/src/core/routing/i18n/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NextConfig } from "config/index.js";
import type { i18nConfig } from "types/next-types";
import type { InternalEvent } from "types/open-next";
import type { DomainLocale, i18nConfig } from "types/next-types";
import type { InternalEvent, InternalResult } from "types/open-next";

import { emptyReadableStream } from "utils/stream.js";
import { debug } from "../../../adapters/logger.js";
import { acceptLanguage } from "./accept-header";

Expand All @@ -20,6 +21,31 @@ function getLocaleFromCookie(cookies: Record<string, string>) {
: undefined;
}

// Inspired by https://github.com/vercel/next.js/blob/canary/packages/next/src/shared/lib/i18n/detect-domain-locale.ts
export function detectDomainLocale({
hostname,
detectedLocale,
}: {
hostname?: string;
detectedLocale?: string;
}): DomainLocale | undefined {
const i18n = NextConfig.i18n;
if (!i18n || i18n.localeDetection === false || !i18n.domains) {
return;
}
for (const item of i18n.domains) {
// We remove the port if present
const domainHostname = item.domain.split(":", 1)[0].toLowerCase();
if (
hostname === domainHostname ||
detectedLocale === item.defaultLocale.toLowerCase() ||
item.locales?.some((locale) => detectedLocale === locale.toLowerCase())
) {
return item;
}
}
}

export function detectLocale(
internalEvent: InternalEvent,
i18n: i18nConfig,
Expand All @@ -39,9 +65,16 @@ export function detectLocale(
defaultLocale: i18n.defaultLocale,
});

return cookiesLocale ?? preferredLocale ?? i18n.defaultLocale;
const domainLocale = detectDomainLocale({
hostname: internalEvent.headers.host,
});

// TODO: handle domain based locale detection
return (
domainLocale?.defaultLocale ??
cookiesLocale ??
preferredLocale ??
i18n.defaultLocale
);
}

export function localizePath(internalEvent: InternalEvent): string {
Expand All @@ -52,6 +85,70 @@ export function localizePath(internalEvent: InternalEvent): string {
if (isLocalizedPath(internalEvent.rawPath)) {
return internalEvent.rawPath;
}

const detectedLocale = detectLocale(internalEvent, i18n);

return `/${detectedLocale}${internalEvent.rawPath}`;
}

export function handleLocaleRedirect(
internalEvent: InternalEvent,
): false | InternalResult {
const i18n = NextConfig.i18n;
if (
!i18n ||
i18n.localeDetection === false ||
internalEvent.rawPath !== "/"
) {
return false;
}
const preferredLocale = acceptLanguage(
internalEvent.headers["accept-language"],
i18n?.locales,
);

const detectedLocale = detectLocale(internalEvent, i18n);

const domainLocale = detectDomainLocale({
hostname: internalEvent.headers.host,
});
const preferredDomain = detectDomainLocale({
detectedLocale: preferredLocale?.toLowerCase(),
});

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 {
type: "core",
statusCode: 307,
headers: {
Location: `${scheme}://${preferredDomain.domain}/${rlocale}`,
},
body: emptyReadableStream(),
isBase64Encoded: false,
};
}
}

const defaultLocale = domainLocale?.defaultLocale ?? i18n.defaultLocale;

if (detectedLocale.toLowerCase() !== defaultLocale.toLowerCase()) {
return {
type: "core",
statusCode: 307,
headers: {
Location: new URL(
`${NextConfig.basePath || ""}/${detectedLocale}`,
internalEvent.url,
).href,
},
body: emptyReadableStream(),
isBase64Encoded: false,
};
}
return false;
}
6 changes: 5 additions & 1 deletion packages/open-next/src/core/routing/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { InternalEvent, InternalResult } from "types/open-next";
import { emptyReadableStream, toReadableStream } from "utils/stream";

import { debug } from "../../adapters/logger";
import { localizePath } from "./i18n";
import { handleLocaleRedirect, localizePath } from "./i18n";
import {
constructNextUrl,
convertFromQueryString,
Expand Down Expand Up @@ -317,6 +317,10 @@ export function handleRedirects(
): InternalResult | undefined {
const trailingSlashRedirect = handleTrailingSlashRedirect(event);
if (trailingSlashRedirect) return trailingSlashRedirect;

const localeRedirect = handleLocaleRedirect(event);
if (localeRedirect) return localeRedirect;

const { internalEvent, __rewrite } = handleRewrites(
event,
redirects.filter((r) => !r.internal),
Expand Down
8 changes: 8 additions & 0 deletions packages/open-next/src/types/next-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,17 @@ export type Header = {
missing?: RouteHas[];
};

export interface DomainLocale {
defaultLocale: string;
domain: string;
http?: true;
locales: readonly string[];
}

export interface i18nConfig {
locales: string[];
defaultLocale: string;
domains?: Array<DomainLocale>;
localeDetection?: false;
}
export interface NextConfig {
Expand Down
Loading
Loading