Vuelidate with i18n: Key not found in locale messages

huangapple go评论65阅读模式
英文:

Vuelidate with i18n: Key not found in locale messages

问题

你的代码中似乎出现了一些问题,导致无法找到所需的翻译消息。以下是一些可能的解决方案,以确保正确翻译消息:

  1. 确保你的messages对象中有与你的默认语言(fallbackLocale)相对应的翻译消息。在你的代码中,fallbackLocale 设置为 'en',所以你需要在 messages 对象中包括 'en' 的翻译。

  2. 确保使用正确的语言代码。在你的代码中,locale 设置为 'cz',这可能是语言代码的问题。通常,捷克语的语言代码是 'cs',所以你需要将 locale 设置为 'cs'

  3. 确保你正确地配置了 vue-i18n,包括加载和设置默认语言、回退语言等。你可以查看 vue-i18n 的文档来确认配置的正确性。

  4. 检查 VuelidateValidators.required 是否期望接收正确的翻译消息。根据你的代码,它似乎期望接收 t 函数,你需要确保 t 函数可以正确地提供翻译消息。

根据上述问题,你可以尝试修改你的代码以确保正确翻译消息。如果问题仍然存在,你可能需要更深入地检查你的 vue-i18nVuelidate 配置以解决问题。

英文:

So in my i18n-validators.js file I want to export validators with translated messages to my language of choice and use them in my vue component to validate a form.

My code:


// import * as VuelidateValidators from 'https://cdn.jsdelivr.net/npm/@vuelidate/validators';
// import * as VueI18n from 'https://unpkg.com/vue-i18n@9';

const messages = {
  en: {
    validations: {
      required: 'The field {property} is required.',
    }
  },
  cs: {
    validations: {
      required: 'Toto pole {property} je povinné',
    }
  },
}


const i18n = VueI18n.createI18n({
  locale: 'cz',
  fallbackLocale: 'en',
  messages
})

const withI18nMessage = VuelidateValidators.createI18nMessage({ 
  t: VueI18n.createI18n().global.t.bind(i18n)
})

export const required = withI18nMessage(VuelidateValidators.required)

Console:


Not found 'validations.required' key in 'en-US' locale messages. vue-i18n@9
Fall back to translate 'validations.required' key with 'en' locale. vue-i18n@9
Not found 'validations.required' key in 'en' locale messages.

And I want the validator to throw me the specified message instead of the "validations.required" message

答案1

得分: 1

以下是翻译好的部分:

首先确保您已安装了vuelidadevue-i18n

根据您的示例,您可以将上面的文件更改为:

import * as validators from "@vuelidate/validators";
import { createI18n } from "vue-i18n";

const { createI18nMessage } = validators;

const messages = {
  en: {
    validations: {
      required: "The field {property} is required.",
    },
  },
  cs: {
    validations: {
      required: "Toto pole {property} je povinné",
    },
  },
};

const i18n = createI18n({
  locale: "cs",
  fallbackLocale: "en",
  messages,
});

const withI18nMessage = createI18nMessage({ t: i18n.global.t.bind(i18n) });
export const required = withI18nMessage(validators.required);

作为一个组件,您可以参照以下示例:

<template>
  ...
  <div class="mb-3">
    <input
      v-model="formData.name"
      className="form-control"
      placeholder="Insert your name.."
    />
  </div>

  <span v-for="error in v$.name.$errors" :key="String(error.$uid)">
    <span class="text-danger">{{ error.$message }}</span>
  </span>

  <div class="mt-5 submit">
    <button class="btn btn-primary btn-sm" type="button" @click="submitForm">
      Next
    </button>
  </div>
  ...
</template>

<script lang="ts">
import { defineComponent, reactive } from "vue";
import useVuelidate from "@vuelidate/core";
import { required } from "@/utils/validators/i18n-validators";

export default defineComponent({
  name: "InitialDataForm",
  setup() {
    const formData = reactive({
      name: "",
    });
    const rules = {
      name: { required },
    };
    const v$ = useVuelidate(rules, formData);
    return {
      formData,
      v$,
    };
  },
  methods: {
    async submitForm() {
      const result = await this.v$.$validate();
      if (result) {
        alert("validation passed");
      }
    },
  },
});
</script>

现在,您应该能够看到已翻译的消息。

英文:

First make sure you have installed vuelidade and vue-i18n

Following your example, you can change the file above to:

import * as validators from &quot;@vuelidate/validators&quot;;
import { createI18n } from &quot;vue-i18n&quot;;

const { createI18nMessage } = validators;

const messages = {
  en: {
    validations: {
      required: &quot;The field {property} is required.&quot;,
    },
  },
  cs: {
    validations: {
      required: &quot;Toto pole {property} je povinn&#233;&quot;,
    },
  },
};

const i18n = createI18n({
  locale: &quot;cs&quot;,
  fallbackLocale: &quot;en&quot;,
  messages,
});

const withI18nMessage = createI18nMessage({ t: i18n.global.t.bind(i18n) });
export const required = withI18nMessage(validators.required);

as a component you can follow this one as example:

