Skip to content

Commit 464b4fd

Browse files
committedJun 13, 2024·
fix: updated i18n handling
1 parent a0dd45d commit 464b4fd

File tree

9 files changed

+59
-93
lines changed

9 files changed

+59
-93
lines changed
 

‎src/app/[locale]/_components/LocaleToggle.tsx

-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ const LocaleToggle = () => {
2323
}
2424
}, [iconToggle])
2525

26-
console.log(locale === 'ko')
27-
2826
return (
2927
<button
3028
aria-label="Theme Toggle"

‎src/app/[locale]/page.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface Params {
1313
}
1414

1515
const Home = async ({ params }: Params) => {
16-
const dictionary = await getDictionary(params.locale, 'navigation')
16+
const dictionary = await getDictionary(params.locale)
1717
return (
1818
<main className={inter.variable}>
1919
<DictionaryProvider dictionary={dictionary}>

‎src/app/[locale]/posts/layout.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ interface Params {
1717
}
1818

1919
const PostsLayouts = async ({ children, params }: Params) => {
20-
const t = await getDictionary(params.locale, 'navigation')
20+
const t = await getDictionary(params.locale)
21+
console.log(t)
2122

2223
const navOption: Array<{ label: string; href: string; locale: Locale; external?: boolean }> = [
2324
{ label: t.navigation.home.title, href: '/', locale: params.locale },

‎src/dictionaries/en.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"navigation": {
3+
"home": {
4+
"title": "Home",
5+
"description": ""
6+
},
7+
"posts": {
8+
"title": "Posts",
9+
"description": "Check out my dev blog posts"
10+
},
11+
"about": {
12+
"title": "About",
13+
"description": ""
14+
},
15+
"github": {
16+
"title": "Github",
17+
"description": "Head over to my github!"
18+
},
19+
"chat": {
20+
"title": "Chat Bot",
21+
"description": "Chat Bot Coming Soon...!"
22+
}
23+
}
24+
}

‎src/dictionaries/en/navigation.json

-24
This file was deleted.

‎src/dictionaries/ko.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"navigation": {
3+
"home": {
4+
"title": "",
5+
"description": ""
6+
},
7+
"posts": {
8+
"title": "포스트",
9+
"description": "기술 블로그 포스트 보러가기"
10+
},
11+
"about": {
12+
"title": "소개",
13+
"description": ""
14+
},
15+
"github": {
16+
"title": "Github",
17+
"description": "깃허브 페이지로 이동"
18+
},
19+
"chat": {
20+
"title": "챗봇",
21+
"description": "준비중입니다...!"
22+
}
23+
}
24+
}

‎src/dictionaries/ko/navigation.json

-24
This file was deleted.

‎src/lib/dictionaries.ts

+5-40
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,9 @@
11
import 'server-only'
22
import type { Locale } from '@/i18n.config'
33

4-
const dictionaries = (() => {
5-
let obj: { [key: string]: { [key: string]: () => Promise<any> } } = {}
6-
for (let locale of ['en', 'ko']) {
7-
obj[locale] = {
8-
navigation: async () => import(`src/dictionaries/${locale}/navigation.json`).then((module) => module.default)
9-
}
10-
}
11-
return obj
12-
})()
13-
// const dictionaries = {
14-
// en: () => import('@/dictionaries/en.json').then((module) => module.default),
15-
// ko: () => import('@/dictionaries/ko.json').then((module) => module.default)
16-
// }
17-
18-
export const getDictionary = async (locale: Locale, key?: string | string[]) => {
19-
switch (typeof key) {
20-
case 'string': {
21-
return dictionaries[locale ?? 'en'][key]()
22-
}
23-
case 'object': {
24-
if (Array.isArray(key)) {
25-
let obj: { [key: string]: Promise<any> } = {}
26-
for (let k of key) {
27-
console.log(locale)
28-
if (dictionaries[locale ?? 'en'][k]) {
29-
{
30-
obj[k] = await dictionaries[locale ?? 'en'][k]()
31-
}
32-
}
33-
}
34-
35-
return obj
36-
}
37-
}
38-
case 'undefined':
39-
default: {
40-
return dictionaries[locale ?? 'en']
41-
}
42-
}
4+
const dictionaries = {
5+
en: () => import('@/dictionaries/en.json').then((module) => module.default),
6+
ko: () => import('@/dictionaries/ko.json').then((module) => module.default)
437
}
44-
// export const getDictionary = async (locale: Locale) => dictionaries[locale]()
8+
9+
export const getDictionary = async (locale: Locale) => dictionaries[locale]()

‎src/middleware.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ export function middleware(request: NextRequest) {
2626

2727
// Redirect if there is no locale
2828
if (pathnameIsMissingLocale) {
29-
const locale = getLocale(request)
29+
// const locale = getLocale(request)
30+
31+
const locale = i18n.defaultLocale
3032

3133
if (locale === i18n.defaultLocale) {
3234
return NextResponse.rewrite(new URL(`/${locale}${pathname.startsWith('/') ? '' : '/'}${pathname}`, request.url))

0 commit comments

Comments
 (0)
Please sign in to comment.