Skip to content

Commit

Permalink
refactor: refactor i18nConfig and locale logic in middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitb35 committed Mar 13, 2024
1 parent cbf739b commit 031b5a0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion i18n-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
export const i18nConfig = {
defaultLocale: 'en',
locales: ['en', 'de', 'cs', 'es', 'fr', 'it', 'pt-BR'],
} as const;
};

export type Locale = (typeof i18nConfig)['locales'][number];
14 changes: 8 additions & 6 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { NextRequest, NextResponse } from 'next/server';
import { getTenantSlug } from './src/utils/multiTenancy/helpers';
import { match as matchLocale } from '@formatjs/intl-localematcher';
import Negotiator from 'negotiator';
import { i18nConfig, Locale } from './i18n-config';
import { i18nConfig } from './i18n-config';

function getLocale(request: NextRequest): string | undefined {
// Negotiator expects plain object so we need to transform headers
const negotiatorHeaders: Record<string, string> = {};
request.headers.forEach((value, key) => (negotiatorHeaders[key] = value));

const locales = i18nConfig.locales as unknown as Locale[];
const locales = i18nConfig.locales;

// Use negotiator and intl-localematcher to get best locale
const languages = new Negotiator({ headers: negotiatorHeaders }).languages(
Expand All @@ -19,6 +19,7 @@ function getLocale(request: NextRequest): string | undefined {
const previouslySelectedLanguage = request.cookies.get('NEXT_LOCALE')?.value;
if (
previouslySelectedLanguage !== undefined &&
locales.includes(previouslySelectedLanguage) &&
languages[0] !== previouslySelectedLanguage
) {
languages.unshift(previouslySelectedLanguage);
Expand All @@ -31,7 +32,6 @@ function getLocale(request: NextRequest): string | undefined {

/** Identifies locale in relative url and removes it */
function removeLocaleFromUrl(pathname: string): string {
let newPathname = '';
const localeRegex = /^[a-z]{2}(-[a-z]{2})?$/i;

const splitPathname = pathname.split('/');
Expand All @@ -41,8 +41,7 @@ function removeLocaleFromUrl(pathname: string): string {
const splitFirstSegment = firstSegment.split('-');

if (localeRegex.test(splitFirstSegment[0])) {
newPathname = splitPathname.slice(2).join('/');
return newPathname;
return splitPathname.slice(2).join('/'); //returns the new pathname without the locale
}

return pathname;
Expand Down Expand Up @@ -96,7 +95,10 @@ export default async function middleware(req: NextRequest) {
// store NEXT_LOCALE cookie if available
const localeFromPath = pathname.split('/')[1];
const localeCookieValue = req.cookies.get('NEXT_LOCALE')?.value;
if (localeFromPath !== localeCookieValue) {
if (
i18nConfig.locales.includes(localeFromPath) &&
localeFromPath !== localeCookieValue
) {
res.cookies.set('NEXT_LOCALE', localeFromPath, {
sameSite: 'strict',
maxAge: 31536000, // 1 year
Expand Down

0 comments on commit 031b5a0

Please sign in to comment.