&lt;template&gt;
  ...
  &lt;div class=&quot;mb-3&quot;&gt;
    &lt;input
      v-model=&quot;formData.name&quot;
      className=&quot;form-control&quot;
      placeholder=&quot;Insert your name..&quot;
    /&gt;
  &lt;/div&gt;

  &lt;span v-for=&quot;error in v$.name.$errors&quot; :key=&quot;String(error.$uid)&quot;&gt;
    &lt;span class=&quot;text-danger&quot;&gt;{{ error.$message }}&lt;/span&gt;
  &lt;/span&gt;

  &lt;div class=&quot;mt-5 submit&quot;&gt;
    &lt;button class=&quot;btn btn-primary btn-sm&quot; type=&quot;button&quot; @click=&quot;submitForm&quot;&gt;
      Next
    &lt;/button&gt;
  &lt;/div&gt;
  ...
&lt;/template&gt;

&lt;script lang=&quot;ts&quot;&gt;
import { defineComponent, reactive } from &quot;vue&quot;;
import useVuelidate from &quot;@vuelidate/core&quot;;
import { required } from &quot;@/utils/validators/i18n-validators&quot;;

export default defineComponent({
  name: &quot;InitialDataForm&quot;,
  setup() {
    const formData = reactive({
      name: &quot;&quot;,
    });
    const rules = {
      name: { required },
    };
    const v$ = useVuelidate(rules, formData);
    return {
      formData,
      v$,
    };
  },
  methods: {
    async submitForm() {
      const result = await this.v$.$validate();
      if (result) {
        alert(&quot;validation passed&quot;);
      }
    },
  },
});
&lt;/script&gt;

and now you should be able to see the translated message:

Vuelidate with i18n: Key not found in locale messages

答案2

得分: 0

以下是您的代码的翻译部分:

这是我的国际化插件和可组合文件
在插件中您将定义 Nuxt 插件如下

import { createI18n } from 'vue-i18n'
import en from '~/i18n/locales/en.json'
import cs from '~/i18n/locales/cs.json'
export default defineNuxtPlugin(({ vueApp }) => {
  const i18n = createI18n({
    legacy: false,
    globalInjection: true,
    fallbackLocale: 'en',
    locale: 'en',
    messages: {
        en,
        cs
    },
    defaultBrowserLanguage: false,
    locales: [
      {
        code: 'en',
      },
      {
        code: 'cs',
      },
    ],
  })

  vueApp.use(i18n)//重要
})

然后,创建名为 i18n-validators.ts 的可组合文件如下:

import * as validators from "@vuelidate/validators";
import { NuxtI18nInstance } from '@nuxtjs/i18n'
export const useI18nValidators = () => {
  const { createI18nMessage } = validators;
  const { vueApp } = useNuxtApp();
  const i18n = vueApp.config.globalProperties.$t;
  const withI18nMessage = createI18nMessage({ t:  i18n });
  const required = withI18nMessage(validators.required);
  const email = withI18nMessage(validators.email);
  // 您可以添加更多验证器
  return {
    required,
    email,
  };
};

然后,您可以在 Vue 中使用它如下:

import { useVuelidate } from "@vuelidate/core";
import { useI18nValidators } from "~/composables/i18n-validators";
export default {
  data() {
    return {
      v$: useVuelidate(),
      role: {
        name: "",
      },
    };
  },
  validations() {
    const { required } = useI18nValidators();
    return {
      role: {
        name: { required },
      },
    };
  },
}
英文:

Here is my i18n plugin and composable files :
In plugin you will define nuxt plugin like :

import { createI18n } from &#39;vue-i18n&#39;
import en from &#39;~/i18n/locales/en.json&#39;
import cs from &#39;~/i18n/locales/cs.json&#39;
export default defineNuxtPlugin(({ vueApp }) =&gt; {
  const i18n = createI18n({
    legacy: false,
    globalInjection: true,
    fallbackLocale: &#39;en&#39;,
    locale: &#39;en&#39;,
    messages: {
        en,
        cs
    },
    defaultBrowserLanguage:false,
    locales: [
      {
        code: &#39;en&#39;,
      },
      {
        code: &#39;cs&#39;,
      },
    ],
  })

  vueApp.use(i18n)//Important
})

And then make your composable like file i18n-validators.ts:

import * as validators from &quot;@vuelidate/validators&quot;;
import { NuxtI18nInstance } from &#39;@nuxtjs/i18n&#39;
export const useI18nValidators = () =&gt; {
  const { createI18nMessage } = validators;
  const { vueApp } = useNuxtApp();
  const i18n = vueApp.config.globalProperties.$t;
  const withI18nMessage = createI18nMessage({ t:  i18n });
  const required = withI18nMessage(validators.required);
  const email = withI18nMessage(validators.email);
  // You can add more validators
  return {
    required,
    email,
  };
};

Then you can use is in Vue like :

import { useVuelidate } from &quot;@vuelidate/core&quot;;
import { useI18nValidators } from &quot;~/composables/i18n-validators&quot;;
export default {
  data() {
    return {
      v$: useVuelidate(),
      role: {
        name: &quot;&quot;,
      },
    };
  },
  validations() {
    const { required } = useI18nValidators();
    return {
      role: {
        name: { required },
      },
    };
  },

};

</details>



huangapple
  • 本文由 发表于 2023年2月10日 06:18:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405042.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定