Skip to content

Commit

Permalink
fix: next-intl usage
Browse files Browse the repository at this point in the history
  • Loading branch information
yo-iwamoto committed Dec 30, 2024
1 parent cc8cd59 commit 79c3958
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/app/[locale]/_components/layout/layout-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ export function LayoutView({ children, locale }: Props) {
</button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem>XXX</DropdownMenuItem>
<DropdownMenuItem>YYY</DropdownMenuItem>
<DropdownMenuItem>ZZZ</DropdownMenuItem>
<DropdownMenuItem>foo</DropdownMenuItem>
<DropdownMenuItem>bar</DropdownMenuItem>
<DropdownMenuItem>baz</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
Expand Down
13 changes: 13 additions & 0 deletions src/app/[locale]/_components/top-page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useTranslations } from "next-intl";

export function TopPage() {
const t = useTranslations("top-page");

return (
<main className="container">
<h1 className="font-bold text-5xl italic mt-32 text-center">
{t("tagline")}
</h1>
</main>
);
}
7 changes: 2 additions & 5 deletions src/app/[locale]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { initializeLocale } from "@/i18n/initialize-locale";
import { TopPage } from "./_components/top-page";

type Props = {
params: Promise<{
Expand All @@ -10,9 +11,5 @@ export default async function Page({ params }: Props) {
const { locale } = await params;
initializeLocale(locale);

return (
<main className="container">
<h1 className="font-bold text-xl">Page</h1>
</main>
);
return <TopPage />;
}
6 changes: 4 additions & 2 deletions src/i18n/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { getRequestConfig } from "next-intl/server";
import { notFound } from "next/navigation";
import { locales } from "./locales/list";

export default getRequestConfig(async ({ locale }) => {
if (!locales.includes(locale as (typeof locales)[number])) notFound();
export default getRequestConfig(async ({ requestLocale }) => {
const locale = (await requestLocale) as (typeof locales)[number];
if (!locales.includes(locale)) notFound();

return {
messages: (await import(`./locales/${locale}.json`)).default,
locale,
};
});
3 changes: 3 additions & 0 deletions src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"light": "Light",
"dark": "Dark",
"system": "System"
},
"top-page": {
"tagline": "Hello world"
}
}
3 changes: 3 additions & 0 deletions src/i18n/locales/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"light": "ライト",
"dark": "ダーク",
"system": "システム"
},
"top-page": {
"tagline": "こんにちは、世界"
}
}

0 comments on commit 79c3958

Please sign in to comment.