airbnb / polyglot.js

Give your JavaScript the ability to speak many languages.
http://airbnb.github.io/polyglot.js
BSD 2-Clause "Simplified" License
3.71k stars 208 forks source link

Error thrown with undefined option #79

Closed oliviertassinari closed 8 years ago

oliviertassinari commented 8 years ago

Let's take the following code

polyglot.t('snackbar_error', { message })

and the following key:

snackbar_error: 'Erreur : %{message}',

It's gonna output Erreur : foo when message = foo But when message = undefined it's throwing here:

TypeError: String.prototype.replace called on null or undefined

I think that it would be better not to crash and to display a warning in the console so we save time digging into it.

oliviertassinari commented 8 years ago

Actually, it's most likely a regression. I have some new errors in production link to this since the latest release of polyglot.

ljharb commented 8 years ago

Indeed, this works in v2.0.0 and is broken in v2.1.0. It seems that I broke it in 4e6fcf80a4286f27cce632dcd39a99584a9cbd2f - I'll get a v2.1.1 out that fixes it ASAP.

ljharb commented 8 years ago

v2.1.1 published with the fix. Thanks for the report!

klaemo commented 7 years ago

This change introduces different behavior than in 2.0.0.

It used to be that t('snackbar_error', { message }) with undefined message, would return 'Erreur : %{message}' (with the expression still included). Now the expression gets replaced with an empty string.

This could be considered breaking, at least it broke a bunch of tests for us.

ljharb commented 7 years ago

Thanks, I'll get a fix out asap.

ljharb commented 7 years ago

It's unfortunate that null and undefined have different behavior than "absent" - that may need to be corrected in v3.

ljharb commented 7 years ago

v2.1.2 and v2.2.1 published with the fix.

klaemo commented 7 years ago

Thanks so much for the quick fix! ❤