Skip to content

Commit

Permalink
refactor(core): split i18n messages from middleware (bigcommerce#1333)
Browse files Browse the repository at this point in the history
  • Loading branch information
deini authored Sep 4, 2024
1 parent 0381bf1 commit e2c0153
Show file tree
Hide file tree
Showing 31 changed files with 83 additions and 76 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-zoos-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": patch
---

Splits i18n into request.ts and routing.ts This helps reduce our middleware bundle as we no longer do a dynamic import on our middleware entrypoint.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getTranslations, unstable_setRequestLocale } from 'next-intl/server';

import { ProductCard } from '~/components/product-card';
import { Pagination } from '~/components/ui/pagination';
import { LocaleType } from '~/i18n';
import { LocaleType } from '~/i18n/routing';

import { FacetedSearch } from '../../_components/faceted-search';
import { MobileSideNav } from '../../_components/mobile-side-nav';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getChannelIdFromLocale } from '~/channels.config';
import { client } from '~/client';
import { graphql, VariablesOf } from '~/client/graphql';
import { revalidate as revalidateTarget } from '~/client/revalidate-target';
import { locales } from '~/i18n';
import { locales } from '~/i18n/routing';

import BrandPage from '../page';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getTranslations, unstable_setRequestLocale } from 'next-intl/server';
import { Breadcrumbs } from '~/components/breadcrumbs';
import { ProductCard } from '~/components/product-card';
import { Pagination } from '~/components/ui/pagination';
import { LocaleType } from '~/i18n';
import { LocaleType } from '~/i18n/routing';

import { FacetedSearch } from '../../_components/faceted-search';
import { MobileSideNav } from '../../_components/mobile-side-nav';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { client } from '~/client';
import { graphql, VariablesOf } from '~/client/graphql';
import { revalidate as revalidateTarget } from '~/client/revalidate-target';
import { ExistingResultType } from '~/client/util';
import { locales } from '~/i18n';
import { locales } from '~/i18n/routing';

import { CategoryTreeFragment } from '../_components/sub-categories';
import CategoryPage from '../page';
Expand Down
2 changes: 1 addition & 1 deletion core/app/[locale]/(default)/account/(tabs)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { unstable_setRequestLocale } from 'next-intl/server';
import { PropsWithChildren } from 'react';

import { Link } from '~/components/link';
import { LocaleType } from '~/i18n';
import { LocaleType } from '~/i18n/routing';
import { cn } from '~/lib/utils';

const tabList = ['addresses', 'settings'] as const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
Input,
} from '~/components/ui/form';
import { Message } from '~/components/ui/message';
import { useRouter } from '~/routing';
import { useRouter } from '~/i18n/routing';

import { submitCustomerChangePasswordForm } from '../_actions/submit-customer-change-password-form';

Expand Down
2 changes: 1 addition & 1 deletion core/app/[locale]/(default)/blog/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { notFound } from 'next/navigation';

import { BlogPostCard } from '~/components/blog-post-card';
import { Pagination } from '~/components/ui/pagination';
import { LocaleType } from '~/i18n';
import { LocaleType } from '~/i18n/routing';

import { getBlogPosts } from './page-data';

Expand Down
2 changes: 1 addition & 1 deletion core/app/[locale]/(default)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { FooterFragment } from '~/components/footer/fragment';
import { Header } from '~/components/header';
import { Cart } from '~/components/header/cart';
import { HeaderFragment } from '~/components/header/fragment';
import { LocaleType } from '~/i18n';
import { LocaleType } from '~/i18n/routing';

interface Props extends PropsWithChildren {
params: { locale: LocaleType };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
Input,
} from '~/components/ui/form';
import { Message } from '~/components/ui/message';
import { useRouter } from '~/routing';
import { useRouter } from '~/i18n/routing';

import { useAccountStatusContext } from '../../account/(tabs)/_components/account-status-provider';
import { submitChangePasswordForm } from '../_actions/submit-change-password-form';
Expand Down
2 changes: 1 addition & 1 deletion core/app/[locale]/(default)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { revalidate } from '~/client/revalidate-target';
import { ProductCardCarousel } from '~/components/product-card-carousel';
import { ProductCardCarouselFragment } from '~/components/product-card-carousel/fragment';
import { Slideshow } from '~/components/slideshow';
import { LocaleType } from '~/i18n';
import { LocaleType } from '~/i18n/routing';

