Skip to content

Commit 69ba2e5

Browse files
committed
feat: link with locale
1 parent 28dad49 commit 69ba2e5

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

demo/new-compiler-next16/app/[locale]/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Counter } from "@/components/Counter";
22
import { LocaleSwitcher } from "@lingo.dev/compiler/react";
3-
import Link from "next/link";
3+
import { Link } from "@/components/LocaleLink";
44
import { ServerChild } from "@/components/ServerChild";
55
import { ClientChildWrapper } from "@/components/ClientChildWrapper";
66

@@ -22,7 +22,7 @@ export default function Home() {
2222
<header className="flex justify-between p-10 w-full">
2323
<span>Lingo.dev compiler Next demo</span>
2424
<nav>
25-
<Link href="/test">Test</Link> 1
25+
<Link href="test">Test</Link> 1
2626
</nav>
2727
<LocaleSwitcher
2828
locales={[
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use client";
2+
3+
import NextLink from "next/link";
4+
import { useLingoContext } from "@lingo.dev/compiler/react";
5+
import type { ComponentProps } from "react";
6+
7+
/**
8+
* Link component that automatically prefixes hrefs with current locale
9+
*/
10+
export function Link({ href, ...props }: ComponentProps<typeof NextLink>) {
11+
const { locale } = useLingoContext();
12+
13+
// If href is already locale-prefixed or external, use as-is
14+
const localizedHref = typeof href === "string" && !href.startsWith("http") && !href.startsWith(`/${locale}`)
15+
? `/${locale}${href.startsWith("/") ? "" : "/"}${href}`
16+
: href;
17+
18+
return <NextLink href={localizedHref} {...props} />;
19+
}

0 commit comments

Comments
 (0)