Server & API Integrations
Relevant source files
The following files were used as context for generating this wiki page:
Server & API Integrations enable server-side translation loading, dynamic locale detection at the edge, and communication with development tools. The module integrates directly with Nitro to manage internationalization context during Server-Side Rendering (SSR) and provides dedicated API endpoints for translation payloads and DevTools management.
Nitro Server Middleware
The global server middleware intercepts every request to initialize the i18n context. It identifies the target locale and loads the required translations before the request reaches the application logic.
Request Processing Flow
When a request enters the Nitro server, the middleware performs the following actions:
- It resolves the module configuration and routing strategy.
- It detects the user's locale using path segments, cookies, or browser headers.
- It initializes the
event.context.i18nobject with the detected locale and loaded translations.
The diagram shows the sequence of operations within the global Nitro middleware to establish the translation context.
Sources: src/runtime/server/middleware/i18n.global.ts:1-25, src/module.ts:602-605
I18n Context Structure
The middleware augments the h3 event context to ensure locale data is available across server-side handlers.
| Field | Type | Description |
|---|---|---|
locale | string | The detected language code for the current request. |
translations | Record<string, unknown> | The pre-merged dictionary for the requested page and locale. |
Sources: src/module.ts:553-563, src/runtime/server/middleware/i18n.global.ts:18-21
Locale Detection Logic
The server utilizes a detection utility to determine the preferred language for a visitor who has not explicitly selected one.
Detection Hierarchy
The system evaluates the following sources in order of priority:
- URL Path: The middleware checks the URL against the configured routing strategy (e.g.,
prefix). - Cookies: If the
localeCookieis configured, the system reads the stored preference. - Headers: The
accept-languageheader is parsed to match browser preferences against available locales. - Default Locale: If no matches are found, the system reverts to the
defaultLocale.
This flowchart illustrates the priority-based logic used to identify the target locale on the server.
Sources: src/runtime/server/utils/locale-detector.ts:6-25, src/runtime/server/utils/locale-server-middleware.ts:10-35
Translation Payload API
The module provides a specialized server route to serve translation JSON files to the client. This allows for asynchronous loading of translations for specific pages to minimize initial bundle sizes.
Endpoint Definition
The API base URL is configurable via apiBaseUrl (defaulting to _locales).
- Route:
/{apiBaseUrl}/:page/:locale/data.json - Method:
GET
The server handler retrieves data using a internal payload source. If a version hash or build date is present, the API includes cache-busting parameters.
Sources: src/runtime/server/routes/i18n.ts:1-20, src/module.ts:503-506
DevTools API Integration
During development, the module registers a Vite plugin that provides administrative API endpoints. These endpoints support the DevTools UI for real-time translation management.
Available DevTools Endpoints
| Endpoint | Method | Purpose |
|---|---|---|
/config | GET | Retrieves current module configuration. |
/files | GET | Scans and lists all available translation JSON files. |
/file | GET | Reads the content of a specific translation file. |
/save | POST | Writes updated translation content back to the filesystem. |
Sources: packages/devtools-ui/vite/plugin.ts:285-450
DevTools Communication Sequence
The following sequence shows how the DevTools UI interacts with the server-side Vite plugin to save changes:
The sequence demonstrates the flow of data when a developer updates a translation via the UI.
Sources: packages/devtools-ui/vite/plugin.ts:258-278, packages/devtools-ui/vite/plugin.ts:413-448
Configuration for Server Assets
Build-time configuration dictates how translations are bundled for the server.
serverAssets: When enabled, translations are embedded into the Nitro server bundle usingserverAssets.premergedmode: Embeds a full page/locale matrix into the server.sourcemode: Embeds a compact tree, which is recommended for large catalogs to reduce server memory footprint.
Sources: src/module.ts:614-630, src/module.ts:98-110
Summary
Server & API Integrations facilitate high-performance internationalization by offloading locale detection and translation resolution to the Nitro server layer. By providing structured API endpoints for payloads and development tools, the module ensures a consistent experience between server-side rendering, client-side navigation, and local development workflows. Sources for this implementation are found primarily in src/runtime/server/ and the DevTools package.