const HomePageQuery = graphql(
`
Expand Down
2 changes: 1 addition & 1 deletion core/app/[locale]/(default)/product/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getTranslations, unstable_setRequestLocale } from 'next-intl/server';
import { Suspense } from 'react';

import { Breadcrumbs } from '~/components/breadcrumbs';
import { LocaleType } from '~/i18n';
import { LocaleType } from '~/i18n/routing';

import { Description } from './_components/description';
import { Details } from './_components/details';
Expand Down
2 changes: 1 addition & 1 deletion core/app/[locale]/(default)/product/[slug]/static/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getChannelIdFromLocale } from '~/channels.config';
import { client } from '~/client';
import { graphql } from '~/client/graphql';
import { revalidate as revalidateTarget } from '~/client/revalidate-target';
import { locales } from '~/i18n';
import { locales } from '~/i18n/routing';

import ProductPage from '../page';

Expand Down
2 changes: 1 addition & 1 deletion core/app/[locale]/(default)/static/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { locales } from '~/i18n';
import { locales } from '~/i18n/routing';

import HomePage from '../page';

Expand Down
2 changes: 1 addition & 1 deletion core/app/[locale]/maintenance/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { client } from '~/client';
import { graphql } from '~/client/graphql';
import { StoreLogo } from '~/components/store-logo';
import { StoreLogoFragment } from '~/components/store-logo/fragment';
import { LocaleType } from '~/i18n';
import { LocaleType } from '~/i18n/routing';

const MaintenancePageQuery = graphql(
`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Link } from '~/components/link';
import { localeLanguageRegionMap, LocaleType } from '~/i18n';
import { localeLanguageRegionMap, LocaleType } from '~/i18n/routing';
import { cn } from '~/lib/utils';

export const LocaleLink = ({ locale, selected }: { locale: string; selected: boolean }) => {
Expand Down
2 changes: 1 addition & 1 deletion core/app/[locale]/store-selector/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { graphql } from '~/client/graphql';
import { Link } from '~/components/link';
import { StoreLogo } from '~/components/store-logo';
import { StoreLogoFragment } from '~/components/store-logo/fragment';
import { locales, LocaleType } from '~/i18n';
import { locales, LocaleType } from '~/i18n/routing';

import { LocaleLink } from './_components/locale-link';

Expand Down
2 changes: 1 addition & 1 deletion core/channels.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type LocaleType } from './i18n';
import { type LocaleType } from './i18n/routing';

export type RecordFromLocales = {
[K in LocaleType]: string;
Expand Down
2 changes: 1 addition & 1 deletion core/components/header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ReactNode, Suspense } from 'react';
import { getSessionCustomerId } from '~/auth';
import { FragmentOf } from '~/client/graphql';
import { logoTransformer } from '~/data-transformers/logo-transformer';
import { localeLanguageRegionMap } from '~/i18n';
import { localeLanguageRegionMap } from '~/i18n/routing';

import { Link } from '../link';
import { Button } from '../ui/button';
Expand Down
2 changes: 1 addition & 1 deletion core/components/link/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ComponentPropsWithRef, ElementRef, forwardRef, useReducer } from 'react

import { cn } from '~/lib/utils';

import { Link as NavLink, useRouter } from '../../routing';
import { Link as NavLink, useRouter } from '../../i18n/routing';

type NextLinkProps = Omit<ComponentPropsWithRef<typeof NavLink>, 'prefetch'>;

Expand Down
2 changes: 1 addition & 1 deletion core/components/ui/compare-drawer/compare-drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useTranslations } from 'next-intl';

import { BcImage } from '~/components/bc-image';
import { Link as CustomLink } from '~/components/link';
import { usePathname } from '~/routing';
import { usePathname } from '~/i18n/routing';

import { Button } from '../button';

Expand Down
2 changes: 1 addition & 1 deletion core/components/ui/footer/locale.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getLocale } from 'next-intl/server';

import { localeLanguageRegionMap, locales } from '~/i18n';
import { localeLanguageRegionMap, locales } from '~/i18n/routing';

import { Link as CustomLink } from '../../link';

Expand Down
9 changes: 5 additions & 4 deletions core/components/ui/header/locale-switcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import * as PopoverPrimitive from '@radix-ui/react-popover';
import { useTranslations } from 'next-intl';
import { useMemo, useState } from 'react';
import { FormEvent, useMemo, useState } from 'react';

import { LocaleType } from '~/i18n';
import { useRouter } from '~/routing';
import { LocaleType, useRouter } from '~/i18n/routing';

import { Button } from '../button';
import { Select } from '../form';
Expand Down Expand Up @@ -78,7 +77,9 @@ const LocaleSwitcher = ({ activeLocale, locales }: Props) => {
}
};

