koala-interactive / frenchkiss.js

The blazing fast lightweight internationalization (i18n) module for javascript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Suggestion] Add support for nested objects

lauritzsh opened this issue · comments

For organization purposes it is nice to have a definition file with nested objects. Just a random example:

frenchkiss.set('en', {
  fruits: {
    apple: 'An apple',
    banana: 'A banana'
  },
  vegetables: {
    carrot: 'A carrot',
    daikon: 'A daikon'
  }
})

I would then use some lodash syntax and access them by t('fruits.banana') => 'A banana'.

Would this be out of scope for frenchkiss or something that could be added? Right now I'm converting our definition file to follow this dot syntax but to have it supported by frenchkiss would be really nice.

The main reasons we didn't add support for nested objects are the following :

  • What happened if we ask in your example t('fruits') ? Handled by onMissingKey() ?
  • What happened if we currently have a key named "fruits.apple" in the table ? Which one should have priority, the key or the nested resolution ?
  • Need some checks to avoid TypeError t('fruits.undefinedKey.typeerror') or object escaping t('fruits.constructor.constructor.name') === 'Function'
  • Need to implement nested support in extends function.
  • To keep the implementation fast and avoid re-parsing the key at each call, we need to store the generated function in an inline cache object (cache['fruits.apple'] = <generated function>) leading to problems while updating the table, deleting parent of nested entries or erasing the parent with a string.

For this reason, and because I don't really get the point of nested objects except developers comfort, we didn't feel the need to implement it in the first version.

But still, it's on the roadmap, even if I'm not sure about the first two questions.