Skip to content

Commit 721cd39

Browse files
committed
added country names to locales
1 parent 2a5d98b commit 721cd39

File tree

5 files changed

+16
-26
lines changed

5 files changed

+16
-26
lines changed

app/[lang]/_components/constants.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const chomskyFont = LocalFont(
2828
},
2929
);
3030

31-
export type Language = "en" | "fr";
31+
export type Language = "en-US" | "fr-FR";
3232

3333
export interface LanguageProps {
3434
language: Language;

app/[lang]/_lib/dictionary.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import getFR from "@/app/[lang]/_lib/dictionaries/fr";
55

66
export const getDictionary = (locale: Locale) => {
77
switch (locale) {
8-
case 'en':
8+
case 'en-US':
99
return getEN();
10-
case 'fr':
10+
case 'fr-FR':
1111
return getFR();
1212

1313
default:

app/[lang]/page.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export default function Home({
1010
params
1111
}: { params: { lang: Language }
1212
}) {
13-
if (params.lang !== 'en' && params.lang !== 'fr') {
14-
params.lang = 'en';
13+
if (params.lang !== 'en-US' && params.lang !== 'fr-FR') {
14+
params.lang = 'en-US';
1515
}
1616
return (
1717
<div className="flex flex-col gt-lg:justify-center w-screen h-screen">

i18n.config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export const i18n = {
2-
defaultLocale: 'en',
3-
locales: ['en', 'fr']
2+
defaultLocale: 'fr-FR',
3+
locales: ['en-US', 'fr-FR']
44
} as const
55

66
export type Locale = (typeof i18n)['locales'][number]

middleware.ts

+9-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type {NextRequest} from 'next/server'
22
import {NextResponse} from 'next/server'
33

4-
import {i18n} from '@/i18n.config'
4+
import {i18n, Locale} from '@/i18n.config'
55

66
import {match as matchLocale} from '@formatjs/intl-localematcher'
77
import Negotiator from "negotiator";
@@ -27,31 +27,21 @@ export function middleware(request: NextRequest) {
2727
locale => !pathname.startsWith(`/${locale}`) && pathname !== `/${locale}`
2828
)
2929

30-
// console.log("request.url ", request);
30+
console.log("request.url ", request.url);
3131

3232
// console.log("pathnameIsMissingLocale ", pathnameIsMissingLocale);
3333
// console.log("pathname ", pathname);
3434

3535
// Redirect if there is no locale
3636
if (pathnameIsMissingLocale) {
37-
const locale = getLocale(request);
38-
if (locale === 'fr') {
39-
console.log("redirecting " + pathname + " to " + `/${locale}${pathname.startsWith('/') ? '' : '/'}${pathname}`);
40-
return NextResponse.redirect(
41-
new URL(
42-
`/${locale}${pathname.startsWith('/') ? '' : '/'}${pathname}`,
43-
request.url
44-
)
37+
const locale = getLocale(request) as Locale;
38+
console.log("redirecting " + pathname + " to " + `/${locale}${pathname.startsWith('/') ? '' : '/'}${pathname}`);
39+
return NextResponse.redirect(
40+
new URL(
41+
`/${locale}${pathname.startsWith('/') ? '' : '/'}${pathname}`,
42+
request.url
4543
)
46-
} else {
47-
//console.log("rewriting " + pathname + " to " + `/en${pathname.startsWith('/') ? '' : '/'}${pathname}`);
48-
return NextResponse.redirect(
49-
new URL(
50-
`/fr${pathname.startsWith('/') ? '' : '/'}${pathname}`,
51-
request.url
52-
)
53-
)
54-
}
44+
)
5545
}
5646
}
5747

0 commit comments

Comments
 (0)