Ask your question and get a summary of the document by referencing this page and the AI provider of your choice
Version History
- "Initial documentation"v8.0.01/30/2026
If you have an idea for improving this documentation, please feel free to contribute by submitting a pull request on GitHub.
GitHub link to the documentationCopy doc Markdown to clipboard
Documentation: t Function in adonis-intlayer
The t function in the adonis-intlayer package is the core utility for providing localized responses in your AdonisJS application. It simplifies internationalization (i18n) by dynamically selecting content based on the user's preferred language.
Overview
The t function is used to define and retrieve translations for a given set of languages. It automatically determines the appropriate language to return based on the client's request settings, such as the Accept-Language header. If the preferred language is unavailable, it gracefully falls back to the default locale specified in your configuration.
Key Features
- Dynamic Localization: Automatically selects the most appropriate translation for the client.
- Fallback to Default Locale: Falls back to a default locale if the client's preferred language isn't available, ensuring continuity in user experience.
- Asynchronous Context: Works seamlessly within the AdonisJS request lifecycle using Async Local Storage.
- TypeScript Support: Enforce type safety for your translations.
Function Signature
Copy the code to the clipboard
Parameters
translations: An object where the keys are locale codes (e.g.,en,fr,es) and the values are the corresponding translated content.
Returns
- The content representing the client's preferred language.
Loading the Middleware
To ensure that the t function works correctly, you must register the intlayer middleware in your AdonisJS application.
Copy the code to the clipboard
Usage Examples
Basic Example
Copy the code to the clipboard
Usage in Controllers
Copy the code to the clipboard
Advanced Topics
Fallback Mechanism
If a preferred locale is unavailable, the t function will fallback to the default locale defined in your intlayer.config.ts.
Copy the code to the clipboard
TypeScript Integration
The t function is type-safe when used with defined dictionaries. For more details, refer to the TypeScript documentation.
