Skip to content

Commit

Permalink
chore: update next-intl setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ktmouk committed Nov 24, 2024
1 parent 61ad78c commit 640ba2e
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { TabPanel } from "@headlessui/react";
import { useTranslations } from "next-intl";
import { useState } from "react";
import { PiPlus, PiQuestion } from "react-icons/pi";
import { Link } from "../../../navigation";
import { Link } from "../../../i18n/routing";
import { trpc } from "../_components/TrpcProvider";
import { CategoryModal } from "./CategoryModal";
import { CategoryTreeItem } from "./CategoryTreeItem";
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/app/[locale]/_components/SidebarNavItem.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ReactNode } from "react";
import { tv } from "tailwind-variants";
import { Link, usePathname } from "../../../navigation";
import { Link, usePathname } from "../../../i18n/routing";

const barStyle = tv({
base: "my-2 mr-4 block h-6 w-1 rounded-r-sm",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useMemo, useState } from "react";
import { PiCalendarBlank, PiTimer } from "react-icons/pi";
import * as R from "remeda";
import { tv } from "tailwind-variants";
import { useRouter } from "../../../../../navigation";
import { useRouter } from "../../../../../i18n/routing";
import { TimeEntryModal } from "../../../_components/TimeEntryModal";
import { Tooltip } from "../../../_components/Tooltip";
import { trpc } from "../../../_components/TrpcProvider";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { addWeeks, format, isSameWeek, startOfWeek } from "date-fns";
import { useTranslations } from "next-intl";
import { PiCalendarBlank } from "react-icons/pi";
import { useRouter } from "../../../../../navigation";
import { useRouter } from "../../../../../i18n/routing";
import { DatePicker } from "../../../_components/DatePicker";

type Props = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from "date-fns";
import { useTranslations } from "next-intl";
import { tv } from "tailwind-variants";
import { useRouter } from "../../../../../navigation";
import { useRouter } from "../../../../../i18n/routing";
import { DatePicker } from "../../../_components/DatePicker";
import { ListboxButton } from "../../../_components/ListboxButton";
import { ListboxOption } from "../../../_components/ListboxOption";
Expand Down
14 changes: 12 additions & 2 deletions apps/web/src/app/[locale]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import { Provider } from "jotai";
import { notFound } from "next/navigation";
import { NextIntlClientProvider } from "next-intl";
import { getMessages } from "next-intl/server";
import type { ReactNode } from "react";
import { roboto, inter } from "../../../config/fonts";
import { type Locale, formats } from "../../../config/locale";
import { formats } from "../../../config/locale";
import { routing } from "../../i18n/routing";
import { Toast } from "./_components/Toast";
import { TrpcProvider } from "./_components/TrpcProvider";

Expand All @@ -13,11 +15,19 @@ export const fetchCache = "only-no-store";
type Props = {
children: ReactNode;
params: {
locale: Locale;
locale: string;
};
};

const Layout = async ({ children, params: { locale } }: Props) => {
// https://next-intl-docs.vercel.app/docs/getting-started/app-router/with-i18n-routing#layout
if (
typeof locale !== "string" ||
!routing.locales.includes(locale as (typeof routing.locales)[number])
) {
notFound();
}

const messages = await getMessages();

return (
Expand Down
14 changes: 0 additions & 14 deletions apps/web/src/i18n.ts

This file was deleted.

24 changes: 24 additions & 0 deletions apps/web/src/i18n/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { AbstractIntlMessages } from "next-intl";
import { getRequestConfig } from "next-intl/server";
import { routing } from "./routing";

// https://next-intl-docs.vercel.app/docs/getting-started/app-router/with-i18n-routing#i18n-request
export default getRequestConfig(async ({ requestLocale }) => {
let locale = await requestLocale;

if (
typeof locale !== "string" ||
!routing.locales.includes(locale as (typeof routing.locales)[number])
) {
locale = routing.defaultLocale;
}

return {
locale,
messages: (
(await import(`../../messages/${locale}.json`)) as {
default: AbstractIntlMessages;
}
).default,
};
});
11 changes: 11 additions & 0 deletions apps/web/src/i18n/routing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { createNavigation } from "next-intl/navigation";
import { defineRouting } from "next-intl/routing";

// https://next-intl-docs.vercel.app/docs/getting-started/app-router/with-i18n-routing#i18nts
export const routing = defineRouting({
locales: ["en", "ja"],
defaultLocale: "en",
});

export const { Link, redirect, usePathname, useRouter, getPathname } =
createNavigation(routing);
8 changes: 3 additions & 5 deletions apps/web/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import type { NextRequestWithAuth } from "next-auth/middleware";
import withAuth from "next-auth/middleware";
import createIntlMiddleware from "next-intl/middleware";
import * as R from "remeda";
import { defaultLocale, locales } from "../config/locale";
import { locales } from "../config/locale";
import { pageOptions } from "../config/next-auth";
import { serverEnv } from "../env/server.mjs";
import { routing } from "./i18n/routing";

const PUBLIC_PAGES = ["/auth/sign-in"];
const PUBLIC_FILES = ["/favicon.ico", "/robots.txt"];
Expand Down Expand Up @@ -66,10 +67,7 @@ const isAllowedIp = (ip: string | undefined) => {
return serverEnv.ALLOWED_IPS.split(",").includes(ip);
};

const intlMiddleware = createIntlMiddleware({
locales: [...locales],
defaultLocale,
});
const intlMiddleware = createIntlMiddleware(routing);

const authMiddleware = withAuth(
function onSuccess(req) {
Expand Down
5 changes: 0 additions & 5 deletions apps/web/src/navigation.ts

This file was deleted.

0 comments on commit 640ba2e

Please sign in to comment.