Skip to content

Commit

Permalink
refactoring after fix conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
bc-alexsaiannyi committed Feb 1, 2024
1 parent 9262cbe commit 977e3d0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions apps/core/app/[locale]/(default)/product/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Suspense } from 'react';

import { getProduct } from '~/client/queries/getProduct';
import { ProductForm } from '~/components/ProductForm';
import { LocaleType } from '~/i18n';

import { BreadCrumbs } from './_components/Breadcrumbs';
import { Gallery } from './_components/Gallery';
Expand Down
2 changes: 2 additions & 0 deletions apps/core/components/NavLink/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ type NavLinkType = Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, keyof Nav
children?: React.ReactNode;
} & React.RefAttributes<HTMLAnchorElement>;

// This component wraps next/link and automatically prefixes the href with the current locale as necessary.
// https://next-intl-docs.vercel.app/docs/routing/navigation#link
export const NavLink = forwardRef<ElementRef<'a'>, NavLinkType>(
({ href, prefetch = false, children, ...rest }, ref) => {
return (
Expand Down
17 changes: 13 additions & 4 deletions apps/core/middlewares/with-i18n.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
import { match as matchLocale } from '@formatjs/intl-localematcher';
import Negotiator from 'negotiator';
import { cookies } from 'next/headers';
import { NextRequest } from 'next/server';
import createMiddleware from 'next-intl/middleware';

import { defaultLocale, localePrefix, locales, LocaleType } from '../i18n';
import { defaultLocale, localePrefix, LocalePrefixes, locales, LocaleType } from '../i18n';

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

// we use negotiator and intl-localematcher to get best locale
const getLocale = (request: NextRequest): LocaleType | undefined => {
const negotiatorHeaders: Record<string, string> = {};

request.headers.forEach((value, key) => (negotiatorHeaders[key] = value));

// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const supportedLocales = locales as unknown as string[];

// Use negotiator and intl-localematcher to get best locale
const languages = new Negotiator({ headers: negotiatorHeaders }).languages(supportedLocales);

const locale = matchLocale(languages, supportedLocales, defaultLocale);

// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
return locale as LocaleType;
};

const updateLocaleCookie = (currentLocale: string, request: NextRequest) => {
const nextLocale = cookies().get('NEXT_LOCALE')?.value;

if (localePrefix === LocalePrefixes.NEVER && nextLocale !== currentLocale) {
request.cookies.set('NEXT_LOCALE', currentLocale);
}
};

export const withI18n: MiddlewareFactory = () => {
return (request: NextRequest) => {
const locale = getLocale(request) ?? defaultLocale;

updateLocaleCookie(locale, request);

const intlMiddleware = createMiddleware({
locales,
localePrefix,
Expand Down
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 977e3d0

Please sign in to comment.