Last active
March 23, 2025 01:24
-
-
Save nixjs/866554e2c92a8eca05712ea69282c21f to your computer and use it in GitHub Desktop.
Use i18n CDN for next app router
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const locales = ['en', 'vi'] as const; | |
export interface Messages { | |
welcome: string; | |
[key: string]: string | object; | |
} | |
declare module 'next-intl' { | |
interface AppConfig { | |
Locale: (typeof locales)[number]; | |
Messages: Messages; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Messages } from './i18n'; | |
export async function getRequestConfig({ locale }: { locale: string }) { | |
const response = await fetch(`https://your-cdn-url.com/locales/${locale}/translation.json`); | |
const messages: Messages = await response.json(); | |
return { messages }; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NextIntlClientProvider } from 'next-intl'; | |
import { getRequestConfig } from '../../i18n'; | |
export async function generateStaticParams() { | |
return [{ locale: 'en' }, { locale: 'vi' }]; | |
} | |
export default async function RootLayout({ | |
children, | |
params: { locale }, | |
}: { | |
children: React.ReactNode; | |
params: { locale: string }; | |
}) { | |
const { messages } = await getRequestConfig({ locale }); | |
return ( | |
<html lang={locale}> | |
<body> | |
<NextIntlClientProvider locale={locale} messages={messages}> | |
{children} | |
</NextIntlClientProvider> | |
</body> | |
</html> | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useTranslations } from 'next-intl'; | |
export default function Home() { | |
const t = useTranslations(); | |
return <h1>{t('welcome')}</h1>; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment