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

Export utility functions #74

Closed opatut closed 8 years ago

opatut commented 8 years ago

I'd love to use choosePluralForm and interpolate without using the phrase-finding stuff (I have a much more complex logic for that and don't want to merge objects to create the phrases object every time).

Could we have them exported as statics on the Polyglot object?

ljharb commented 8 years ago

I'm hesitant to do that because it increases the surface area we have to maintain. Currently they're private, which means we don't have to directly test them and we're free to refactor them.

Curious, what's your complex logic for phrase-finding? I've never seen anyone implement i18n with anything besides key → value lookup, while swapping maps based on locale.

opatut commented 8 years ago

I'm fetching multiple dynamic overlays for the phrases object from the database. I don't want to fetch all of them, and certainly not merge multiple (huge) phrases objects each time or keep all of them in memory.

So I have my own key -> phrase resolution which I can freely optimize (i.e. Lookup in order from different sources).

Currently I am working around the missing exports by creating an instance with just one key and then interpolating that key. But that feels dirty and I am unsure of the overhead of creating new Polyglot instance each time I translate a string.

I could add tests and documentation if that helps... ;)

ljharb commented 8 years ago

They'd certainly need both tests and documentation were they to be exposed.

I wonder if perhaps a better approach would be to create a function that takes a phrase, a locale, and options (including substitutions), call that internally in t and polyglot, and then expose that one function directly?

opatut commented 8 years ago

Yeah, I could perform that refactoring and add tests. Brb :)

opatut commented 8 years ago

See updated #75.

Sorry for the large diff, somehow had to move stuff around to make eslint happy :)

ljharb commented 8 years ago

Closed in #75.