VitePress Integration
Relevant source files
The following files were used as context for generating this wiki page:
VitePress Integration provides internationalization (i18n) capabilities for VitePress documentation sites. It enables runtime dictionaries, localized routing, automated SEO metadata, and language switching via the @i18n-micro/vitepress package. The integration consists of a configuration wrapper, a Vite plugin for virtual modules, and runtime theme helpers.
Sources: packages/vitepress/README.md:1-5, AGENTS.md:14, packages/vitepress/package.json:2-3
Architecture and Core Components
The integration utilizes a multi-layered approach to inject i18n logic into the VitePress build and runtime environments.
The withI18n Configuration Wrapper
The withI18n function wraps the VitePress user configuration. It automatically injects required settings, including:
- Routing Logic: Injects
themeConfig.i18nRoutingto handle locale-based path transformations. - SEO Metadata: Adds a
transformHeadhook to generate canonical links andhreflangtags when ametaBaseUrlis provided. - Vite Plugins: Registers a dedicated Vite plugin (
vite-plugin-i18n-vitepress) to handle virtual modules.
Sources: packages/vitepress/tests/with-i18n.test.ts:14-40, packages/vitepress/README.md:46-60
Virtual Modules
The system uses virtual modules to bridge build-time configuration with the client-side runtime.
virtual:i18n-micro/config: Exports the resolved i18n configuration, including thedefaultLocaleand locale mappings.virtual:i18n-micro/messages: Exports both global translations and page-scoped dictionaries.
Sources: packages/vitepress/tests/with-i18n.test.ts:98-115
Data Flow Diagram
This diagram shows how the withI18n wrapper initiates the flow from build configuration to the runtime i18n instance.
Sources: packages/vitepress/tests/with-i18n.test.ts:98-120, packages/vitepress/README.md:13-25
Localized Routing and Link Resolution
VitePress Integration manages how URLs are resolved and localized across different languages.
Link Resolution Logic
The system supports VitePress cleanUrls. Links are resolved by checking multiple candidates:
- A path like
/newsresolves tonews.mdif it exists. - If
news.mdis missing, it resolves tonews/index.md. - Paths ending with a slash (e.g.,
/news/) strictly resolve toindex.mdwithin that directory.
Sources: scripts/src/commands/docs-audit.ts:70-82
Localized Paths
The createI18n instance provides methods for path manipulation:
localizePath(path, locale): Prefixes the path with the specified locale (e.g.,/guidebecomes/fr/guide).switchLocalePath(path, targetLocale): Transitions an existing localized path to another locale.
Sources: packages/vitepress/README.md:23, packages/vitepress/tests/with-i18n.test.ts:167-175
Dynamic Routes
The integration supports dynamic route templates using the [param].md syntax. The auditor recognizes templates like api/packages/[pkg].md and allows links to resolve to these virtual paths even if a specific .md file does not exist on disk for every parameter value.
Sources: scripts/src/commands/docs-audit.ts:85-102, scripts/test/docs-generate.test.ts:41-50
SEO and Metadata
The integration automatically manages SEO tags to ensure localized pages are correctly indexed by search engines.
| Meta Tag | Function | Trigger Condition |
|---|---|---|
rel="canonical" | Defines the primary URL for a page. | metaBaseUrl is configured. |
hreflang | Signals alternative language versions of the page, including x-default. | Multiple locales are defined. |
og:locale | Sets the OpenGraph locale for social sharing. | og field exists in locale config. |
Sources: packages/vitepress/tests/with-i18n.test.ts:46-65, packages/vitepress/tests/playground-ssg.test.ts:54-65
SEO Injection Sequence
The sequence shows the decision logic for injecting SEO tags into the document head during the build process.
Sources: packages/vitepress/tests/with-i18n.test.ts:68-88, packages/vitepress/tests/playground-ssg.test.ts:68-76
Integration Procedures
Configuration Setup
- Import
withI18nandbuildVitePressLocalesfrom@i18n-micro/vitepress/config. - Define your locales array with
code,iso, and optionalogproperties. - Wrap your VitePress configuration object with
withI18n. - Provide i18n options including
locale,defaultLocale, andtranslationDir.
Sources: packages/vitepress/README.md:46-64
Theme Setup
To enable i18n in the VitePress theme:
- Import
defineI18nThemefrom@i18n-micro/vitepress/theme. - Pass the
DefaultTheme(or a custom theme) todefineI18nTheme. - Use the
useI18n()composable in Vue components to accesslocalePath,switchLocale, or the$thelper.
Sources: packages/vitepress/README.md:68-74
Implementation Details
Dependency Management
The @i18n-micro/vitepress package relies on other internal workspace packages to handle core logic:
@i18n-micro/core: Translation engine and base i18n class.@i18n-micro/node: File system operations and dictionary loading for SSG.@i18n-micro/vue: Vue-specific bindings and components.
Sources: packages/vitepress/package.json:76-80, packages/vitepress/README.md:31-40
Dictionary Loading
The integration handles two types of dictionaries:
- Global Messages: Loaded from the
translationDir(e.g.,locales/en.json). - Page-Scoped Messages: Loaded from subdirectories matching the page structure (e.g.,
locales/pages/guide/demo/en.json).
Sources: packages/vitepress/tests/playground-ssg.test.ts:28-44, packages/vitepress/tests/with-i18n.test.ts:133-145