[英]Translating Class Validation error message using i18n library in NestJs project
[英]Multilingual validation message with Mongo Schema and i18n
我是Node.js / Mongo的新手,我想知道如何在驗證中使用i18n。 到目前為止,這就是我所擁有的
架構
const UserSchema = new Schema({
language: {
type: String,
enum: ['fr', 'en']
},
email: {
type: String,
default: ''
}
});
驗證
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的配置,因此可以使用它,但是我知道這不是正確的方法。 我嘗試使用this.language,但默認情況下始終為英語。
我想使用在表單中發送的語言配置i18n。 模型中有沒有辦法做到這一點? 謝謝!
這樣我就可以找到解決方法,到目前為止,這里是我的解決方案。 我直接在字段中使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.