martinandert / counterpart

A translation and localization library for Node.js and the browser.
MIT License
242 stars 25 forks source link

Errorhandler during translation #46

Closed rvdkooy closed 7 years ago

rvdkooy commented 7 years ago

Hi Martin,

This PR adds the ability to listen for errors during translation.

since counterpart is depending on sprintf, which can throw, I think it is a good idea to wrap the sprintf call with a try/catch block.

This add the need to listen to these errors and handle this gracefully if needed.

see: https://github.com/alexei/sprintf.js/blob/master/src/sprintf.js#L45 https://github.com/alexei/sprintf.js/blob/master/src/sprintf.js#L62 https://github.com/alexei/sprintf.js/blob/master/src/sprintf.js#L62 ... and more

What do you think?

R

martinandert commented 7 years ago

Looks good, thanks!

What if the user hasn't subscribed to the error event? Shouldn't then the error be thrown (to be backwards compatible)?

rvdkooy commented 7 years ago

Good catch!

my last commit should address this.

What do we return when a translation fails? I now return null or should it be "" of even undefined?

R

martinandert commented 7 years ago

Thanks, Ronald!