Catalysts / cat-angular

Basic support for Angular Apps
Apache License 2.0
7 stars 16 forks source link

Allow applications to configure i18n key prefixes #48

Closed szabyg closed 8 years ago

szabyg commented 8 years ago

Right now all cat-angular cat-i18n keys look like this:

'cc.catalysts.cat-[breadcrumbs|menu|facets|paginated|sortable].[...whatever...]'.

Among all application-specific keys in our application, namespaced with the application's context, this strict naming convention is not elegant and we'd like to use our own prefix. Therefore I'd like to propose a configurable prefix for cat-i18n to optionally replace 'cc.catalysts' from the above prefix by whatever is the application's context.

tsalzinger commented 8 years ago

you can do so easily, just decorate the catI18nMessageSourceService and modify to the key to your liking. internally this keys will always be used, and I'm not inclined to add yet another mapping layer which by default maps from one internal key to another.

Something like this should do the trick:

function catI18nMessageSourceServiceDecorator(catI18nMessageSourceService) {
    var originalGetMessage = catI18nMessageSourceService.getMessage;
    var originalHasMessage = catI18nMessageSourceService.hasMessage;

    function adaptI18nKeyPrefix(key) {
        // TODO - place your key modifications here
        return key.replace('cc.catalysts.cat-', 'myOwnPrefix');
    }

    catI18nMessageSourceService.getMessage = function(key, locale) {
        return originalGetMessage(adaptI18nKeyPrefix(key), locale);
    };

    catI18nMessageSourceService.hasMessage = function(key, locale) {
        return originalHasMessage(adaptI18nKeyPrefix(key), locale);
    };

    return catI18nMessageSourceService;
}

angular
    .module('cat-angular-decorator', ['cat-angular'])
    .config(['$provide', function($provide) {
        $provide.decorate('catI18nMessageSourceService', ['$delegate', catI18nMessageSourceServiceDecorator]);
    }]);

No guarantee that the code is executable as is, just wrote it down as a kind of guideline

tsalzinger commented 8 years ago

closing this issue as the proposed decorator solution should be sufficient