Skip to content

Commit 10848bb

Browse files
ericyangpanclaude
andcommitted
refactor(build): implement DRY principle for locale configuration
Eliminate hardcoded locale arrays throughout the codebase by using a single source of truth from src/i18n/config.ts: - Update metadata/config.ts to import locales from i18n/config - Update articles/[slug]/page.tsx to dynamically import locales - Update docs/[slug]/page.tsx to dynamically import locales - Remove 3 hardcoded SUPPORTED_LOCALES arrays This ensures adding a new language only requires changing one file. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 80daf85 commit 10848bb

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

src/app/[locale]/articles/[slug]/page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ import Link from 'next/link';
22
import { notFound } from 'next/navigation';
33
import Header from '@/components/Header';
44
import Footer from '@/components/Footer';
5-
import { getArticles, getArticleBySlug, getArticleComponents } from '@/lib/articles';
5+
import { getArticles, getArticleBySlug, getArticleComponents } from '@/lib/generated/articles';
66
import { generateArticleMetadata } from '@/lib/metadata';
77

88
type Props = {
99
params: Promise<{ slug: string; locale: string }>;
1010
};
1111

1212
export async function generateStaticParams() {
13-
const SUPPORTED_LOCALES = ['en', 'zh-Hans'];
13+
const { locales } = await import('@/i18n/config');
1414
const params: { slug: string; locale: string }[] = [];
1515

16-
for (const locale of SUPPORTED_LOCALES) {
16+
for (const locale of locales) {
1717
const articles = getArticles(locale);
1818
for (const article of articles) {
1919
params.push({

src/app/[locale]/docs/[slug]/page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ import { notFound } from 'next/navigation';
33
import Header from '@/components/Header';
44
import Footer from '@/components/Footer';
55
import DocsSidebar from '@/components/sidebar/DocsSidebar';
6-
import { getDocSections, getDocBySlug, getDocComponents } from '@/lib/docs';
6+
import { getDocSections, getDocBySlug, getDocComponents } from '@/lib/generated/docs';
77
import { generateDocsMetadata } from '@/lib/metadata';
88

99
type Props = {
1010
params: Promise<{ locale: string; slug: string }>;
1111
};
1212

1313
export async function generateStaticParams() {
14-
const SUPPORTED_LOCALES = ['en', 'zh-Hans'];
14+
const { locales } = await import('@/i18n/config');
1515
const staticParams: Array<{ locale: string; slug: string }> = [];
1616

17-
for (const locale of SUPPORTED_LOCALES) {
17+
for (const locale of locales) {
1818
const docSections = getDocSections(locale);
1919
for (const doc of docSections) {
2020
staticParams.push({ locale, slug: doc.slug });

src/lib/metadata/config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Centralized configuration for site-wide metadata constants and category-specific SEO keywords
44
*/
55

6+
import { locales, defaultLocale } from '@/i18n/config';
7+
68
export const SITE_CONFIG = {
79
name: 'AI Coding Stack',
810
url: 'https://aicodingstack.io',
@@ -11,8 +13,8 @@ export const SITE_CONFIG = {
1113
site: '@aicodingstack',
1214
creator: '@aicodingstack',
1315
},
14-
defaultLocale: 'en' as const,
15-
supportedLocales: ['en', 'zh-Hans'] as const,
16+
defaultLocale,
17+
supportedLocales: locales,
1618
} as const;
1719

1820
export const OG_IMAGE_CONFIG = {

0 commit comments

Comments
 (0)