const handleOnSubmit = () => {
const handleOnSubmit = (event: FormEvent) => {
event.preventDefault();

const newLocale = locales.find(
(locale) => locale.language === languageSelected && locale.region === regionSelected,
);
Expand Down
16 changes: 16 additions & 0 deletions core/i18n/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { notFound } from 'next/navigation';
import { getRequestConfig } from 'next-intl/server';

import { locales, LocaleType } from './routing';

export default getRequestConfig(async ({ locale }) => {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
if (!locales.includes(locale as LocaleType)) {
notFound();
}

return {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
messages: (await import(`../messages/${locale}.json`)).default,
};
});
45 changes: 21 additions & 24 deletions core/i18n.ts → core/i18n/routing.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { notFound } from 'next/navigation';
import { getRequestConfig } from 'next-intl/server';

enum LocalePrefixes {
ALWAYS = 'always',
NEVER = 'never',
ASNEEDED = 'as-needed', // removes prefix on default locale
}
import { createSharedPathnamesNavigation } from 'next-intl/navigation';
import { defineRouting, LocalePrefix } from 'next-intl/routing';

// Enable locales by including them here.
// List includes locales with existing messages support.
const locales = [
export const locales = [
'en',
// 'da',
// 'es-419',
Expand All @@ -32,6 +26,8 @@ const locales = [
// 'sv',
] as const;

export type LocaleType = (typeof locales)[number];

interface LocaleEntry {
id: LocaleType;
language: string;
Expand Down Expand Up @@ -66,25 +62,26 @@ export const localeLanguageRegionMap: LocaleEntry[] = [
// { id: 'sv', language: 'Svenska', region: 'Sverige', flag: '🇸🇪' },
];

type LocalePrefixesType = `${LocalePrefixes}`;
enum LocalePrefixes {
ALWAYS = 'always',
NEVER = 'never',
ASNEEDED = 'as-needed', // removes prefix on default locale
}

// Temporary we use NEVER prefix to prioritize accept-language header
// & disable internationalized routes due to incomplete multilingual implementation
const localePrefix: LocalePrefixesType = LocalePrefixes.NEVER;
const defaultLocale = 'en';

type LocaleType = (typeof locales)[number];

export default getRequestConfig(async (params) => {
let lang = params.locale as LocaleType; // eslint-disable-line
export const localePrefix: LocalePrefix = LocalePrefixes.NEVER;

if (!locales.includes(lang)) notFound();
export const defaultLocale = 'en';

return {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
messages: (await import(`./messages/${lang}`)).default,
};
export const routing = defineRouting({
locales,
defaultLocale,
localePrefix: {
mode: localePrefix,
},
});

export { LocalePrefixes, locales, localePrefix, defaultLocale };
export type { LocaleType };
// Lightweight wrappers around Next.js' navigation APIs
// that will consider the routing configuration
export const { Link, redirect, usePathname, useRouter } = createSharedPathnamesNavigation(routing);
2 changes: 1 addition & 1 deletion core/middlewares/with-intl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import createMiddleware from 'next-intl/middleware';

import { routing } from '~/routing';
import { routing } from '~/i18n/routing';

import { type MiddlewareFactory } from './compose-middlewares';

Expand Down
2 changes: 1 addition & 1 deletion core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"lucide-react": "^0.436.0",
"next": "14.2.6",
"next-auth": "5.0.0-beta.20",
"next-intl": "^3.18.1",
"next-intl": "^3.19.0",
"react": "^18.3.1",
"react-day-picker": "^8.10.0",
"react-dom": "^18.3.1",
Expand Down
12 changes: 0 additions & 12 deletions core/routing.ts

This file was deleted.

2 changes: 1 addition & 1 deletion integrations/makeswift/integration.patch
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ index 71d40507..9179455c 100644

-export default function CatchAllPage() {
- notFound();
+import { locales } from '~/i18n';
+import { locales } from '~/i18n/routing';
+import { client } from '~/lib/makeswift/client';
+import { MakeswiftProvider } from '~/lib/makeswift/provider';
+
Expand Down
6 changes: 3 additions & 3 deletions integrations/strapi-rest/integration.patch
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ index 3edd5ac4..4b7c2c40 100644

import { BcImage } from '~/components/bc-image';
import { Link } from '~/components/link';
@@ -9,35 +10,32 @@ import { LocaleType } from '~/i18n';
@@ -9,35 +10,32 @@ import { LocaleType } from '~/i18n/routing';

import { SharingLinks } from './_components/sharing-links';
import { getBlogPageData } from './page-data';
Expand Down Expand Up @@ -261,7 +261,7 @@ index 00000000..6d2a5579
+import { BlocksRenderer, type BlocksContent } from '@strapi/blocks-react-renderer';
+import { unstable_setRequestLocale } from 'next-intl/server';
+import { Link } from '~/components/link';
+import { LocaleType } from '~/i18n';
+import { LocaleType } from '~/i18n/routing';
+import { fetchStrapiData } from '~/lib/strapi/data-fetcher';
+
+interface Props {
Expand Down Expand Up @@ -352,7 +352,7 @@ index 00000000..54a634d2
@@ -0,0 +1,71 @@
+import { unstable_setRequestLocale } from 'next-intl/server';
+import { Link } from '~/components/link';
+import { LocaleType } from '~/i18n';
+import { LocaleType } from '~/i18n/routing';
+import { fetchStrapiData } from '~/lib/strapi/data-fetcher';
+
+interface Props {
Expand Down
Loading

0 comments on commit e2c0153

Please sign in to comment.