Nuxt I18n `t()` in shared folder for validation messages?
I have a
shared/types/login.ts file with a validator schema definition for a login request type. How would I localize validator error messages?5 Replies
Hi @ppseafield. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
I'm sorry, but the knowledge sources do not contain explicit information about using Nuxt I18n's
t() function directly within the shared/ directory (such as shared/types/login.ts) for localizing validator error messages.
From the available sources, we know:
- The shared/ directory is intended for pure JavaScript/TypeScript code that can be used in both the Vue app and the Nitro server, but it cannot import any Vue or Nitro code. This restriction likely includes composables like useI18n() or the t() function, which depend on the application context and are not available in the shared layer. You can only use auto-imports for pure functions in shared/utils/ and types in shared/types/—not for anything that requires runtime context from Vue or Nuxt, such as i18n translation functions Docs: shared directory.
- The t() function from Nuxt I18n (or useI18n()) must be called within a setup context or with access to the Nuxt app instance, as shown in examples for Pinia stores and validators. In those cases, the translation function is passed into validators or used within composables, not directly in shared files Stack Overflow: Pinia store i18n, Stack Overflow: Vuetify rules.
Recommended approach (based on available information):
- Define your validator schemas in the shared/ directory with error keys or codes, not localized strings.
- Perform the actual translation in the app layer (e.g., in your component, store, or composable), where you have access to t() or useI18n(). For example, pass the translation function to your validators or map error codes to localized messages after validation.
Example pattern:
Then, in your component or composable:
This pattern keeps your shared code framework-agnostic and allows localization where the i18n context is available.
If you need to use i18n in validators, you can pass the translation function as an argument, as shown in the Vuetify validation example Stack Overflow: Vuetify rules.
If you need more advanced patterns or direct support for i18n in the shared directory, the knowledge sources do not provide enough information to confirm this is possible.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@551124838564102154> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai How would I use a localized message with the Form component from Nuxt UI?
Let me check the knowledge base for you again. One moment! :books:
----------------------
To use localized messages with the Form component from Nuxt UI, you should leverage the i18n system in your Nuxt app. The recommended approach is to use the translation function (such as
Use the translation function inside your validation logic to return localized error messages. The Form component will display these messages as part of its built-in error handling Custom Validation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
t() from Nuxt I18n) within your validation logic or when displaying error messages.
How to do it:
1. Define your validation function using localized messages:
You can use the validate prop on the <UForm> component and call the translation function inside your validation logic. For example:
In this example, t('validation.required') will return the localized message for the required field error, based on your i18n configuration Custom Validation.
2. Automatic error display:
When a validation error occurs, the error message returned by your validation function will be displayed automatically by the <UFormField> component.
Summary:Use the translation function inside your validation logic to return localized error messages. The Form component will display these messages as part of its built-in error handling Custom Validation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
<@551124838564102154> Kapa.ai is still learning and improving, please let me know how I did by reacting below.