Angular Message Translation (i18n)
In this section we will go through a step by step example on how to translate the form error messages generated by simple-body-validator in the Angular application.
Add The Translation Messages
The first step is to create a directory that will hold all the translation files. In the following example we will
create a directory called lang
, of course you can use any other directory name. Add to the directory
three files, en.ts
for the English translations, fr.ts
for the French translations, and index.ts
/lang
en.ts
fr.ts
index.ts
Now we should add the corresponding entries for each of the files. You can find the full list of translation attributes here.
export default {
required: 'Le champ :attribute est obligatoire',
numeric: 'Le champ :attribute doit être un nombre.',
min: {
number: 'Le :attribute doit être au moins égal à :min.',
string: 'Le :attribute doit comporter au moins trois caractères'
},
}
export default {
// We will leave the en.ts file empty, and just rely in the default english messages provided by the library
}
Default Messages
By default simple-body-validator returns the messages in English. The en.ts
in that case will be only used
to override any existing message.
Now the index.ts
will be used to export all the translations in one file.
import en from './en';
import fr from './fr';
export default {
en, fr
};
Apply the Translated Messages
After adding all the needed translations, we need to initialize them in the main.ts
file using the setTranslationObject
method.
import translations from './lang';
import { setTranslationObject } from 'simple-body-validator';
setTranslationObject(translations);
Of course we will also need to add a default lang for our application. In case the default lang was not set,
the English
language will be used as the default one. To accomplish this we will use the setDefaultLang
method.
import translations from './lang';
import { setTranslationObject, setDefaultLang } from 'simple-body-validator';
setTranslationObject(translations);
// Set the French language as the default
setDefaultLang('fr');
caution
The setDefaultLang
should be set after setting the translation object and not before.
A full reference to the translation feature can be found here.
Form Translation Example
Assuming that you already started with the example from the previous section.
We will continue by adding a button to switch between the English
and French
languages.
Now let's start by adding the changeLanguage
method to our code.
changeLanguage() {
this.validator.getLang() === 'fr' ? this.validator.setLang('en') : this.validator.setLang('fr');
}
As shown in the code snippet above, the setLang
method can be used on the validator
instance
to specify the language. Now let's add the change Language button under the form
tag.
<form>
...
</form>
<button (click)="changeLanguage()">
Change Language
</button>
Try to switch between languages and submit the form. The messages will appear according to the selected language.
However, we still face one problem, the attribute
value for the French
language is incorrect.
To solve this problem we will navigate to the fr.ts
file that we created previously and add the following.
attributes: {
name: 'Nom',
age: 'âge',
movie: 'film'
},
What we did above, is that we mapped each attribute to a new custom attribute name. You can read more about custom attributes here. The full code for the translation example can be found below.
In the example above we wanted to have a custom message for the min
rule only when used with the age
attribute.
To do so, we added the following custom
attribute to each of the language files.
custom: {
age: {
min: 'The age must be at least :min years.',
},
},
custom: {
age: {
min: 'L\'âge doit être d\'au moins :min ans.',
},
},
You can read more about attribute custom messages here.