-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Make i18n languages fully configurable via docs.site.json #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a95e618
eda65d6
95a79d7
1cef231
b656a36
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,9 +2,60 @@ import fs from 'node:fs'; | |||||||||||||||||||||||||
| import path from 'node:path'; | ||||||||||||||||||||||||||
| import OpenAI from 'openai'; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Supported language suffixes for i18n | ||||||||||||||||||||||||||
| const LANGUAGE_SUFFIXES = ['.cn.mdx', '.en.mdx']; | ||||||||||||||||||||||||||
| const TARGET_LANGUAGE_SUFFIX = '.cn.mdx'; | ||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * Load site configuration from docs.site.json | ||||||||||||||||||||||||||
| * @returns {object} - The site configuration | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| function loadSiteConfig() { | ||||||||||||||||||||||||||
| const configPath = path.resolve(process.cwd(), 'content/docs.site.json'); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (!fs.existsSync(configPath)) { | ||||||||||||||||||||||||||
| console.warn(`Warning: docs.site.json not found at ${configPath}, using defaults`); | ||||||||||||||||||||||||||
| // Fallback matches the default configuration in packages/site/lib/site-config.ts | ||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||
| i18n: { | ||||||||||||||||||||||||||
| enabled: true, | ||||||||||||||||||||||||||
| defaultLanguage: 'en', | ||||||||||||||||||||||||||
| languages: ['en', 'cn'] | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||
| const configContent = fs.readFileSync(configPath, 'utf-8'); | ||||||||||||||||||||||||||
| return JSON.parse(configContent); | ||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||
| console.error('Error loading docs.site.json:', error); | ||||||||||||||||||||||||||
| throw error; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Load configuration | ||||||||||||||||||||||||||
| const siteConfig = loadSiteConfig(); | ||||||||||||||||||||||||||
| const languages = siteConfig.i18n?.languages || ['en', 'cn']; | ||||||||||||||||||||||||||
| const defaultLanguage = siteConfig.i18n?.defaultLanguage || 'en'; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Generate language suffixes dynamically from config | ||||||||||||||||||||||||||
| // e.g., ['en', 'cn'] -> ['.en.mdx', '.cn.mdx'] | ||||||||||||||||||||||||||
| const LANGUAGE_SUFFIXES = languages.map(lang => `.${lang}.mdx`); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Target language is the first non-default language | ||||||||||||||||||||||||||
| const targetLanguage = languages.find(lang => lang !== defaultLanguage) || languages[0]; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| const targetLanguage = languages.find(lang => lang !== defaultLanguage) || languages[0]; | |
| const nonDefaultLanguages = languages.filter(lang => lang !== defaultLanguage); | |
| if (nonDefaultLanguages.length === 0) { | |
| throw new Error( | |
| `Invalid i18n configuration: languages (${JSON.stringify( | |
| languages | |
| )}) must include at least one language different from defaultLanguage ("${defaultLanguage}").` | |
| ); | |
| } | |
| const targetLanguage = nonDefaultLanguages[0]; |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,102 @@ | ||||||
| import { siteConfig } from './site-config'; | ||||||
|
|
||||||
| /** | ||||||
| * Translation strings for different languages | ||||||
| * These are the UI strings used by fumadocs-ui | ||||||
| */ | ||||||
| interface LanguageTranslations { | ||||||
| displayName: string; | ||||||
| toc?: string; | ||||||
| search?: string; | ||||||
| lastUpdate?: string; | ||||||
| searchNoResult?: string; | ||||||
| previousPage?: string; | ||||||
| nextPage?: string; | ||||||
| chooseLanguage?: string; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Default translations for supported languages | ||||||
| * Add new language translations here as needed | ||||||
| */ | ||||||
| const defaultTranslations: Record<string, LanguageTranslations> = { | ||||||
| en: { | ||||||
| displayName: 'English', | ||||||
| }, | ||||||
| cn: { | ||||||
|
||||||
| cn: { | |
| zh-CN: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The config path is hardcoded to 'content/docs.site.json'. Consider making this configurable or using a constant to allow flexibility for different project structures.