Skip to content

Commit 392b38f

Browse files
committed
feat: add i18n toggle
1 parent 489e1cc commit 392b38f

File tree

10 files changed

+272
-230
lines changed

10 files changed

+272
-230
lines changed

apps/docs/src/app/provider.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use client';
22

33
import { ORAMA_CONFIGS_MAP } from '@/lib/orama/config';
4+
import { LOCALES, LOCALE_URL_MAP } from '@next-i18n/const';
45
import { RootProvider } from 'fumadocs-ui/provider';
56
import type { RootProviderProps } from 'fumadocs-ui/provider/base';
67
import { useLocale } from 'next-intl';
@@ -16,6 +17,17 @@ export function Provider({ children, ...props }: RootProviderProps) {
1617
return (
1718
<RootProvider
1819
{...props}
20+
i18n={{
21+
locale,
22+
locales: LOCALES.filter((l) => l.enabled).map((l) => ({
23+
name: `${l.flag} ${l.nativeName}`,
24+
locale: l.locale,
25+
})),
26+
onLocaleChange: (locale) => {
27+
const url = LOCALE_URL_MAP[locale];
28+
window.location.href = new URL(window.location.pathname, url).href;
29+
},
30+
}}
1931
search={
2032
config
2133
? {

apps/docs/src/hooks/useLayoutOptions.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Image from 'next/image';
77
export const useBaseOptions = () => {
88
const t = useTranslations('baseOptions');
99
return {
10+
i18n: true,
1011
nav: {
1112
title: (
1213
<>

apps/docs/src/lib/metadata.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { PENDING_SITES, SITES } from '@next-i18n/const';
1+
import { LOCALES, LOCALE_URL_MAP } from '@next-i18n/const';
2+
import type { Locale } from 'next-intl';
23
import type { Metadata } from 'next/types';
34

45
export function createMetadata(
@@ -35,11 +36,15 @@ export function createMetadata(
3536
? {
3637
alternates: {
3738
canonical: baseUrl.origin + override.pathname,
38-
languages: {
39-
'en-US': SITES.en + override.pathname,
40-
'zh-Hans': SITES['zh-hans'] + override.pathname,
41-
'zh-Hant': SITES['zh-hant'] + override.pathname,
42-
},
39+
languages: LOCALES.reduce(
40+
(acc, site) => {
41+
if (site.enabled) {
42+
acc[site.locale] = new URL(site.url + override.pathname).href;
43+
}
44+
return acc;
45+
},
46+
{} as Record<Locale, string>,
47+
),
4348
},
4449
}
4550
: {}),
@@ -51,5 +56,5 @@ export const baseUrl =
5156
? new URL('http://localhost:3000')
5257
: new URL(
5358
// biome-ignore lint/style/noNonNullAssertion: <explanation>
54-
SITES[process.env.LOCALE!] || PENDING_SITES[process.env.LOCALE!],
59+
LOCALE_URL_MAP[process.env.LOCALE!],
5560
);

apps/index/src/app/globals.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@import "tailwindcss";
2-
2+
@source "../../../../packages/const/dist/**/*.js";
33
:root {
44
--background: #ffffff;
55
--foreground: #171717;

0 commit comments

Comments
 (0)