Ask your question and get a summary of the document by referencing this page and the AI provider of your choice
This doc is out of date, the base version has been updated on 23 August 2025.
Go to English docVersion History
- "Init history"v5.5.1029/06/2025
The content of this page was translated using an AI.
See the last version of the original content in EnglishIf 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 next-intlayer
The t function in the next-intlayer package is a fundamental tool for inline internationalisation within your Next.js application. It allows you to define translations directly within your components, making it simple to display localised content based on the current locale.
Overview
The t function is used to provide translations for different locales directly in your components. By passing an object containing translations for each supported locale, t returns the appropriate translation based on the current locale context in your Next.js application.
Key Features
- Inline Translations: Ideal for quick, inline text that doesn't require a separate content declaration.
- Automatic Locale Selection: Returns the translation corresponding to the current locale automatically.
- TypeScript Support: Provides type safety and autocompletion when used with TypeScript.
- Easy Integration: Works seamlessly within both client and server components in Next.js.
Function Signature
Parameters
translations: An object where keys are locale codes (e.g.,en,fr,es) and values are the corresponding translated strings.
Returns
- A string representing the translated content for the current locale.
Usage Examples
Using t in a Client Component
Ensure you include the 'use client'; directive at the top of your component file when using t in a client-side component.
Using t in a Server Component
Inline Translations in Attributes
The t function is particularly useful for inline translations in JSX attributes.
When localising attributes like alt, title, href, or aria-label, you can use t directly within the attribute.
Advanced Topics
TypeScript Integration
The t function is type-safe when used with TypeScript, ensuring that all required locales are provided.
Locale Detection and Context
In next-intlayer, the current locale is managed through context providers: IntlayerClientProvider and IntlayerServerProvider. Ensure these providers wrap your components and that the locale prop is correctly passed.
Example:
Common Errors and Troubleshooting
t Returns Undefined or Incorrect Translation
- Cause: The current locale is not correctly set, or the translation for the current locale is missing.
- Solution:
- Verify that the
IntlayerClientProviderorIntlayerServerProvideris properly configured with the appropriatelocale. - Ensure that your translations object contains all the necessary locales.
- Verify that the
Missing Translations in TypeScript
- Cause: The translations object does not satisfy the required locales, resulting in TypeScript errors.
- Solution: Use the
IConfigLocalestype to enforce the completeness of your translations.
Tips for Effective Usage
- Use
tfor Simple Inline Translations: Ideal for translating small pieces of text directly within your components. - Prefer
useIntlayerfor Structured Content: For more complex translations and content reuse, define content in declaration files and useuseIntlayer. - Consistent Locale Provision: Ensure that your locale is consistently provided across your application through the appropriate providers.
- Leverage TypeScript: Use TypeScript types to catch missing translations and ensure type safety.
Conclusion
The t function in next-intlayer is a powerful and convenient tool for managing inline translations in your Next.js applications. By integrating it effectively, you enhance the internationalisation capabilities of your app, providing a better experience for users worldwide.
For more detailed usage and advanced features, refer to the next-intlayer documentation.
Note: Remember to set up your IntlayerClientProvider and IntlayerServerProvider properly to ensure that the current locale is correctly passed down to your components. This is crucial for the t function to return the correct translations.