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

Support phrases in nested JSONs through the t() method #11

Closed olivierbeaulieu closed 10 years ago

olivierbeaulieu commented 10 years ago

I migrated to polyglot from another library that allowed nested JSONs as such:

var polyglot = new Polyglot({
  phrases: {
    "hello": "Hello",
    "actions": {
      "delete": "Delete",
      "add": "Add"
    }
  }
});

polyglot.t('actions.add') // "Add"

This wasn't possible with Polyglot so I added it. Simply add allowNestedJSON: true when instantiating Polyglot, and it'll work.

I know there's a similar PR opened, but I think it's cleaner to have everything pass through the t() method.

Let me know what you think.

spikebrehm commented 10 years ago

Cool, thanks so much for this -- there have been a few other requests for this functionality. I would support a PR that does this, however I think for simplicity and performance reasons, rather than trying to split the key and look up a nested phrase at translation time (in t()), it would be better to just normalize a phrase object into a string key at extend() time. Then the t() method can be unchanged. Make sense?

olivierbeaulieu commented 10 years ago

That makes a lot of sense, i'll update the code and give you an update once that's done!

spikebrehm commented 10 years ago

I might beat you to it! I'm about to open a PR with this functionality. Just couldn't help it -- too fun :)

spikebrehm commented 10 years ago

Check it out: https://github.com/airbnb/polyglot.js/pull/12

olivierbeaulieu commented 10 years ago

Terrific. Thanks a bunch!

We are switching our whole app to Polyglot because it's awesome, so let me know if you need anything, I'll be happy to help!

spikebrehm commented 10 years ago

Cool, merged! Give it a try. Closing for https://github.com/airbnb/polyglot.js/pull/12.