Skip to main content

Translating Error Messages

Simple Body Validator comes with a built-in translation feature that provides a convenient way to retrieve error messages in various languages, allowing you to easily support multiple languages for your data validation.

Translation Configuration

To add error messages translations you must use the setTranslationObject method.

    import { setTranslationObject } from 'simple-body-validator';
    const { setTranslationObject } = require('simple-body-validator');

Each object key must refer to a language code.

    setTranslationObject({
en: {
required: 'The :attribute is required'
},
fr: {
required: 'Le :attribute est requis'
}
})

We suggest that you add translations for different languages in separate translation files. Let's say that the directory where you want to add your translations is named lang and you want to add translations for the English and French languages. The structure of your translations files will be as follows. For the purpose of the example we will use app.js as the entry point to our project.

    /lang
en.js
fr.js
index.js
app.js

Let's take a look at each file separately.

lang/en.js
    module.exports = {
required: 'The field :attribute is required.'
// You can add as many translations as needed
};
lang/fr.js
    module.exports = {
required: 'Le champ :attribute est requis'
};

And finally after adding all your translations, use the index.js to export them all in one file.

lang/index.js
    const en = require('./en');
const fr = require('./fr');

module.exports = {
en, fr
};

Meanwhile in the app.js you can pass the translation object to the setTranslationObject method.

app.js
    const translations = require('./lang.js');
const { setTranslationObject } = require('simple-body-validator');

setTranslationObject(translations);

And that's it, this is how you add your translations

Setting The Default Language

You can specify the default language to be used by the validator, by invoking the setDefaultLang method and passing the language code as parameter.

    import { setDefaultLang } from 'simple-body-validator';
    const { setDefaultLang } = require('simple-body-validator');

Let's say we want to set the fr lang as the default language.

    setDefaultLang('fr');

Now the French error messages will be returned in case an error occurred and in case no language was specified explicitly.

Setting The Fallback Language

You can determine the fallback language to be used when the current one is not available. To do so the setFallbackLang method needs to be invoked with the desired fallback language passed as the parameter.

    import { setFallbackLang } from 'simple-body-validator';
    const { setFallbackLang } = require('simple-body-validator');

Let's say we want to set the fr lang as the fallback language.

    setFallbackLang('fr');
Difference Between Default and Fallback Language

The default language will be used when no language is specified explicitly, while the fallback language will be used in case the translation mechanism was not able to find the message related to the current language.

Specifying Validation Language

Besides the default language, you can explicitly specify the lang to be used each time you want to run the validation. This is done by invoking the setLang method on the validator instance.

    const validator = make(data, rules).setLang(lang);
tip

If the validator cannot find the error message in the specified language file, it will use the fallback language. If the error message is not found in the fallback language either, the validator will use English.

Specifying Custom Messages

Simple body validator has already default error messages specified for each rule. You are free to change or modify these messages based on the needs of your application

Custom Messages for Error rules

After creating the translation files, you need to populate them with messages. Let's take for example the fr.js.

fr.js
    module.exports = {
min: {
numeric: 'Le champ :attribute doit être supérieur à :min.',
string: 'Le champ :attribute doit contenir plus de :min caractères.'
},
required: 'Le champ :attribute est requis.',
string: 'Le chanp :attribute doit être une chaîne'
};

You can find a list of all error messages here. In case an error message is not filled for a rule the default message will be returned.

Custom Messages For Specific Attributes

You may customize the error messages used for specific attribute and rule combinations within your application's validation language files. To do so, add your message customizations to the custom object of your application's lang/xx.js language file.

    custom: {
email: {
required: 'We need to know your email address!',
max: 'Your email address is too long!'
}
}

Specifying Attributes in Language Files

Many of Simple Body Validator built-in error messages include an :attribute placeholder that is replaced with the name of the field or attribute under validation. If you would like the :attribute portion of your validation message to be replaced with a custom value, you may specify the custom attribute name in the attributes object of your lang/xx.js language file.

    attributes: {
email: 'email address',
}