Best practice of Error messages, Constants in Angular for Localization

Viewed 485

I am using Angular 9 for application development.

I have so many hard-coded texts in terms of Messages, Conditional Structures, Errors ect,. as shown below:

if (args.item.id === 'Excel') {  //here text 'Excel' is hard-coded
}


this.toasterService.showMessage({
  success: true,
  message: 'Successfully exported data to your local system.' // Here message is hard-coded
});

I wanted to know that, is there any best-practice available to handle all these in a file (Json / TS ), so that it'll be helpful bundle for Localization in future?

Please guide me in this.

1 Answers

I could guess enum is one of the most (if not the most) popular way to manage sets of constants. And Typescript has it:

export enum Messages {
  Login = 'Login successful',
  Logout = 'Logout successful',
} 

Easy to manage, easy to debug (especially with string enum). For the examples you provided, I would most likely go with enums.

Related