Skip to content

Instantly share code, notes, and snippets.

@ayane0857
Created December 26, 2025 17:14
Show Gist options
  • Select an option

  • Save ayane0857/361abb25d6377be2ed34ee104ce5fcc2 to your computer and use it in GitHub Desktop.

Select an option

Save ayane0857/361abb25d6377be2ed34ee104ce5fcc2 to your computer and use it in GitHub Desktop.
next-intl-sample-qiita
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,
};
});
import { defineRouting } from "next-intl/routing";
export const routing = defineRouting({
locales: ["ja", "en"],
defaultLocale: "ja",
});
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>
);
}
import { useTranslations } from "next-intl";
export default function Home() {
const t = useTranslations("HomePage");
return (
<div>
<h1>{t("Name")}</h1>
<p>{t("Message")}</p>
</div>
);
}
{
"HomePage": {
"Name": "Ayane",
"Message": "Hello and welcome to my site!"
}
}
{
"HomePage": {
"Name": "彩音",
"Message": "こんにちは、私のサイトへようこそ!"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment