Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions nuxt/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ interface NuxtModuleOptions {
* external variables all work normally — no serialization limitations.
*/
config?: string;
/**
* When true, the active locale is automatically synced with @nuxtjs/i18n.
* Valiform will use `nuxtApp.$i18n.locale` (a Ref<string>) at runtime,
* so changing the i18n language also changes the validation messages language.
* The `locale` option is used as fallback if $i18n is not available.
*/
syncI18n?: boolean;
}

export default defineNuxtModule<NuxtModuleOptions>({
Expand Down
12 changes: 11 additions & 1 deletion nuxt/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@ import { FormsPlugin, registerRules } from '@overgaming/valiform';
import { pluginOptions, valiformConfig } from '#build/valiform/options.mjs';

export default defineNuxtPlugin((nuxtApp) => {
const opts = { ...pluginOptions };
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { syncI18n, ...formsOpts } = pluginOptions;
const opts = { ...formsOpts };

if (valiformConfig?.locales) {
opts.locales = valiformConfig.locales;
}

if (syncI18n) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const i18nLocale = (nuxtApp as any).$i18n?.locale;
if (i18nLocale) {
opts.locale = i18nLocale;
}
}

nuxtApp.vueApp.use(FormsPlugin, opts);

if (valiformConfig?.rules) {
Expand Down