英文:
Vuelidate with i18n: Key not found in locale messages
问题
你的代码中似乎出现了一些问题,导致无法找到所需的翻译消息。以下是一些可能的解决方案,以确保正确翻译消息:
-
确保你的
messages
对象中有与你的默认语言(fallbackLocale
)相对应的翻译消息。在你的代码中,fallbackLocale
设置为'en'
,所以你需要在messages
对象中包括'en'
的翻译。 -
确保使用正确的语言代码。在你的代码中,
locale
设置为'cz'
,这可能是语言代码的问题。通常,捷克语的语言代码是'cs'
,所以你需要将locale
设置为'cs'
。 -
确保你正确地配置了
vue-i18n
,包括加载和设置默认语言、回退语言等。你可以查看vue-i18n
的文档来确认配置的正确性。 -
检查
VuelidateValidators.required
是否期望接收正确的翻译消息。根据你的代码,它似乎期望接收t
函数,你需要确保t
函数可以正确地提供翻译消息。
根据上述问题,你可以尝试修改你的代码以确保正确翻译消息。如果问题仍然存在,你可能需要更深入地检查你的 vue-i18n
和 Vuelidate
配置以解决问题。
英文:
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
以下是翻译好的部分:
首先确保您已安装了vuelidade
和vue-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 "@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);
as a component you can follow this one as example:
<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>
and now you should be able to see the translated message:
答案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 '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)//Important
})
And then make your composable like file 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);
// You can add more validators
return {
required,
email,
};
};
Then you can use is in Vue like :
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 },
},
};
},
};
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论