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

Remove warning when translating the empty key #85

Closed fzaninotto closed 7 years ago

fzaninotto commented 7 years ago

Some of the strings I translate can sometimes be disabled. Think for instance an Edit button, where the translation key is "edit", but that the developer can override to "" to hide the label.

When that happens, Polyglot warns in the console:

Warning: Missing translation for key: ""

I think this edge case should be handled by the library.

ljharb commented 7 years ago

This is a breaking change, and the warning is intentional.

Can you describe your use case where you can't just choose not to translate anything when you don't need any text?

fzaninotto commented 7 years ago

Here is the React component I use in the admin-on-rest library:

const EditButton = ({ basePath = '', label = 'aor.action.edit', record = {}, translate }) => <FlatButton
    label={translate(label)}
    icon={<ContentCreate />}
    containerElement={<Link to={linkToRecord(basePath, record.id)} />}
/>;

The idea is to offer a button with a label that the user can choose to override - or to remove, when called as follows:

<EditButton label="" basePath="/posts" record={record} />

Admin-on-rest is a framework to build admin GUI, so it must be very configurable, and let developers override pretty much anything.

Also, you say that the warning is intentional. Can you explain me the rationale?

ljharb commented 7 years ago

So, in your case, label ? translate(label) : '' should work just fine.

The warning is intentional because if you're calling a "translate" function with a value that wasn't previously seeded, it's very likely to be a bug.

fzaninotto commented 7 years ago

Yes, I know how to overcome the warning, but I'd have to repeat this one liner in all buttons (I have many).

Besides, the empty string is not undefined or null. If the developer passes this value, it's intentional - and as such, it should not throw an error in the console. At least IMHO.

ljharb commented 7 years ago

An empty string could be a valid translation key too.

In your case, you could just replace/alter your translate function such that it doesn't attempt to translate the empty string, thus solving it in one location in your codebase.

fzaninotto commented 7 years ago

Can you tell me what's the possible translation for the empty string as translation key, apart from an empty string? This doesn't make sense to me.

But you're right, whatever Polyglot doesn't do the way I want, I can do it myself. I just considered that the problem was with Polyglot and not the way I use it. I was probably wrong.