jeresig / i18n-node-2

Lightweight simple translation module for node.js / express.js with dynamic json storage. Uses common __('...') syntax in app and templates.
MIT License
507 stars 79 forks source link

Dictionaries inheritance proposal #53

Closed awilczek closed 9 years ago

awilczek commented 9 years ago

It would be nice to have a feature like inheritance between dictionaries that could spare a lot of duplications. Assuming we have files de-de.json and at-de.json - both extending the root de.json - we would provide the options:

{
    "locales": ["de-de", "at-de"],
    "extension": ".json",
    "parents": ".{2}\.json"
}

Where parents is a regexp for extracting parent name for each file. What do you think?

gjuchault commented 9 years ago

What would be the interest of this ? Extending a language for a specific regional language ? You may do it by reading the files and extend the objects and passing an object on locales option. Could you make a pull request ?

awilczek commented 9 years ago

In our project - we would use this kind of feature for overriding the language by country-specific translations. But your example could be covered as well. And yes, we came to the solution you propose:

var locales = _.reduce(["de-de", "at-de"], function(result, locale) {
        var base = _.clone(require('../locales/' + locale.slice(-2)));
        result[locale] = _.merge(base, require('../locales/' + locale));
        return result;
}, {});

i18n.expressBind(app, {
    locales: locales,
    defaultLocale: "de-de"
});

But it would be nice to do it in just one config parameter. I'll prepare a PR in couple of weeks.

awilczek commented 9 years ago

https://github.com/jeresig/i18n-node-2/pull/59