Created
December 26, 2025 17:14
-
-
Save ayane0857/361abb25d6377be2ed34ee104ce5fcc2 to your computer and use it in GitHub Desktop.
next-intl-sample-qiita
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 { getRequestConfig } from "next-intl/server"; | |
| import { headers } from "next/headers"; | |
| import { routing } from "./i18nrouting"; | |
| export default getRequestConfig(async () => { | |
| const headersList = await headers(); | |
| const accept_lang = headersList.get("Accept-Language") || ""; | |
| const firstTag = accept_lang.split(",")[0]?.trim(); // e.g. "en-US" | |
| const browserLocale = firstTag.split(/[-;]/)[0]; // e.g. "en" | |
| const locale = routing.locales.includes( | |
| browserLocale as (typeof routing.locales)[number] | |
| ) | |
| ? browserLocale | |
| : routing.defaultLocale; | |
| return { | |
| locale, | |
| messages: (await import(`@/components/locales/${locale}.json`)).default, | |
| }; | |
| }); |
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 { defineRouting } from "next-intl/routing"; | |
| export const routing = defineRouting({ | |
| locales: ["ja", "en"], | |
| defaultLocale: "ja", | |
| }); |
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 { getLocale } from "next-intl/server"; | |
| import type { Metadata } from "next"; | |
| import "./globals.css"; | |
| export const metadata: Metadata = { | |
| title: "Next-intl sample", | |
| description: "A sample project for next-intl", | |
| }; | |
| export default async function RootLayout({ | |
| children, | |
| }: Readonly<{ | |
| children: React.ReactNode; | |
| }>) { | |
| const locale = await getLocale(); | |
| return ( | |
| <html lang={locale}> | |
| <body>{children}</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("HomePage"); | |
| return ( | |
| <div> | |
| <h1>{t("Name")}</h1> | |
| <p>{t("Message")}</p> | |
| </div> | |
| ); | |
| } |
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
| { | |
| "HomePage": { | |
| "Name": "Ayane", | |
| "Message": "Hello and welcome to my site!" | |
| } | |
| } |
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
| { | |
| "HomePage": { | |
| "Name": "彩音", | |
| "Message": "こんにちは、私のサイトへようこそ!" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment