Ng2-translate in ts file

Hi,

I’m trying to use ng2-translate and ng-validation work together.

I’m creation the validation array like:
validationMessages = {
‘name’: {
‘required’: ‘El nombre es obligatorio
’,
‘pattern’: ‘El nombre es inválido
’,
‘maxlength’: ‘La longitud máxima del nombre es 30
’,
},
‘sex’: {
‘required’: ‘El sexo es obligatorio
’,
},
‘birthdate’: {
‘required’: ‘La fecha de nacimiento es obligatoria
’,
}
};

I have the es.json and en.json files for spanish and english languages. How can I make the translation of the error messages? As I know the ng2-translate is async.

Any suggestion? best regards

OK. I do a simple trick solution from my self.

I call an async function get and after subcribe returns I know the translate file is loaded and them I can call the instant method. Something like…

this.translate.get(‘PERFIL.TITLE’).subscribe(
value => {
this.validationMessages = {
‘name’: {
‘required’: this.translate.instant(‘PERFIL.name_required’),
‘pattern’: this.translate.instant(‘PERFIL.name_pattern’),
‘maxlength’: this.translate.instant(‘PERFIL.name_maxlength’),
},
‘gender’: {
‘required’: this.translate.instant(‘PERFIL.gender_required’),
},
‘birthdate’: {
‘required’: this.translate.instant(‘PERFIL.birthdate_required’),
}
};

  }
);

Best regards

This looks fragile to me, and I don’t see what the benefit is over just using get.

You can use the TranslateService from ng2-translate to do that.
Check this tutorial to see how to do it.
Hope it helps :slight_smile:

Hi,

It may be fragile as you say, but at code level it’s simple. Don’t you think adding a subscription for each of the texts is excessive? How would you develop the example I’ve put with subscriptions in an easy way?

(Sorry for my english. I’m from Spain)

Thanks for your anwers anb best regards

Hi,

Thanks. The tutorial is good but does not respond to the problem presented.

Thanks and best regards