Saltar al contenido principal
Version: 4.x (latest)

Internacionalización

Una vez que @ngx-translate está configurado (ver Standalone o NgModule), la librería resuelve los mensajes de error desde tus archivos de traducción en lugar de un objeto errorMessages estático.

Uso​

<input
type="password"
formControlName="password"
placeholder="Password"
class="form-control"
ngxErrorMessage
/>
import { regEx } from 'ngx-error-message' // Validaciones de patrones incluidas en la librería

ngOnInit() {
this.form = this.fb.group({
password: [
null,
[
Validators.required,
Validators.minLength(6),
Validators.maxLength(50),
Validators.pattern(regEx.alphaNumeric),
],
],
})
}

Estructura del archivo de traducción​

El objeto de error de Angular siempre tiene la forma { [nombreDelValidador]: true | { ... } } — por ejemplo, Validators.required produce { required: true }. Tu archivo de traducción refleja eso bajo la clave validations (o la que hayas definido en validationsPrefix):

{
"validations": {
"required": "The field is required.",
"maxlength": "The maximum length allowed is {{param}}.",
"minlength": "The minimum allowed length is {{param}}.",
"email": "It is not a valid email.",
"min": "The minimum allowed is {{param}}.",
"max": "The maximum allowed is {{param}}.",
"pattern": {
"numeric": "The valid format is numeric.",
"alphabet": "The valid format is alphabetical.",
"smallLetters": "The valid format is lowercase letters.",
"capitalLetters": "The valid format is capital letters.",
"alphaNumeric": "The valid format is alphanumeric.",
"phoneNumber": "Invalid phone number.",
"websiteUrl": "Invalid website URL.",
"ip": "Invalid IP address."
}
}
}

Mantené los nombres de las claves y el placeholder {{param}} exactamente como se muestran — renombrarlos rompe la resolución de mensajes. {{param}} se completa con el valor de la restricción del validador (por ejemplo, el 6 en Validators.minLength(6)).