directus-extension-i18n-register
v1.0.0
Published
Directus extension that automatically sets the language field during user registration based on HTTP request headers
Maintainers
Readme
directus-extension-i18n-register
this is a Directus extension that automatically sets the language field during user registration based on the HTTP request header. Installation
Prerequisites
- You need Directus 9+ with a hook-enabled deployment
- Node.js 18 or higher
Install the Extension Via NPM
directus-extension-i18n-registerHow It Works
The extension hooks into the middlewares.before lifecycle event and intercepts POST requests to:
/users/register
If the header is set with a valid language code, it is stored in a request-scoped AsyncLocalStorage for use during the user.create hook handler. The handler will then inject the language field into the user registration payload before it is persisted to the database. A bad language code will be silently ignored to prevent registration failures.
Language Code Validation
The extension validates incoming language codes against a comprehensive list language codes. This ensures only valid, standard language codes are accepted. Invalid codes are silently ignored to prevent registration failures.
Request Headers Supported
| Header Name | Description |
| --------------------------- | ------------------------ |
| x-user-language (default) | Directus-specific header |
See Custom Header Configuration for instructions on how to change the header name.
Example Usage
1. Default Configuration (no env file needed)
Send requests with the default x-user-language header:
POST /users/register
Authorization: Bearer <token>
Content-Type: application/json
x-user-language: en-US
{
"email": "[email protected]",
"password": "securePassword123",
"firstName": "John",
"lastName": "Doe"
}The extension will automatically inject the language field with value en-US. Note that only valid, supported language codes will be injected - invalid codes are ignored to prevent registration failures.
Custom Header Configuration
Create a .env file:
# Set your preferred header name
I18N_REGISTER_HEADER=Accept-LanguageThen send requests with Accept-Language:
POST /users/register
Authorization: Bearer <token>
Content-Type: application/json
Accept-Language: es-ES
{
"email": "[email protected]",
"password": "contraseña123",
"firstName": "Juan",
"lastName": "Pérez"
}The extension will automatically inject the language field with value es-ES.
Supported Language Codes
The supported language codes are pulled from Directus's internal list. If you notice our list is missing a language code, please submit an issue or pull request.
See the full list in the source code for all supported variants.
