Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom storage backed and initialization #91

Closed
wants to merge 3 commits into from
Closed

Custom storage backed and initialization #91

wants to merge 3 commits into from

Conversation

davidkalosi
Copy link

Hi,

no idea if anybody will find this interesting but here are some changes I implemented in order to enable loading from different sources then files. Our application stores all the translation messages as well as the list of supported locales in the database so I came up with this idea how to define custom storage backends without any hard dependencies to specific db drivers or ORMs.

the locales parameter can accept a closure with a callback parameter that is the actual locale that is being loaded.
then i added a reader parameter that recieves 2 parameters - one is the locale from the first function and the second param is the array of locales.

the code bellow is my initialization using sequelize.js.

i18n.configure({
    defaultLocale: 'en',
    updateFiles: false,
    locales: function (callback) {
        db.Intl.Locale.findAll().success(function (locales) {
            locales.forEach(function(locale) {
                callback(locale);                
            });
        });
    },
    reader: function(locale, result) {      
        result[locale.locale] = {};

        db.Intl.Error.findAll({where: {locale_id: locale.id}}).success(function (rows) {
            rows.forEach(function(row) {
                result[locale.locale][row.key] = row.message;                
            });
        });        
    }
});

another feature I have added is support for %text% style Symfony placeholders since we are sharing localization data between a Symfony 2 and node application.
This call handles the %id% placeholder in the text.

res.__('intl.locale.notfound', {id: req.params.id})

david

@Jack-Walter-Smith
Copy link

this is really useful feature and it's needed

@mashpie mashpie added this to To do in custom storage backend(s) via automation Aug 15, 2020
@mashpie mashpie added project tracked in a project and removed feature request labels Aug 18, 2020
@mashpie
Copy link
Owner

mashpie commented Aug 18, 2020

so... although not merged directly - will keep as todo for refactored backend project

@mashpie mashpie closed this Aug 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
project tracked in a project
Projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants