使用Mongo Schema和i18n的多语言validation消息

我是Node.js / Mongo的新手,我想知道如何使用i18n进行validation。 到目前为止,这是我的

架构

const UserSchema = new Schema({ language: { type: String, enum: ['fr', 'en'] }, email: { type: String, default: '' } }); 

validation

 i18n.configure({ locales:['en', 'fr'], directory:'locales', defaultLocale: this.language, cookie: 'locale' }); UserSchema.path('email').validate(function (email) { return email.length; }, i18n.__('Email_is_required')); 

我之前添加了i18n的configuration,所以我可以使用它,但我知道这是不正确的做法。 我试图使用this.language,但默认情况下,它总是英语。

我想用我的表单发送的语言来configurationi18n。 有没有办法在模型中做到这一点? 谢谢!

所以我能find一个解决方法,在这里我的解决scheme到目前为止。 我直接在字段中使用id

 const UserSchema = new Schema({ email: { type: String, unique: 'Unique_email', // id error in en.json trim: true, default: '', required: [true, 'Email_required'] } }); 

在我看来,使用帕格这是我为每个错误所做的

 ul each error in errors li!= __(error) // the error return will be the key for this language