Skip to content

Commit b7acff6

Browse files
committed
update locale
1 parent 303c382 commit b7acff6

File tree

11 files changed

+78
-99
lines changed

11 files changed

+78
-99
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ APP_CMD=start
8787

8888
- On first startup, you may encounter an error indicating that `external volume "xc2f_pgdata" not found`. You can resolve this by commenting out the `volumes.pgdata.external` and `volumes.pgdata.name` lines in `docker-compose.yml`. Or you can create the xc2f_pgdata volume by running `docker volume create xc2f_pgdata`.
8989
- On first startup or after new table structures are added, enter the payload container to run `pnpm payload migrate:create` and `pnpm payload migrate`.
90+
- If the API returns a 404, check if the URL is being processed by locale middleware. Exclude the API prefix in `src/middleware.ts`.
9091

9192
## Commands
9293

src/Footer/Locale.tsx

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,21 @@ import {
77
SelectTrigger,
88
SelectValue,
99
} from '@/components/ui/select'
10-
import React, { useState } from 'react'
10+
import React from 'react'
11+
import { useLocale } from 'next-intl'
1112

12-
type Language = 'zh' | 'en'
13-
14-
const defaultLocale = 'zh'
15-
const LOCALE_COOKIE = 'locale'
13+
type Language = 'en' | 'zh'
1614

1715
export const LanguageSelector: React.FC = () => {
18-
const [value, setValue] = useState(defaultLocale)
16+
const locale = useLocale()
1917

20-
const onlanguageChange = (language: Language) => {
21-
document.cookie = `${LOCALE_COOKIE}=${language || defaultLocale}; path=/; max-age=${60 * 60 * 24 * 365};`
22-
setValue(language)
23-
// TODO: 查 locale slug
18+
const onlanguageChange = (nextLocal: Language) => {
2419
const { pathname, search } = window.location
25-
window.location.href = `/${language}${pathname.replace(/^\/[a-z]+(\/|$)/, '/')}${search}`
20+
window.location.href = `/${nextLocal}${pathname.replace(/^\/[a-z]+(\/|$)/, '/')}${search}`
2621
}
2722

28-
React.useEffect(() => {
29-
const cookieLocale = document.cookie
30-
.split('; ')
31-
.find((row) => row.startsWith('locale='))
32-
?.split('=')[1] as Language | undefined
33-
if (cookieLocale === 'zh' || cookieLocale === 'en') {
34-
setValue(cookieLocale)
35-
}
36-
}, [])
37-
3823
return (
39-
<Select onValueChange={onlanguageChange} value={value}>
24+
<Select onValueChange={onlanguageChange} value={locale}>
4025
<SelectTrigger
4126
aria-label="Select a language"
4227
className="w-auto bg-transparent gap-2 pl-0 md:pl-3 border-none"

src/app/(frontend)/[locale]/layout.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,22 @@ import { draftMode } from 'next/headers'
1616
import { getServerSideURL } from '@/utilities/getURL'
1717
import { LogoFont } from '../fonts'
1818

19-
export default async function RootLayout({
19+
import { Locale } from '@/i18n/types'
20+
import { setRequestLocale } from 'next-intl/server'
21+
22+
export default async function LocaleLayout({
2023
children,
2124
params,
2225
}: {
2326
children: React.ReactNode
24-
params: Promise<{ locale: string }>
27+
params: Promise<{ locale: Locale }>
2528
}) {
2629
const { isEnabled } = await draftMode()
2730
const { locale } = await params
2831

32+
// Enable static rendering
33+
setRequestLocale(locale)
34+
2935
return (
3036
<html
3137
className={cn(GeistSans.variable, GeistMono.variable, LogoFont.variable)}

src/app/(frontend)/[locale]/page.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import { generateMeta } from '@/utilities/generateMeta'
1212
import PageClient from './page.client'
1313
import { LivePreviewListener } from '@/components/LivePreviewListener'
1414

15+
import { Locale } from '@/i18n/types'
16+
import { routing } from '@/i18n/routing'
17+
import { setRequestLocale } from 'next-intl/server'
18+
1519
export async function generateStaticParams() {
1620
const payload = await getPayload({ config: configPromise })
1721
const pages = await payload.find({
@@ -25,22 +29,30 @@ export async function generateStaticParams() {
2529
},
2630
})
2731

28-
const params = pages.docs.map(({ slug }) => {
29-
return { slug }
30-
})
32+
const params = []
33+
for (const locale of routing.locales) {
34+
for (const { slug } of pages.docs) {
35+
params.push({ slug, locale })
36+
}
37+
}
3138

3239
return params
3340
}
3441

3542
type Args = {
3643
params: Promise<{
3744
slug?: string
45+
locale: Locale
3846
}>
3947
}
4048

4149
export default async function Page({ params: paramsPromise }: Args) {
4250
const { isEnabled: draft } = await draftMode()
43-
const { slug = 'home' } = await paramsPromise
51+
const { slug = 'home', locale } = await paramsPromise
52+
53+
// Enable static rendering
54+
setRequestLocale(locale)
55+
4456
// Decode to support slugs with special characters
4557
const decodedSlug = decodeURIComponent(slug)
4658
const url = '/' + decodedSlug

src/app/(frontend)/[locale]/posts/page.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,19 @@ import { getPayload } from 'payload'
88
import React from 'react'
99
import PageClient from './page.client'
1010

11-
export const dynamic = 'force-static'
12-
export const revalidate = 600
11+
import { Locale } from '@/i18n/types'
12+
import { routing } from '@/i18n/routing'
13+
import { setRequestLocale } from 'next-intl/server'
14+
15+
export async function generateStaticParams() {
16+
return routing.locales.map((locale) => ({ locale }))
17+
}
18+
19+
export default async function Page({ params }: { params: Promise<{ locale: Locale }> }) {
20+
const { locale } = await params
21+
// Enable static rendering
22+
setRequestLocale(locale)
1323

14-
export default async function Page() {
1524
const payload = await getPayload({ config: configPromise })
1625

1726
const posts = await payload.find({

src/collections/Posts/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,7 @@ export const Posts: CollectionConfig<'posts'> = {
216216
},
217217
],
218218
},
219-
slugField({
220-
localized: true,
221-
}),
219+
slugField(),
222220
{
223221
name: 'githubDiscussionUrl',
224222
type: 'text',

src/i18n/navigation.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { createNavigation } from 'next-intl/navigation'
2+
import { routing } from './routing'
3+
4+
// Lightweight wrappers around Next.js' navigation
5+
// APIs that consider the routing configuration
6+
export const { Link, redirect, usePathname, useRouter, getPathname } = createNavigation(routing)

src/i18n/request.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { cookies } from 'next/headers'
21
import { getRequestConfig } from 'next-intl/server'
2+
import { hasLocale } from 'next-intl'
3+
import { routing } from './routing'
34

4-
export default getRequestConfig(async () => {
5-
const store = await cookies()
6-
const locale = store.get('locale')?.value || 'zh'
5+
export default getRequestConfig(async ({ requestLocale }) => {
6+
// Typically corresponds to the `[locale]` segment
7+
const requested = await requestLocale
8+
const locale = hasLocale(routing.locales, requested) ? requested : routing.defaultLocale
79

810
return {
911
locale,

src/i18n/routing.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineRouting } from 'next-intl/routing'
2+
3+
export const routing = defineRouting({
4+
// A list of all locales that are supported
5+
locales: ['zh', 'en'],
6+
7+
// Used when no locale matches
8+
defaultLocale: 'en',
9+
})

src/i18n/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type Locale = 'en' | 'zh'

0 commit comments

Comments
 (0)