Resource Loading
The plugin supports three resource loading methods: HTTP backend, File System backend (FS Backend), and SDK backend. Additionally, the plugin supports chained backend, which allows combining multiple backends.
HTTP Backend
HTTP backend loads resource files through HTTP requests, suitable for client-side rendering (CSR) scenarios.
Configuration
Resource File Structure
Resource files need to be placed in the config/public directory or the directory configured through server.publicDir:
Or configure a custom directory through server.publicDir:
Resource File Format:
Path Variables
loadPath supports the following variables:
{{lng}}: Language code (e.g.,en,zh){{ns}}: Namespace (e.g.,translation,common)
Examples:
File System Backend (FS Backend)
File System backend reads resource files directly from the file system, suitable for server-side rendering (SSR) scenarios.
Configuration
In SSR scenarios, the plugin will automatically use the file system backend. Resource files need to be placed in the project directory:
Resource File Path
The default path format for file system backend is a relative path:
You can customize the path through loadPath:
The loadPath configuration is used for both HTTP backend (frontend) and file system backend (server-side). If configured as an absolute path starting with / (e.g., /locales/{{lng}}/{{ns}}.json), the file system backend will automatically convert it to a relative path (./locales/{{lng}}/{{ns}}.json). Therefore, it's recommended to use absolute paths in the configuration, which can meet both frontend and backend requirements.
SDK Backend
SDK backend allows loading resources through custom functions, suitable for scenarios where translation resources need to be loaded from external services, databases, or other custom sources.
Enable SDK Mode
Step 1: Enable SDK mode in modern.config.ts
Step 2: Implement SDK function in modern.runtime.ts
Implement SDK Loader Function
The SDK function receives an I18nSdkLoadOptions parameter and needs to return data in Resources format:
Batch Loading Examples
SDK backend supports multiple loading modes:
1. Load single resource:
2. Batch load multiple languages:
3. Batch load multiple namespaces:
4. Load all resources:
Check Resource Loading State
When using SDK backend, you can check if resources are loaded using isResourcesReady:
This is particularly useful when resources are loaded asynchronously, as it ensures all required namespaces for the current language are loaded before rendering content that depends on translations.
Chained Backend
Chained backend allows using multiple backends simultaneously, enabling progressive resource loading and updates. When both loadPath (or FS backend) and sdk are configured, the plugin automatically uses i18next-chained-backend to chain resource loading.
How It Works
The chained backend workflow:
- Initial Load: First load resources from HTTP/FS backend and display immediately (quick display of basic translations)
- Async Update: Then asynchronously load resources from SDK backend and update the i18next store (update/supplement translations)
This ensures users see page content quickly while the latest translation resources are loaded in the background.
Configuration
Step 1: Configure chained backend in modern.config.ts
Step 2: Implement SDK function in modern.runtime.ts
Cache Hit Mode (cacheHitMode)
The cacheHitMode option controls the behavior of chained backend:
'none'(default, only when chained backend is not configured): If the first backend returns resources, stop and don't try the next backend'refresh': Try to refresh the cache by loading from the next backend and update the cache'refreshAndUpdateStore'(default for chained backend): Try to refresh the cache by loading from the next backend, update the cache and also update the i18next resource store. This allows FS/HTTP resources to be displayed first, then SDK resources will update them asynchronously.
Configuration example:
Use Cases
Chained backend is particularly suitable for the following scenarios:
- Progressive Loading: Display local/static resources first, then load latest translations from remote services
- Offline Support: Local resources as offline fallback, SDK resources provide online updates
- Performance Optimization: Quickly display basic translations, load complete translation content in the background
- A/B Testing: Local resources as default values, SDK provides dynamic translation variants
Complete Example
In this example:
- When the page loads, resources are first loaded from
/locales/{{lng}}/{{ns}}.jsonand displayed immediately - Latest translations are loaded asynchronously from
https://api.example.com/i18n/...in the background - After SDK resources are loaded, the i18next store is automatically updated, and the UI text is automatically updated