feat: enhance internationalization support with dynamic routing

This commit is contained in:
radishzzz 2025-01-18 02:25:00 +00:00
parent d6c98880d3
commit 8c19d26cfd
12 changed files with 199 additions and 87 deletions

View file

@ -1,5 +1,12 @@
---
import { themeConfig } from '@/config'
import Layout from '@/layouts/Layout.astro'
export function getStaticPaths() {
return themeConfig.global.moreLocale.map(lang => ({
params: { lang },
}))
}
---
<Layout>

View file

@ -1,12 +1,12 @@
---
import { themeConfig } from '@/config'
import Layout from '@/layouts/Layout.astro'
import { getPinnedPosts, getPosts } from '@/utils/content.config'
export function getStaticPaths() {
return [
{ params: { lang: 'zh' } },
{ params: { lang: 'en' } },
]
return themeConfig.global.moreLocale.map(lang => ({
params: { lang },
}))
}
const { lang } = Astro.params

View file

@ -1,13 +1,16 @@
---
import { themeConfig } from '@/config'
import Layout from '@/layouts/Layout.astro'
import { getCollection } from 'astro:content'
export async function getStaticPaths() {
const posts = await getCollection('posts')
return posts.map(post => ({
params: { slug: post.slug },
props: { post },
}))
return themeConfig.global.moreLocale.flatMap(lang =>
posts.map(post => ({
params: { lang, slug: post.slug },
props: { post },
})),
)
}
const { post } = Astro.props

View file

@ -0,0 +1,86 @@
import type { APIContext } from 'astro'
import type { CollectionEntry } from 'astro:content'
import themeConfig from '@/config'
import rss from '@astrojs/rss'
import { getCollection } from 'astro:content'
import MarkdownIt from 'markdown-it'
import sanitizeHtml from 'sanitize-html'
const parser = new MarkdownIt()
const { title, description, url } = themeConfig.site
const { locale: defaultLocale, moreLocale } = themeConfig.global
const followConfig = themeConfig.seo?.follow
// Extract first 100 chars from content as description
function getExcerpt(content: string): string {
const plainText = sanitizeHtml(
parser.render(content),
{
allowedTags: [],
allowedAttributes: {},
},
)
return `${plainText.slice(0, 100).trim()}...`
}
// Return 404 response for invalid language routes
function return404() {
return new Response(null, {
status: 404,
statusText: 'Not found',
})
}
// Add getStaticPaths for dynamic routes
export function getStaticPaths() {
return moreLocale.map(lang => ({ params: { lang } }))
}
// Generate RSS feed for non-default languages
export async function GET({ params }: APIContext) {
const lang = params.lang as string
// Only generate RSS for valid non-default languages
if (!moreLocale.includes(lang)) {
return return404()
}
// Get posts for specific language (include universal posts)
const posts = await getCollection(
'posts',
({ data }: { data: CollectionEntry<'posts'>['data'] }) =>
(!data.draft && (data.lang === lang || data.lang === '')),
)
return rss({
title: `${title} (${lang})`,
description,
site: url,
stylesheet: '/rss/styles.xsl',
// Map posts to RSS items with language-specific URLs
items: posts.map(post => ({
title: post.data.title,
pubDate: post.data.published,
description: post.data.description || getExcerpt(post.body),
link: `/${lang}/posts/${post.slug}/`,
content: sanitizeHtml(
parser.render(post.body),
{
allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img']),
},
),
})),
// Add language and follow challenge info
customData: `
<language>${lang}</language>
${followConfig?.feedID && followConfig?.userID
? `<follow_challenge>
<feedId>${followConfig.feedID}</feedId>
<userId>${followConfig.userID}</userId>
</follow_challenge>`
: ''
}
`.trim(),
})
}

View file

@ -1,15 +1,19 @@
---
import { themeConfig } from '@/config'
import Layout from '@/layouts/Layout.astro'
import { getAllTags, getPostsByTag } from '@/utils/content.config'
export async function getStaticPaths() {
const tags = await getAllTags()
return tags.map(tag => ({
params: { tags: tag },
props: { tags: tag },
}))
return themeConfig.global.moreLocale.flatMap(lang =>
tags.map(tag => ({
params: { lang, tags: tag },
props: { tags: tag },
})),
)
}
const { lang } = Astro.params
const { tags } = Astro.props
const posts = await getPostsByTag(tags)
const allTags = await getAllTags()
@ -18,7 +22,7 @@ const allTags = await getAllTags()
<Layout>
<div>
{allTags.map(tag => (
<a href={`/tags/${tag}/`}>
<a href={`/${lang}/tags/${tag}/`}>
{tag}
</a>
))}
@ -28,7 +32,7 @@ const allTags = await getAllTags()
<ul>
{posts.map(post => (
<li>
<a href={`/posts/${post.slug}/`}>{post.data.title}</a>
<a href={`/${lang}/posts/${post.slug}/`}>{post.data.title}</a>
</li>
))}
</ul>

View file

@ -1,14 +1,22 @@
---
import { themeConfig } from '@/config'
import Layout from '@/layouts/Layout.astro'
import { getAllTags } from '@/utils/content.config'
export function getStaticPaths() {
return themeConfig.global.moreLocale.map(lang => ({
params: { lang },
}))
}
const { lang } = Astro.params
const allTags = await getAllTags()
---
<Layout>
<div>
{allTags.map(tag => (
<a href={`/tags/${tag}/`}>
<a href={`/${lang}/tags/${tag}/`}>
{tag}
</a>
))}

View file

@ -24,11 +24,13 @@ function getExcerpt(content: string): string {
return `${plainText.slice(0, 100).trim()}...`
}
// Generate RSS feed
export async function GET(_context: APIContext) {
// Generate RSS feed for default language
export async function GET() {
// Only handle posts for default language
const posts = await getCollection(
'posts',
({ data }: { data: CollectionEntry<'posts'>['data'] }) => !data.draft,
({ data }: { data: CollectionEntry<'posts'>['data'] }) =>
(!data.draft && (data.lang === locale || data.lang === '')),
)
return rss({
@ -36,6 +38,7 @@ export async function GET(_context: APIContext) {
description,
site: url,
stylesheet: '/rss/styles.xsl',
// Map posts to RSS items
items: posts.map((post: CollectionEntry<'posts'>) => ({
title: post.data.title,
pubDate: post.data.published,
@ -48,13 +51,14 @@ export async function GET(_context: APIContext) {
},
),
})),
// Add language and follow challenge info
customData: `
<language>${locale}</language>
${followConfig?.feedID && followConfig?.userID
? `<follow_challenge>
<feedId>${followConfig.feedID}</feedId>
<userId>${followConfig.userID}</userId>
</follow_challenge>`
<feedId>${followConfig.feedID}</feedId>
<userId>${followConfig.userID}</userId>
</follow_challenge>`
: ''
}
`.trim(),