Vue/Nuxt DevTools UI
Relevant source files
The following files were used as context for generating this wiki page:
The Vue/Nuxt DevTools UI for nuxt-i18n-micro provides an interactive development environment for managing internationalization. It includes a Vite plugin that injects a management interface into the browser and a dedicated API to handle translation file operations. The system prioritizes developer experience by enabling direct viewing and editing of translation keys during development.
Sources: packages/devtools-ui/vite/plugin.ts:162-168, AGENTS.md:12-12
Architecture and Integration
The DevTools system operates through a Vite plugin named i18n-micro-devtools-plugin. This plugin runs exclusively during the development server phase (serve mode). It performs three primary functions: injecting a UI access button, serving a virtual HTML page for the editor, and providing a local API for file system communication.
Sources: packages/devtools-ui/vite/plugin.ts:162-168, packages/devtools-ui/vite/plugin.ts:173-178
UI Injection Mechanism
The plugin utilizes the transformIndexHtml hook to inject a JavaScript-based button into the application's DOM. This button appears at the bottom-right corner of the browser window. When clicked, it opens a modal containing an iframe that loads the DevTools interface from a virtual path.
Sources: packages/devtools-ui/vite/plugin.ts:38-160, packages/devtools-ui/vite/plugin.ts:185-195
Component Relationship Diagram
The following diagram illustrates the relationship between the Vite server, the injected client-side script, and the file system.
The diagram shows how the injected browser components interact with the Vite server middleware to manage physical translation files on the disk.
DevTools API Endpoints
The system implements a specialized API handled by Vite server middleware. This API allows the UI to communicate with the project's local file system. By default, the API base is /__i18n_api.
| Endpoint | Method | Description |
|---|---|---|
/config | GET | Returns the current i18n configuration, including default and fallback locales. |
/files | GET | Scans the translationDir and returns a flat list and tree structure of all JSON translation files. |
/file | GET | Retrieves the content of a specific translation file via the path query parameter. |
/save | POST | Writes updated JSON content back to a specified file on the disk. |
Sources: packages/devtools-ui/vite/plugin.ts:273-418
API Communication Sequence
This sequence diagram demonstrates the flow of data when a developer saves a translation update.
The sequence shows the validation and file system operations performed by the plugin when saving translations.
Plugin Configuration
The i18nDevToolsPlugin accepts an options object to customize its behavior and path resolution.
| Option | Type | Default | Description |
|---|---|---|---|
base | string | /__i18n_api | The base path for the development API endpoints. |
translationDir | string | src/locales | The directory where translation JSON files are stored. |
injectButton | boolean | true | Determines whether the floating UI button is injected into the HTML. |
Sources: packages/devtools-ui/vite/plugin.ts:9-13, packages/devtools-ui/vite/plugin.ts:163-165
The DevTools Bridge
The DevTools UI communicates with the Vite server through a "bridge" object. This bridge uses the fetch API to interact with the plugin's middleware. It includes methods for initial data loading and a polling mechanism to watch for locale updates.
Key Bridge Methods:
getLocalesAndTranslations(): Aggregates file lists and individual file contents into a single state object.getConfigs(): Fetches global module settings.saveTranslation(filePath, content): Sends updated keys to the server.onLocalesUpdate(callback): Sets up a 2-second interval to refresh the UI state from the server.
Sources: packages/devtools-ui/vite/plugin.ts:219-257
File Scanning Logic
The plugin recursively scans the translationDir to identify all .json files. It normalizes paths to ensure cross-platform compatibility, replacing backslashes with forward slashes. It ignores directory access errors to prevent the dev server from crashing if folders are missing.
Sources: packages/devtools-ui/vite/plugin.ts:16-34
Security and Safety
The plugin implements path validation to prevent directory traversal attacks. The safeResolvePath utility ensures that file operations remain within the project root. The API specifically restricts operations to files ending in the .json extension.
Sources: packages/devtools-ui/vite/plugin.ts:339-347, packages/devtools-ui/vite/plugin.ts:393-395
Summary
The Vue/Nuxt DevTools UI serves as a critical bridge between the runtime application and the static translation assets. By leveraging Vite middleware and an injected UI, it provides a seamless editing experience that persists changes directly to the project's source code. This integration ensures that developers can manage large translation files without leaving the browser environment.
Sources: packages/devtools-ui/CHANGELOG.md:121-125, README.md:95-99