3

I'm new to Node.js/Mongo and I was wondering how I could use i18n with my validation. So far here's what I have

Schema

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'));

I added the configuration of i18n before so I can use it but I know it is not the right way to do it. I tried to use this.language but it is always English by default.

I want to configure i18n with the language sent in my form. Is there a way to do this in the model? Thanks!

Sam Bellerose
  • 1,782
  • 2
  • 18
  • 43

1 Answers1

0

So I was able to find a workaround, here my solution so far. I'm using the id in the field directly

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

And on my view using pug here's what I do for each errors

ul
    each error in errors
        li!=  __(error) // the error return will be the key for this language
Sam Bellerose
  • 1,782
  • 2
  • 18
  • 43